KiCad PCB EDA Suite
dialog_sim_settings.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2016 CERN
5  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * https://www.gnu.org/licenses/gpl-3.0.html
20  * or you may search the http://www.gnu.org website for the version 3 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include "dialog_sim_settings.h"
27 #include <confirm.h>
28 
29 #include <wx/tokenzr.h>
30 
32 //so there are a few tabs missing (e.g. pole-zero, distortion, sensitivity)
33 
34 // Helper function to shorten conditions
35 static bool empty( const wxTextEntryBase* aCtrl )
36 {
37  return aCtrl->GetValue().IsEmpty();
38 }
39 
40 
42  : DIALOG_SIM_SETTINGS_BASE( aParent ), m_exporter( nullptr ), m_spiceEmptyValidator( true )
43 {
44  m_posIntValidator.SetMin( 1 );
45 
46  m_acPointsNumber->SetValidator( m_posIntValidator );
47  m_acFreqStart->SetValidator( m_spiceValidator );
48  m_acFreqStop->SetValidator( m_spiceValidator );
49 
50  m_dcStart1->SetValidator( m_spiceValidator );
51  m_dcStop1->SetValidator( m_spiceValidator );
52  m_dcIncr1->SetValidator( m_spiceValidator );
53 
54  m_dcStart2->SetValidator( m_spiceValidator );
55  m_dcStop2->SetValidator( m_spiceValidator );
56  m_dcIncr2->SetValidator( m_spiceValidator );
57 
58  m_noisePointsNumber->SetValidator( m_posIntValidator );
59  m_noiseFreqStart->SetValidator( m_spiceValidator );
60  m_noiseFreqStop->SetValidator( m_spiceValidator );
61 
62  m_transStep->SetValidator( m_spiceValidator );
63  m_transFinal->SetValidator( m_spiceValidator );
64  m_transInitial->SetValidator( m_spiceEmptyValidator );
65 
66  m_sdbSizerOK->SetDefault();
68 
69 }
70 
71 
73 {
74  if( !wxDialog::TransferDataFromWindow() )
75  return false;
76 
77  wxWindow* page = m_simPages->GetCurrentPage();
78 
79  // AC analysis
80  if( page == m_pgAC )
81  {
82  if( !m_pgAC->Validate() )
83  return false;
84 
85  m_simCommand = wxString::Format( ".ac %s %s %s %s",
86  scaleToString( m_acScale->GetSelection() ),
87  m_acPointsNumber->GetValue(),
88  SPICE_VALUE( m_acFreqStart->GetValue() ).ToSpiceString(),
89  SPICE_VALUE( m_acFreqStop->GetValue() ).ToSpiceString() );
90  }
91 
92 
93  // DC transfer analysis
94  else if( page == m_pgDC )
95  {
96  // At least one source has to be enabled
97  if( !m_dcEnable1->IsChecked() && !m_dcEnable1->IsChecked() )
98  {
99  DisplayError( this, wxT( "You need to enable at least one source" ) );
100  return false;
101  }
102 
103  wxString simCmd = wxString( ".dc " );
104 
105  if( m_dcEnable1->IsChecked() )
106  {
107  if( empty( m_dcSource1 ) )
108  {
109  DisplayError( this, wxT( "You need to select DC source (sweep 1)" ) );
110  return false;
111  }
112 
114  // hence try..catch below
115  if( !m_dcStart1->Validate() || !m_dcStop1->Validate() || !m_dcIncr1->Validate() )
116  return false;
117 
118  try
119  {
120  simCmd += wxString::Format( "v%s %s %s %s",
121  m_dcSource1->GetValue(),
122  SPICE_VALUE( m_dcStart1->GetValue() ).ToSpiceString(),
123  SPICE_VALUE( m_dcStop1->GetValue() ).ToSpiceString(),
124  SPICE_VALUE( m_dcIncr1->GetValue() ).ToSpiceString() );
125  }
126  catch( std::exception& e )
127  {
128  DisplayError( this, e.what() );
129  return false;
130  }
131  }
132 
133  if( m_dcEnable2->IsChecked() )
134  {
135  if( empty( m_dcSource2 ) )
136  {
137  DisplayError( this, wxT( "You need to select DC source (sweep 2)" ) );
138  return false;
139  }
140 
142  // hence try..catch below
143  if( !m_dcStart2->Validate() || !m_dcStop2->Validate() || !m_dcIncr2->Validate() )
144  return false;
145 
146  try
147  {
148  simCmd += wxString::Format( "v%s %s %s %s",
149  m_dcSource2->GetValue(),
150  SPICE_VALUE( m_dcStart2->GetValue() ).ToSpiceString(),
151  SPICE_VALUE( m_dcStop2->GetValue() ).ToSpiceString(),
152  SPICE_VALUE( m_dcIncr2->GetValue() ).ToSpiceString() );
153  }
154  catch( std::exception& e )
155  {
156  DisplayError( this, e.what() );
157  return false;
158  }
159  }
160 
161  m_simCommand = simCmd;
162  }
163 
164 
165  // Noise analysis
166  else if( page == m_pgNoise )
167  {
169 
172  return false;
173 
174  wxString ref = empty( m_noiseRef )
175  ? wxString() : wxString::Format( ", %d", netMap.at( m_noiseRef->GetValue() ) );
176 
177  m_simCommand = wxString::Format( ".noise v(%d%s) v%s %s %s %s %s",
178  netMap.at( m_noiseMeas->GetValue() ), ref,
179  m_noiseSrc->GetValue(), scaleToString( m_noiseScale->GetSelection() ),
180  m_noisePointsNumber->GetValue(),
181  SPICE_VALUE( m_noiseFreqStart->GetValue() ).ToSpiceString(),
182  SPICE_VALUE( m_noiseFreqStop->GetValue() ).ToSpiceString() );
183  }
184 
185 
186  // DC operating point analysis
187  else if( page == m_pgOP )
188  {
189  m_simCommand = wxString( ".op" );
190  }
191 
192 
193  // Transient analysis
194  else if( page == m_pgTransient )
195  {
196  if( !m_pgTransient->Validate() )
197  return false;
198 
199  wxString initial = empty( m_transInitial )
200  ? "" : SPICE_VALUE( m_transInitial->GetValue() ).ToSpiceString();
201 
202  m_simCommand = wxString::Format( ".tran %s %s %s",
203  SPICE_VALUE( m_transStep->GetValue() ).ToSpiceString(),
204  SPICE_VALUE( m_transFinal->GetValue() ).ToSpiceString(),
205  initial );
206  }
207 
208 
209  // Custom directives
210  else if( page == m_pgCustom )
211  {
212  m_simCommand = m_customTxt->GetValue();
213  }
214 
215  else
216  {
217  return false;
218  }
219 
220  m_simCommand.Trim();
222 
223  return true;
224 }
225 
226 
228 {
230  if( empty( m_customTxt ) )
231  loadDirectives();
232 
233  if( m_simCommand.IsEmpty() && !empty( m_customTxt ) )
234  return parseCommand( m_customTxt->GetValue() );
235 
236  return true;
237 }
238 
239 
241 {
242  // Fill out comboboxes that allow to select nets
243  // Map comoboxes to their current values
244  std::map<wxComboBox*, wxString> cmbNet = {
245  { m_noiseMeas, m_noiseMeas->GetStringSelection() },
246  { m_noiseRef, m_noiseRef->GetStringSelection() }
247  };
248 
249  for( auto c : cmbNet )
250  c.first->Clear();
251 
252  for( auto net : m_exporter->GetNetIndexMap() )
253  {
254  for( auto c : cmbNet )
255  c.first->Append( net.first );
256  }
257 
258  // Try to restore the previous selection, if possible
259  for( auto c : cmbNet )
260  {
261  int idx = c.first->FindString( c.second );
262 
263  if( idx != wxNOT_FOUND )
264  c.first->SetSelection( idx );
265  }
266 
267 
268  // Fill out comboboxes that allow to select power sources
269  std::map<wxComboBox*, wxString> cmbSrc = {
270  { m_dcSource1, m_dcSource1->GetStringSelection() },
271  { m_dcSource2, m_dcSource2->GetStringSelection() },
272  { m_noiseSrc, m_noiseSrc->GetStringSelection() },
273  };
274 
275  for( auto c : cmbSrc )
276  c.first->Clear();
277 
278  for( auto item : m_exporter->GetSpiceItems() )
279  {
280  if( item.m_primitive == 'V' )
281  {
282  for( auto c : cmbSrc )
283  c.first->Append( item.m_refName );
284  }
285  }
286 
287  // Try to restore the previous selection, if possible
288  for( auto c : cmbSrc )
289  {
290  int idx = c.first->FindString( c.second );
291 
292  if( idx != wxNOT_FOUND )
293  c.first->SetSelection( idx );
294  }
295 
296  return DIALOG_SIM_SETTINGS_BASE::ShowModal();
297 }
298 
299 
300 bool DIALOG_SIM_SETTINGS::parseCommand( const wxString& aCommand )
301 {
302  if( aCommand.IsEmpty() )
303  return false;
304 
305  wxStringTokenizer tokenizer( aCommand, " " );
306  wxString tkn = tokenizer.GetNextToken().Lower();
307 
308  try {
309  if( tkn == ".ac" )
310  {
311  m_simPages->SetSelection( m_simPages->FindPage( m_pgAC ) );
312 
313  tkn = tokenizer.GetNextToken().Lower();
314 
315  if( tkn == "dec" )
316  m_acScale->SetSelection( 0 );
317  if( tkn == "oct" )
318  m_acScale->SetSelection( 1 );
319  if( tkn == "lin" )
320  m_acScale->SetSelection( 2 );
321  else
322  return false;
323 
324  // If the fields below are empty, it will be caught by the exception handler
325  m_acPointsNumber->SetValue( tokenizer.GetNextToken() );
326  m_acFreqStart->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
327  m_acFreqStop->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
328  }
329 
330  else if( tkn == ".dc" )
331  {
332  m_simPages->SetSelection( m_simPages->FindPage( m_pgDC ) );
333 
334  tkn = tokenizer.GetNextToken();
335 
336  if( !tkn.IsEmpty() )
337  {
338  m_dcSource1->SetValue( tkn );
339  m_dcStart1->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
340  m_dcStop1->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
341  m_dcIncr1->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
342 
343  // Check the 'Enabled' field, if all values are filled
344  m_dcEnable1->SetValue( !empty( m_dcSource1 ) && !empty( m_dcStart1 )
345  && !empty( m_dcStop1 ) && !empty( m_dcIncr1 ) );
346  }
347 
348  tkn = tokenizer.GetNextToken();
349 
350  if( !tkn.IsEmpty() )
351  {
352  m_dcSource2->SetValue( tkn );
353  m_dcStart2->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
354  m_dcStop2->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
355  m_dcIncr2->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
356 
357  // Check the 'Enabled' field, if all values are filled
358  m_dcEnable2->SetValue( !empty( m_dcSource2 ) && !empty( m_dcStart2 )
359  && !empty( m_dcStop2 ) && !empty( m_dcIncr2 ) );
360  }
361 
362  // Check if the directive is complete
363  if( !m_dcEnable1->IsChecked() || !m_dcEnable2->IsChecked() )
364  return false;
365  }
366 
367  else if( tkn == ".tran" )
368  {
369  m_simPages->SetSelection( m_simPages->FindPage( m_pgTransient ) );
370 
371  // If the fields below are empty, it will be caught by the exception handler
372  m_transStep->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
373  m_transFinal->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
374 
375  // Initial time is an optional field
376  tkn = tokenizer.GetNextToken();
377 
378  if( !tkn.IsEmpty() )
379  m_transInitial->SetValue( SPICE_VALUE( tkn ).ToSpiceString() );
380  }
381 
382  // Custom directives
383  else if( !empty( m_customTxt ) )
384  {
385  m_simPages->SetSelection( m_simPages->FindPage( m_pgCustom ) );
386  }
387  }
388  catch( ... )
389  {
390  // Nothing really bad has happened
391  return false;
392  }
393 
394  return true;
395 }
396 
397 
399 {
400  if( m_exporter )
401  m_customTxt->SetValue( m_exporter->GetSheetSimCommand() );
402 }
403 
404 
406 {
408 
409  if( !m_fixPassiveVals->IsChecked() )
411 
412  if( !m_fixIncludePaths->IsChecked() )
414 }
const NET_INDEX_MAP & GetNetIndexMap() const
Returns a map of circuit nodes to net names.
This file is part of the common library.
SPICE_VALIDATOR m_spiceEmptyValidator
static wxString scaleToString(int aOption)
Class DIALOG_SIM_SETTINGS_BASE.
SPICE_VALIDATOR m_spiceValidator
wxString GetSheetSimCommand()
Returns simulation command directives placed in schematic sheets (if any).
Helper class to handle Spice way of expressing values (e.g. 10.5 Meg)
Definition: spice_value.h:32
bool parseCommand(const wxString &aCommand)
Parses a Spice directive.
const SPICE_ITEM_LIST & GetSpiceItems() const
Returns list of items representing schematic components in the Spice world.
std::map< wxString, int > NET_INDEX_MAP
Net name to circuit node number mapping
bool TransferDataFromWindow() override
NETLIST_EXPORTER_PSPICE_SIM * m_exporter
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
DIALOG_SIM_SETTINGS(wxWindow *aParent)
bool TransferDataToWindow() override
wxIntegerValidator< int > m_posIntValidator
static bool empty(const wxTextEntryBase *aCtrl)
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:73