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_dcEnable2->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  wxString dcSource = m_exporter->GetSpiceDevice( m_dcSource1->GetValue() );
121 
122  simCmd += wxString::Format( "%s %s %s %s",
123  dcSource,
124  SPICE_VALUE( m_dcStart1->GetValue() ).ToSpiceString(),
125  SPICE_VALUE( m_dcStop1->GetValue() ).ToSpiceString(),
126  SPICE_VALUE( m_dcIncr1->GetValue() ).ToSpiceString() );
127  }
128  catch( std::exception& e )
129  {
130  DisplayError( this, e.what() );
131  return false;
132  }
133  }
134 
135  if( m_dcEnable2->IsChecked() )
136  {
137  if( empty( m_dcSource2 ) )
138  {
139  DisplayError( this, wxT( "You need to select DC source (sweep 2)" ) );
140  return false;
141  }
142 
144  // hence try..catch below
145  if( !m_dcStart2->Validate() || !m_dcStop2->Validate() || !m_dcIncr2->Validate() )
146  return false;
147 
148  try
149  {
150  wxString dcSource = m_exporter->GetSpiceDevice( m_dcSource2->GetValue() );
151 
152  simCmd += wxString::Format( "%s %s %s %s",
153  dcSource,
154  SPICE_VALUE( m_dcStart2->GetValue() ).ToSpiceString(),
155  SPICE_VALUE( m_dcStop2->GetValue() ).ToSpiceString(),
156  SPICE_VALUE( m_dcIncr2->GetValue() ).ToSpiceString() );
157  }
158  catch( std::exception& e )
159  {
160  DisplayError( this, e.what() );
161  return false;
162  }
163  }
164 
165  m_simCommand = simCmd;
166  }
167 
168 
169  // Noise analysis
170  else if( page == m_pgNoise )
171  {
173 
176  return false;
177 
178  wxString ref = empty( m_noiseRef )
179  ? wxString() : wxString::Format( ", %d", netMap.at( m_noiseRef->GetValue() ) );
180 
181  wxString noiseSource = m_exporter->GetSpiceDevice( m_noiseSrc->GetValue() );
182 
183  // Add voltage source prefix if needed
184  if( noiseSource[0] != 'v' && noiseSource[0] != 'V' )
185  noiseSource += 'v' + noiseSource;
186 
187  m_simCommand = wxString::Format( ".noise v(%d%s) v%s %s %s %s %s",
188  netMap.at( m_noiseMeas->GetValue() ), ref,
189  noiseSource, scaleToString( m_noiseScale->GetSelection() ),
190  m_noisePointsNumber->GetValue(),
191  SPICE_VALUE( m_noiseFreqStart->GetValue() ).ToSpiceString(),
192  SPICE_VALUE( m_noiseFreqStop->GetValue() ).ToSpiceString() );
193  }
194 
195 
196  // DC operating point analysis
197  else if( page == m_pgOP )
198  {
199  m_simCommand = wxString( ".op" );
200  }
201 
202 
203  // Transient analysis
204  else if( page == m_pgTransient )
205  {
206  if( !m_pgTransient->Validate() )
207  return false;
208 
209  wxString initial = empty( m_transInitial )
210  ? "" : SPICE_VALUE( m_transInitial->GetValue() ).ToSpiceString();
211 
212  m_simCommand = wxString::Format( ".tran %s %s %s",
213  SPICE_VALUE( m_transStep->GetValue() ).ToSpiceString(),
214  SPICE_VALUE( m_transFinal->GetValue() ).ToSpiceString(),
215  initial );
216  }
217 
218 
219  // Custom directives
220  else if( page == m_pgCustom )
221  {
222  m_simCommand = m_customTxt->GetValue();
223  }
224 
225  else
226  {
227  return false;
228  }
229 
230  m_simCommand.Trim();
232 
233  return true;
234 }
235 
236 
238 {
240  if( empty( m_customTxt ) )
241  loadDirectives();
242 
243  if( m_simCommand.IsEmpty() && !empty( m_customTxt ) )
244  return parseCommand( m_customTxt->GetValue() );
245 
246  return true;
247 }
248 
249 
251 {
252  // Fill out comboboxes that allows one to select nets
253  // Map comoboxes to their current values
254  std::map<wxComboBox*, wxString> cmbNet = {
255  { m_noiseMeas, m_noiseMeas->GetStringSelection() },
256  { m_noiseRef, m_noiseRef->GetStringSelection() }
257  };
258 
259  for( auto c : cmbNet )
260  c.first->Clear();
261 
262  for( auto net : m_exporter->GetNetIndexMap() )
263  {
264  for( auto c : cmbNet )
265  c.first->Append( net.first );
266  }
267 
268  // Try to restore the previous selection, if possible
269  for( auto c : cmbNet )
270  {
271  int idx = c.first->FindString( c.second );
272 
273  if( idx != wxNOT_FOUND )
274  c.first->SetSelection( idx );
275  }
276 
277 
278  // Fill out comboboxes that allows one to select power sources
279  std::map<wxComboBox*, wxString> cmbSrc = {
280  { m_dcSource1, m_dcSource1->GetStringSelection() },
281  { m_dcSource2, m_dcSource2->GetStringSelection() },
282  { m_noiseSrc, m_noiseSrc->GetStringSelection() },
283  };
284 
285  for( auto c : cmbSrc )
286  c.first->Clear();
287 
288  for( auto item : m_exporter->GetSpiceItems() )
289  {
290  if( item.m_primitive == 'V' )
291  {
292  for( auto c : cmbSrc )
293  c.first->Append( item.m_refName );
294  }
295  }
296 
297  // Try to restore the previous selection, if possible
298  for( auto c : cmbSrc )
299  {
300  int idx = c.first->FindString( c.second );
301 
302  if( idx != wxNOT_FOUND )
303  c.first->SetSelection( idx );
304  }
305 
306  return DIALOG_SIM_SETTINGS_BASE::ShowModal();
307 }
308 
309 
310 bool DIALOG_SIM_SETTINGS::parseCommand( const wxString& aCommand )
311 {
312  if( aCommand.IsEmpty() )
313  return false;
314 
315  wxStringTokenizer tokenizer( aCommand, " " );
316  wxString tkn = tokenizer.GetNextToken().Lower();
317 
318  try {
319  if( tkn == ".ac" )
320  {
321  m_simPages->SetSelection( m_simPages->FindPage( m_pgAC ) );
322 
323  tkn = tokenizer.GetNextToken().Lower();
324 
325  if( tkn == "dec" )
326  m_acScale->SetSelection( 0 );
327  if( tkn == "oct" )
328  m_acScale->SetSelection( 1 );
329  if( tkn == "lin" )
330  m_acScale->SetSelection( 2 );
331  else
332  return false;
333 
334  // If the fields below are empty, it will be caught by the exception handler
335  m_acPointsNumber->SetValue( tokenizer.GetNextToken() );
336  m_acFreqStart->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
337  m_acFreqStop->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
338  }
339 
340  else if( tkn == ".dc" )
341  {
342  m_simPages->SetSelection( m_simPages->FindPage( m_pgDC ) );
343 
344  tkn = tokenizer.GetNextToken();
345 
346  if( !tkn.IsEmpty() )
347  {
348  m_dcSource1->SetValue( tkn );
349  m_dcStart1->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
350  m_dcStop1->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
351  m_dcIncr1->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
352 
353  // Check the 'Enabled' field, if all values are filled
354  m_dcEnable1->SetValue( !empty( m_dcSource1 ) && !empty( m_dcStart1 )
355  && !empty( m_dcStop1 ) && !empty( m_dcIncr1 ) );
356  }
357 
358  tkn = tokenizer.GetNextToken();
359 
360  if( !tkn.IsEmpty() )
361  {
362  m_dcSource2->SetValue( tkn );
363  m_dcStart2->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
364  m_dcStop2->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
365  m_dcIncr2->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
366 
367  // Check the 'Enabled' field, if all values are filled
368  m_dcEnable2->SetValue( !empty( m_dcSource2 ) && !empty( m_dcStart2 )
369  && !empty( m_dcStop2 ) && !empty( m_dcIncr2 ) );
370  }
371 
372  // Check if the directive is complete
373  if( !m_dcEnable1->IsChecked() || !m_dcEnable2->IsChecked() )
374  return false;
375  }
376 
377  else if( tkn == ".tran" )
378  {
379  m_simPages->SetSelection( m_simPages->FindPage( m_pgTransient ) );
380 
381  // If the fields below are empty, it will be caught by the exception handler
382  m_transStep->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
383  m_transFinal->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
384 
385  // Initial time is an optional field
386  tkn = tokenizer.GetNextToken();
387 
388  if( !tkn.IsEmpty() )
389  m_transInitial->SetValue( SPICE_VALUE( tkn ).ToSpiceString() );
390  }
391 
392  // Custom directives
393  else if( !empty( m_customTxt ) )
394  {
395  m_simPages->SetSelection( m_simPages->FindPage( m_pgCustom ) );
396  }
397  }
398  catch( ... )
399  {
400  // Nothing really bad has happened
401  return false;
402  }
403 
404  return true;
405 }
406 
407 
409 {
410  if( m_exporter )
411  m_customTxt->SetValue( m_exporter->GetSheetSimCommand() );
412 }
413 
414 
416 {
418 
419  if( !m_fixPassiveVals->IsChecked() )
421 
422  if( !m_fixIncludePaths->IsChecked() )
424 }
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)
wxString GetSpiceDevice(const wxString &aComponent) const
Returns name of Spice device corresponding to a schematic component.
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:185