KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_sim_command.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-2022 CERN
5 * Copyright (C) 2016-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 3
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * https://www.gnu.org/licenses/gpl-3.0.html
21 * or you may search the http://www.gnu.org website for the version 3 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include "dialog_sim_command.h"
28#include <sim/ngspice.h>
29#include <sim/simulator_frame.h>
30#include <sim/sim_plot_tab.h>
31#include <confirm.h>
32
33#include <wx/tokenzr.h>
34
35#include <vector>
36#include <utility>
37
38
39// Helper function to shorten conditions
40static bool empty( const wxTextEntryBase* aCtrl )
41{
42 return aCtrl->GetValue().IsEmpty();
43}
44
45
46static void setStringSelection( wxChoice* aCtrl, const wxString& aStr )
47{
48 aCtrl->SetSelection( aCtrl->FindString( aStr ) );
49}
50
51
52static wxString getStringSelection( const wxChoice* aCtrl )
53{
54 if( aCtrl->GetSelection() >= 0 )
55 return aCtrl->GetString( aCtrl->GetSelection() );
56 else
57 return wxEmptyString;
58}
59
60
62 std::shared_ptr<SPICE_CIRCUIT_MODEL> aCircuitModel,
63 std::shared_ptr<SPICE_SETTINGS>& aSettings ) :
64 DIALOG_SIM_COMMAND_BASE( aParent ),
65 m_simulatorFrame( aParent ),
66 m_circuitModel( aCircuitModel ),
67 m_settings( aSettings ),
68 m_spiceEmptyValidator( true )
69{
70 m_simPages->Hide();
71
72 m_posIntValidator.SetMin( 1 );
73
74 m_acPointsNumber->SetValidator( m_posIntValidator );
75 m_acFreqStart->SetValidator( m_spiceValidator );
76 m_acFreqStop->SetValidator( m_spiceValidator );
77
78 m_spPointsNumber->SetValidator( m_posIntValidator );
79 m_spFreqStart->SetValidator( m_spiceValidator );
80 m_spFreqStop->SetValidator( m_spiceValidator );
81
82 m_dcStart1->SetValidator( m_spiceValidator );
83 m_dcStop1->SetValidator( m_spiceValidator );
84 m_dcIncr1->SetValidator( m_spiceValidator );
85
86 m_dcStart2->SetValidator( m_spiceValidator );
87 m_dcStop2->SetValidator( m_spiceValidator );
88 m_dcIncr2->SetValidator( m_spiceValidator );
89
91 m_noiseFreqStart->SetValidator( m_spiceValidator );
92 m_noiseFreqStop->SetValidator( m_spiceValidator );
93
94 m_transStep->SetValidator( m_spiceValidator );
95 m_transFinal->SetValidator( m_spiceValidator );
98
99 m_inputSignalsFilter->SetDescriptiveText( _( "Filter" ) );
100
101 wxChar type1 = getStringSelection( m_dcSourceType1 ).Upper().GetChar( 0 );
103
104 wxChar type2 = getStringSelection( m_dcSourceType2 ).Upper().GetChar( 0 );
106
107 // NoiseRef is optional
108 m_noiseRef->Append( wxEmptyString );
109
110 for( const std::string& net : m_circuitModel->GetNets() )
111 {
112 m_pzInput->Append( net );
113 m_pzInputRef->Append( net );
114 m_pzOutput->Append( net );
115 m_pzOutputRef->Append( net );
116
117 m_noiseMeas->Append( net );
118 m_noiseRef->Append( net );
119 }
120
121 for( const SPICE_ITEM& item : m_circuitModel->GetItems() )
122 {
123 if( item.model->GetDeviceType() == SIM_MODEL::DEVICE_T::V )
124 m_noiseSrc->Append( item.refName );
125 }
126
127 if( !dynamic_cast<NGSPICE_SETTINGS*>( aSettings.get() ) )
128 m_compatibilityModeSizer->Show( false );
129
130 int minWidth = GetTextExtent( wxS( "XXX.XXXXXXX" ) ).x;
131 m_y1Min->SetMinSize( wxSize( minWidth, -1 ) );
132 m_y1Max->SetMinSize( wxSize( minWidth, -1 ) );
133 m_y2Min->SetMinSize( wxSize( minWidth, -1 ) );
134 m_y2Max->SetMinSize( wxSize( minWidth, -1 ) );
135 m_y3Min->SetMinSize( wxSize( minWidth, -1 ) );
136 m_y3Max->SetMinSize( wxSize( minWidth, -1 ) );
137
138 m_bSizerY1->Show( false );
139 m_bSizerY2->Show( false );
140 m_bSizerY3->Show( false );
141
143}
144
146{
148 if( empty( m_customTxt ) )
150
151 m_fixIncludePaths->SetValue( m_settings->GetFixIncludePaths() );
152
153 if( NGSPICE_SETTINGS* settings = dynamic_cast<NGSPICE_SETTINGS*>( m_settings.get() ) )
154 {
155 switch( settings->GetCompatibilityMode() )
156 {
157 case NGSPICE_COMPATIBILITY_MODE::USER_CONFIG: m_compatibilityMode->SetSelection( 0 ); break;
158 case NGSPICE_COMPATIBILITY_MODE::NGSPICE: m_compatibilityMode->SetSelection( 1 ); break;
159 case NGSPICE_COMPATIBILITY_MODE::PSPICE: m_compatibilityMode->SetSelection( 2 ); break;
160 case NGSPICE_COMPATIBILITY_MODE::LTSPICE: m_compatibilityMode->SetSelection( 3 ); break;
161 case NGSPICE_COMPATIBILITY_MODE::LT_PSPICE: m_compatibilityMode->SetSelection( 4 ); break;
162 case NGSPICE_COMPATIBILITY_MODE::HSPICE: m_compatibilityMode->SetSelection( 5 ); break;
163 default: wxFAIL_MSG( wxString::Format( "Unknown NGSPICE_COMPATIBILITY_MODE %d.",
164 settings->GetCompatibilityMode() ) ); break;
165 }
166 }
167
168 return true;
169}
170
171
172void DIALOG_SIM_COMMAND::OnUpdateUILockY1( wxUpdateUIEvent& event )
173{
174 event.Enable( m_lockY1->GetValue() );
175}
176
177
178void DIALOG_SIM_COMMAND::OnUpdateUILockY2( wxUpdateUIEvent& event )
179{
180 event.Enable( m_lockY2->GetValue() );
181}
182
183
184void DIALOG_SIM_COMMAND::OnUpdateUILockY3( wxUpdateUIEvent& event )
185{
186 event.Enable( m_lockY3->GetValue() );
187}
188
189
191{
192 if( const SIM_PLOT_TAB* plotTab = dynamic_cast<const SIM_PLOT_TAB*>( aSimTab ) )
193 {
194 if( !plotTab->GetLabelY1().IsEmpty() )
195 {
196 m_bSizerY1->Show( true );
197 m_lockY1->SetLabel( wxString::Format( m_lockY1->GetLabel(), plotTab->GetLabelY1() ) );
198 m_y1Units->SetLabel( plotTab->GetUnitsY1() );
199
200 double min, max;
201 bool locked = plotTab->GetY1Scale( &min, &max );
202 m_lockY1->SetValue( locked );
203
204 if( !std::isnan( min ) )
205 m_y1Min->SetValue( SIM_VALUE::Normalize( min ) );
206
207 if( !std::isnan( max ) )
208 m_y1Max->SetValue( SIM_VALUE::Normalize( max ) );
209 }
210
211 if( !plotTab->GetLabelY2().IsEmpty() )
212 {
213 m_bSizerY2->Show( true );
214 m_lockY2->SetLabel( wxString::Format( m_lockY2->GetLabel(), plotTab->GetLabelY2() ) );
215 m_y2Units->SetLabel( plotTab->GetUnitsY2() );
216
217 double min, max;
218 bool locked = plotTab->GetY2Scale( &min, &max );
219 m_lockY2->SetValue( locked );
220
221 if( !std::isnan( min ) )
222 m_y2Min->SetValue( SIM_VALUE::Normalize( min ) );
223
224 if( !std::isnan( max ) )
225 m_y2Max->SetValue( SIM_VALUE::Normalize( max ) );
226 }
227
228 if( !plotTab->GetLabelY3().IsEmpty() )
229 {
230 m_bSizerY3->Show( true );
231 m_lockY3->SetLabel( wxString::Format( m_lockY3->GetLabel(), plotTab->GetLabelY3() ) );
232 m_y3Units->SetLabel( plotTab->GetUnitsY3() );
233
234 double min, max;
235 bool locked = plotTab->GetY3Scale( &min, &max );
236 m_lockY3->SetValue( locked );
237
238 if( !std::isnan( min ) )
239 m_y3Min->SetValue( SIM_VALUE::Normalize( min ) );
240
241 if( !std::isnan( max ) )
242 m_y3Max->SetValue( SIM_VALUE::Normalize( max ) );
243 }
244
245 m_grid->SetValue( plotTab->IsGridShown() );
246 m_legend->SetValue( plotTab->IsLegendShown() );
247 m_dottedSecondary->SetValue( plotTab->GetDottedSecondary() );
248
249#define GET_STR( val ) EDA_UNIT_UTILS::UI::MessageTextFromValue( unityScale, EDA_UNITS::UNSCALED, \
250 val, false /* no units */ )
251
252 m_marginLeft->SetValue( GET_STR( plotTab->GetPlotWin()->GetMarginLeft() ) );
253 m_marginTop->SetValue( GET_STR( plotTab->GetPlotWin()->GetMarginTop() ) );
254 m_marginRight->SetValue( GET_STR( plotTab->GetPlotWin()->GetMarginRight() ) );
255 m_marginBottom->SetValue( GET_STR( plotTab->GetPlotWin()->GetMarginBottom() ) );
256 }
257}
258
259
260wxString DIALOG_SIM_COMMAND::evaluateDCControls( wxChoice* aDcSource, wxTextCtrl* aDcStart,
261 wxTextCtrl* aDcStop, wxTextCtrl* aDcIncr )
262{
263 wxString dcSource;
264 wxWindow* ctrlWithError = nullptr;
265
266 if( aDcSource->GetSelection() >= 0 )
267 dcSource = aDcSource->GetString( aDcSource->GetSelection() );
268
269 if( dcSource.IsEmpty() )
270 {
271 DisplayError( this, _( "A DC source must be specified." ) );
272 ctrlWithError = aDcSource;
273 }
274 else if( !aDcStart->Validate() )
275 ctrlWithError = aDcStart;
276 else if( !aDcStop->Validate() )
277 ctrlWithError = aDcStop;
278 else if( !aDcIncr->Validate() )
279 ctrlWithError = aDcIncr;
280
281 if( ctrlWithError )
282 {
283 ctrlWithError->SetFocus();
284 return wxEmptyString;
285 }
286
287 // pick device name from exporter when something different than temperature is selected
288 if( dcSource.Cmp( "TEMP" ) )
289 dcSource = m_circuitModel->GetItemName( std::string( dcSource.ToUTF8() ) );
290
291 return wxString::Format( "%s %s %s %s", dcSource,
292 SPICE_VALUE( aDcStart->GetValue() ).ToSpiceString(),
293 SPICE_VALUE( aDcStop->GetValue() ).ToSpiceString(),
294 SPICE_VALUE( aDcIncr->GetValue() ).ToSpiceString() );
295}
296
297
299{
300 if( !wxDialog::TransferDataFromWindow() )
301 return false;
302
303 // The simulator dependent settings always get transferred.
304 if( NGSPICE_SETTINGS* settings = dynamic_cast<NGSPICE_SETTINGS*>( m_settings.get() ) )
305 {
306 switch( m_compatibilityMode->GetSelection() )
307 {
308 case 0: settings->SetCompatibilityMode( NGSPICE_COMPATIBILITY_MODE::USER_CONFIG ); break;
309 case 1: settings->SetCompatibilityMode( NGSPICE_COMPATIBILITY_MODE::NGSPICE ); break;
310 case 2: settings->SetCompatibilityMode( NGSPICE_COMPATIBILITY_MODE::PSPICE ); break;
311 case 3: settings->SetCompatibilityMode( NGSPICE_COMPATIBILITY_MODE::LTSPICE ); break;
312 case 4: settings->SetCompatibilityMode( NGSPICE_COMPATIBILITY_MODE::LT_PSPICE ); break;
313 case 5: settings->SetCompatibilityMode( NGSPICE_COMPATIBILITY_MODE::HSPICE ); break;
314 }
315 }
316
317 wxString previousSimCommand = m_simCommand;
318 wxWindow* page = m_simPages->GetCurrentPage();
319
320 if( page == m_pgAC ) // AC small-signal analysis
321 {
322 if( !m_pgAC->Validate() )
323 return false;
324
325 m_simCommand.Printf( ".ac %s %s %s %s",
326 scaleToString( m_acScale->GetSelection() ),
327 m_acPointsNumber->GetValue(),
328 SPICE_VALUE( m_acFreqStart->GetValue() ).ToSpiceString(),
329 SPICE_VALUE( m_acFreqStop->GetValue() ).ToSpiceString() );
330 }
331 else if( page == m_pgSP ) // S-params analysis
332 {
333 if( !m_pgSP->Validate() )
334 return false;
335
336 m_simCommand.Printf( ".sp %s %s %s %s %s", scaleToString( m_spScale->GetSelection() ),
337 m_spPointsNumber->GetValue(),
338 SPICE_VALUE( m_spFreqStart->GetValue() ).ToSpiceString(),
339 SPICE_VALUE( m_spFreqStop->GetValue() ).ToSpiceString(),
340 m_spDoNoise ? "1" : "" );
341 }
342 else if( page == m_pgDC ) // DC transfer analysis
343 {
344 wxString simCmd = wxString( ".dc " );
345
347
348 if( src1.IsEmpty() )
349 return false;
350 else
351 simCmd += src1;
352
353 if( m_dcEnable2->IsChecked() )
354 {
356
357 if( src2.IsEmpty() )
358 return false;
359 else
360 simCmd += " " + src2;
361
362 if( m_dcSource1->GetStringSelection() == m_dcSource2->GetStringSelection() )
363 {
364 DisplayError( this, _( "Source 1 and Source 2 must be different." ) );
365 return false;
366 }
367 }
368
369 m_simCommand = simCmd;
370 }
371 else if( page == m_pgFFT ) // Fast Fourier transform
372 {
373 m_simCommand = wxEmptyString;
374 wxString vectors;
375
376 for( int ii = 0; ii < (int) m_inputSignalsList->GetCount(); ++ii )
377 {
378 if( m_inputSignalsList->IsChecked( ii ) )
379 vectors += wxS( " " ) + m_inputSignalsList->GetString( ii );
380 }
381
382 if( m_linearize->IsChecked() )
383 m_simCommand = wxT( "linearize" ) + vectors + wxS( "\n" );
384
385 m_simCommand += wxT( "fft" ) + vectors;
386 }
387 else if( page == m_pgPZ ) // Pole-zero analyses
388 {
389 wxString input = m_pzInput->GetStringSelection();
390 wxString inputRef = m_pzInputRef->GetStringSelection();
391 wxString output = m_pzOutput->GetStringSelection();
392 wxString outputRef = m_pzOutputRef->GetStringSelection();
393 wxString transferFunction = wxS( "vol" );
394 wxString analyses = wxS( "pz" );
395
396 if( m_pzFunctionType->GetSelection() == 1 )
397 transferFunction = wxS( "cur" );
398
399 if( m_pzAnalyses->GetSelection() == 1 )
400 analyses = wxS( "pol" );
401 else if( m_pzAnalyses->GetSelection() == 2 )
402 analyses = wxS( "zer" );
403
404 m_simCommand.Printf( ".pz %s %s %s %s %s %s",
405 input,
406 inputRef,
407 output,
408 outputRef,
409 transferFunction,
410 analyses );
411 }
412 else if( page == m_pgNOISE ) // Noise analysis
413 {
414 wxString output = m_noiseMeas->GetStringSelection();
415 wxString ref = m_noiseRef->GetStringSelection();
416 wxString noiseSource = m_noiseSrc->GetStringSelection();
417
418 if( m_noiseFreqStart->IsEmpty() || m_noiseFreqStop->IsEmpty() )
419 {
420 DisplayError( this, _( "A frequency range must be specified." ) );
421 return false;
422 }
423
424 if( !ref.IsEmpty() )
425 ref = wxS( "," ) + m_circuitModel->GetItemName( std::string( ref.ToUTF8() ) );
426
427 m_simCommand.Printf( ".noise v(%s%s) %s %s %s %s %s %s",
428 output,
429 ref,
430 noiseSource,
431 scaleToString( m_noiseScale->GetSelection() ),
432 m_noisePointsNumber->GetValue(),
435 m_saveAllNoise->GetValue() ? "1" : "" );
436 }
437 else if( page == m_pgOP ) // DC operating point analysis
438 {
439 m_simCommand = wxString( ".op" );
440 }
441 else if( page == m_pgTRAN ) // Transient analysis
442 {
443 if( !m_pgTRAN->Validate() )
444 return false;
445
446 const wxString spc = wxS( " " );
447 const SPICE_VALUE timeStep( m_transStep->GetValue() );
448 const SPICE_VALUE finalTime( m_transFinal->GetValue() );
449
450 SPICE_VALUE startTime( 0 );
451
452 if( !empty( m_transInitial ) )
453 startTime = SPICE_VALUE( m_transInitial->GetValue() );
454
455 wxString optionals;
456
457 if( m_useInitialConditions->GetValue() )
458 optionals = wxS( "uic" );
459
460 if( !empty( m_transMaxStep ) )
461 {
462 optionals = SPICE_VALUE( m_transMaxStep->GetValue() ).ToSpiceString() + spc + optionals;
463 }
464 else if( !optionals.IsEmpty() )
465 {
466 SPICE_VALUE maxStep = ( finalTime - startTime ) / 50.0;
467
468 if( maxStep > timeStep )
469 maxStep = timeStep;
470
471 optionals = maxStep.ToSpiceString() + spc + optionals;
472 }
473
474 if( !empty( m_transInitial ) )
475 optionals = startTime.ToSpiceString() + spc + optionals;
476 else if( !optionals.IsEmpty() )
477 optionals = wxS( "0 " ) + optionals;
478
479 m_simCommand.Printf( wxS( ".tran %s %s %s" ), timeStep.ToSpiceString(),
480 finalTime.ToSpiceString(), optionals );
481 }
482 else if( page == m_pgCustom ) // Custom directives
483 {
484 m_simCommand = m_customTxt->GetValue();
485 }
486 else
487 {
488 return false;
489 }
490
491 if( previousSimCommand != m_simCommand )
492 m_simCommand.Trim();
493
494 m_settings->SetFixIncludePaths( m_fixIncludePaths->GetValue() );
495
496 return true;
497}
498
499
501{
503
504 if( !m_fixIncludePaths->GetValue() )
505 options &= ~NETLIST_EXPORTER_SPICE::OPTION_ADJUST_INCLUDE_PATHS;
506
507 if( !m_saveAllVoltages->GetValue() )
508 options &= ~NETLIST_EXPORTER_SPICE::OPTION_SAVE_ALL_VOLTAGES;
509
510 if( !m_saveAllCurrents->GetValue() )
511 options &= ~NETLIST_EXPORTER_SPICE::OPTION_SAVE_ALL_CURRENTS;
512
513 if( !m_saveAllDissipations->GetValue() )
514 options &= ~NETLIST_EXPORTER_SPICE::OPTION_SAVE_ALL_DISSIPATIONS;
515
516 aTab->SetSimOptions( options );
518
519#define TO_INT( ctrl ) (int) EDA_UNIT_UTILS::UI::ValueFromString( unityScale, EDA_UNITS::UNSCALED, \
520 ctrl->GetValue() )
521
522 if( SIM_PLOT_TAB* plotTab = dynamic_cast<SIM_PLOT_TAB*>( aTab ) )
523 {
524 if( !plotTab->GetLabelY1().IsEmpty() )
525 {
526 plotTab->SetY1Scale( m_lockY1->GetValue(),
527 SIM_VALUE::ToDouble( m_y1Min->GetValue().ToStdString() ),
528 SIM_VALUE::ToDouble( m_y1Max->GetValue().ToStdString() ) );
529 }
530
531 if( !plotTab->GetLabelY2().IsEmpty() )
532 {
533 plotTab->SetY2Scale( m_lockY2->GetValue(),
534 SIM_VALUE::ToDouble( m_y2Min->GetValue().ToStdString() ),
535 SIM_VALUE::ToDouble( m_y2Max->GetValue().ToStdString() ) );
536 }
537
538 if( !plotTab->GetLabelY3().IsEmpty() )
539 {
540 plotTab->SetY3Scale( m_lockY3->GetValue(),
541 SIM_VALUE::ToDouble( m_y3Min->GetValue().ToStdString() ),
542 SIM_VALUE::ToDouble( m_y3Max->GetValue().ToStdString() ) );
543 }
544
545 plotTab->GetPlotWin()->LockY( m_lockY1->GetValue()
546 || m_lockY2->GetValue()
547 || m_lockY3->GetValue() );
548
549 plotTab->ShowGrid( m_grid->GetValue() );
550 plotTab->ShowLegend( m_legend->GetValue() );
551 plotTab->SetDottedSecondary( m_dottedSecondary->GetValue() );
552
553 plotTab->GetPlotWin()->SetMarginLeft( TO_INT( m_marginLeft ) );
554 plotTab->GetPlotWin()->SetMarginRight( TO_INT( m_marginRight ) );
555 plotTab->GetPlotWin()->SetMarginTop( TO_INT( m_marginTop ) );
556 plotTab->GetPlotWin()->SetMarginBottom( TO_INT( m_marginBottom ) );
557
558 plotTab->GetPlotWin()->AdjustLimitedView();
559 plotTab->GetPlotWin()->UpdateAll();
560 }
561}
562
563
564void DIALOG_SIM_COMMAND::updateDCSources( wxChar aType, wxChoice* aSource )
565{
566 wxString prevSelection;
567
568 if( !aSource->IsEmpty() )
569 prevSelection = aSource->GetString( aSource->GetSelection() );
570
571 std::set<wxString> sourcesList;
572 bool enableSrcSelection = true;
573
574 if( aType != 'T' )
575 {
576 for( const SPICE_ITEM& item : m_circuitModel->GetItems() )
577 {
578 if( ( aType == 'R' && item.model->GetDeviceType() == SIM_MODEL::DEVICE_T::R )
579 || ( aType == 'V' && item.model->GetDeviceType() == SIM_MODEL::DEVICE_T::V )
580 || ( aType == 'I' && item.model->GetDeviceType() == SIM_MODEL::DEVICE_T::I ) )
581 {
582 sourcesList.insert( item.refName );
583 }
584 }
585
586 if( aSource == m_dcSource2 && !m_dcEnable2->IsChecked() )
587 enableSrcSelection = false;
588 }
589 else
590 {
591 prevSelection = wxT( "TEMP" );
592 sourcesList.insert( prevSelection );
593 enableSrcSelection = false;
594 }
595
596 aSource->Enable( enableSrcSelection );
597
598 aSource->Clear();
599
600 for( const wxString& src : sourcesList )
601 aSource->Append( src );
602
603 // Try to restore the previous selection, if possible
604 aSource->SetStringSelection( prevSelection );
605}
606
607
608void DIALOG_SIM_COMMAND::parseCommand( const wxString& aCommand )
609{
610 if( aCommand.IsEmpty() )
611 return;
612
613 if( aCommand == wxT( "*" ) )
614 {
615 SetTitle( _( "New Simulation Tab" ) );
616
617 m_commandType->Clear();
618
619 for( SIM_TYPE type : { ST_OP, ST_DC, ST_AC, ST_TRAN, ST_PZ, ST_NOISE, ST_SP, ST_FFT } )
620 {
621 m_commandType->Append( SPICE_SIMULATOR::TypeToName( type, true )
622 + wxT( " \u2014 " )
623 + SPICE_SIMULATOR::TypeToName( type, false ) );
624 }
625
626 m_commandTypeSizer->Show( true );
627 m_commandType->SetSelection( 0 );
628 m_simPages->SetSelection( m_simPages->FindPage( m_pgOP ) );
629 m_simPages->Show();
630 return;
631 }
632
634
635 SetTitle( SPICE_SIMULATOR::TypeToName( simType, true )
636 + wxT( " \u2014 " )
637 + SPICE_SIMULATOR::TypeToName( simType, false ) );
638
639 m_commandTypeSizer->Show( false );
640
641 wxStringTokenizer tokenizer( aCommand, wxS( " \t\n\r" ), wxTOKEN_STRTOK );
642 wxString token = tokenizer.GetNextToken().Lower();
643
644 switch( simType )
645 {
646 case ST_AC:
647 m_simPages->SetSelection( m_simPages->FindPage( m_pgAC ) );
648
649 token = tokenizer.GetNextToken().Lower();
650
651 for( SCALE_TYPE candidate : { DECADE, OCTAVE, LINEAR } )
652 {
653 if( scaleToString( candidate ) == token )
654 {
655 m_acScale->SetSelection( candidate );
656 break;
657 }
658 }
659
660 m_acPointsNumber->SetValue( tokenizer.GetNextToken() );
661 m_acFreqStart->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
662 m_acFreqStop->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
663 break;
664
665 case ST_SP:
666 m_simPages->SetSelection( m_simPages->FindPage( m_pgSP ) );
667
668 token = tokenizer.GetNextToken().Lower();
669
670 for( SCALE_TYPE candidate : { DECADE, OCTAVE, LINEAR } )
671 {
672 if( scaleToString( candidate ) == token )
673 {
674 m_spScale->SetSelection( candidate );
675 break;
676 }
677 }
678
679 m_spPointsNumber->SetValue( tokenizer.GetNextToken() );
680 m_spFreqStart->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
681 m_spFreqStop->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
682
683 if( tokenizer.HasMoreTokens() )
684 m_spDoNoise->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() == "1" );
685
686 break;
687
688 case ST_DC:
689 {
690 m_simPages->SetSelection( m_simPages->FindPage( m_pgDC ) );
691
692 SPICE_DC_PARAMS src1, src2;
693 src2.m_vincrement = SPICE_VALUE( -1 );
694
695 m_circuitModel->ParseDCCommand( aCommand, &src1, &src2 );
696
697 if( src1.m_source.IsSameAs( wxT( "TEMP" ), false ) )
698 setStringSelection( m_dcSourceType1, wxT( "TEMP" ) );
699 else
700 setStringSelection( m_dcSourceType1, src1.m_source.GetChar( 0 ) );
701
702 updateDCSources( src1.m_source.GetChar( 0 ), m_dcSource1 );
703 m_dcSource1->SetStringSelection( src1.m_source );
704 m_dcStart1->SetValue( src1.m_vstart.ToSpiceString() );
705 m_dcStop1->SetValue( src1.m_vend.ToSpiceString() );
706 m_dcIncr1->SetValue( src1.m_vincrement.ToSpiceString() );
707
708 if( src2.m_vincrement.ToDouble() != -1 )
709 {
710 if( src2.m_source.IsSameAs( wxT( "TEMP" ), false ) )
711 setStringSelection( m_dcSourceType2, wxT( "TEMP" ) );
712 else
713 setStringSelection( m_dcSourceType2, src2.m_source.GetChar( 0 ) );
714
715 updateDCSources( src2.m_source.GetChar( 0 ), m_dcSource2 );
716 m_dcSource2->SetStringSelection( src2.m_source );
717 m_dcStart2->SetValue( src2.m_vstart.ToSpiceString() );
718 m_dcStop2->SetValue( src2.m_vend.ToSpiceString() );
719 m_dcIncr2->SetValue( src2.m_vincrement.ToSpiceString() );
720
721 m_dcEnable2->SetValue( true );
722 }
723
724 break;
725 }
726
727 case ST_PZ:
728 {
729 m_simPages->SetSelection( m_simPages->FindPage( m_pgPZ ) );
730
731 wxString transferFunction;
732 wxString input, inputRef;
733 wxString output, outputRef;
734 SPICE_PZ_ANALYSES analyses;
735
736 m_circuitModel->ParsePZCommand( aCommand, &transferFunction, &input, &inputRef, &output,
737 &outputRef, &analyses );
738
739 m_pzInput->SetStringSelection( input );
740 m_pzInputRef->SetStringSelection( inputRef );
741 m_pzOutput->SetStringSelection( output );
742 m_pzOutputRef->SetStringSelection( outputRef );
743
744 m_pzFunctionType->SetSelection( transferFunction.Lower() == "cur" ? 1 : 0 );
745
746 if( analyses.m_Poles && analyses.m_Zeros )
747 m_pzAnalyses->SetSelection( 0 );
748 else if( analyses.m_Poles )
749 m_pzAnalyses->SetSelection( 1 );
750 else
751 m_pzAnalyses->SetSelection( 2 );
752
753 break;
754 }
755
756 case ST_NOISE:
757 {
758 m_simPages->SetSelection( m_simPages->FindPage( m_pgNOISE ) );
759
760 wxString output;
761 wxString ref;
762 wxString source;
763 wxString scale;
764 SPICE_VALUE pts;
765 SPICE_VALUE fStart;
766 SPICE_VALUE fStop;
767 bool saveAll;
768
769 m_circuitModel->ParseNoiseCommand( aCommand, &output, &ref, &source, &scale, &pts,
770 &fStart, &fStop, &saveAll );
771
772 m_noiseMeas->SetStringSelection( output );
773 m_noiseRef->SetStringSelection( ref );
774 m_noiseSrc->SetStringSelection( source );
775
776 for( SCALE_TYPE candidate : { DECADE, OCTAVE, LINEAR } )
777 {
778 if( scaleToString( candidate ) == scale )
779 {
780 m_noiseScale->SetSelection( candidate );
781 break;
782 }
783 }
784
785 m_noisePointsNumber->SetValue( pts.ToSpiceString() );
786 m_noiseFreqStart->SetValue( fStart.ToSpiceString() );
787 m_noiseFreqStop->SetValue( fStop.ToSpiceString() );
788
789 m_saveAllNoise->SetValue( saveAll );
790 break;
791 }
792
793 case ST_TRAN:
794 m_simPages->SetSelection( m_simPages->FindPage( m_pgTRAN ) );
795
796 // If the fields below are empty, it will be caught by the exception handler
797 m_transStep->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
798 m_transFinal->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
799
800 // Initial time is an optional field
801 token = tokenizer.GetNextToken();
802
803 if( !token.IsEmpty() )
804 m_transInitial->SetValue( SPICE_VALUE( token ).ToSpiceString() );
805
806 // Max step is an optional field
807 token = tokenizer.GetNextToken();
808
809 if( !token.IsEmpty() )
810 m_transMaxStep->SetValue( SPICE_VALUE( token ).ToSpiceString() );
811
812 // uic is an optional field
813 token = tokenizer.GetNextToken();
814
815 if( token.IsSameAs( wxS( "uic" ) ) )
816 m_useInitialConditions->SetValue( true );
817
818 break;
819
820 case ST_OP:
821 m_simPages->SetSelection( m_simPages->FindPage( m_pgOP ) );
822 break;
823
824 case ST_FFT:
825 {
826 m_simPages->SetSelection( m_simPages->FindPage( m_pgFFT ) );
827
828 while( tokenizer.HasMoreTokens() )
829 m_fftInputSignals.insert( tokenizer.GetNextToken() );
830
831 break;
832 }
833
834 default:
835 m_simPages->SetSelection( m_simPages->FindPage( m_pgCustom ) );
836 break;
837 }
838
839 m_simPages->Show();
840}
841
842
843void DIALOG_SIM_COMMAND::OnCommandType( wxCommandEvent& event )
844{
845 int sel = ST_UNKNOWN;
846 wxString str = m_commandType->GetString( event.GetSelection() );
847
848 for( int type = ST_UNKNOWN; type < ST_LAST; ++type )
849 {
850 if( str.StartsWith( SPICE_SIMULATOR::TypeToName( (SIM_TYPE) type, true ) ) )
851 sel = type;
852 }
853
854 switch( sel )
855 {
856 case ST_AC: m_simPages->SetSelection( m_simPages->FindPage( m_pgAC ) ); break;
857 case ST_SP: m_simPages->SetSelection( m_simPages->FindPage( m_pgSP ) ); break;
858 case ST_DC: m_simPages->SetSelection( m_simPages->FindPage( m_pgDC ) ); break;
859 case ST_PZ: m_simPages->SetSelection( m_simPages->FindPage( m_pgPZ ) ); break;
860 case ST_NOISE: m_simPages->SetSelection( m_simPages->FindPage( m_pgNOISE ) ); break;
861 case ST_TRAN: m_simPages->SetSelection( m_simPages->FindPage( m_pgTRAN ) ); break;
862 case ST_OP: m_simPages->SetSelection( m_simPages->FindPage( m_pgOP ) ); break;
863 case ST_FFT: m_simPages->SetSelection( m_simPages->FindPage( m_pgFFT ) ); break;
864 default: m_simPages->SetSelection( m_simPages->FindPage( m_pgCustom ) ); break;
865 }
866}
867
868
869void DIALOG_SIM_COMMAND::onSwapDCSources( wxCommandEvent& event )
870{
871 std::vector<std::pair<wxTextEntry*, wxTextEntry*>> textCtrl = { { m_dcStart1, m_dcStart2 },
872 { m_dcStop1, m_dcStop2 },
873 { m_dcIncr1, m_dcIncr2 } };
874
875 for( auto& couple : textCtrl )
876 {
877 wxString tmp = couple.first->GetValue();
878 couple.first->SetValue( couple.second->GetValue() );
879 couple.second->SetValue( tmp );
880 }
881
882 int src1 = m_dcSource1->GetSelection();
883 int src2 = m_dcSource2->GetSelection();
884
885 int sel = m_dcSourceType1->GetSelection();
886 m_dcSourceType1->SetSelection( m_dcSourceType2->GetSelection() );
887 m_dcSourceType2->SetSelection( sel );
888
889 wxChar type1 = getStringSelection( m_dcSourceType1 ).Upper().GetChar( 0 );
891 wxChar type2 = getStringSelection( m_dcSourceType2 ).Upper().GetChar( 0 );
893
894 m_dcSource1->SetSelection( src2 );
895 m_dcSource2->SetSelection( src1 );
896
899}
900
901
902void DIALOG_SIM_COMMAND::onDCSource1Selected( wxCommandEvent& event )
903{
904 wxChar type = m_dcSourceType1->GetString( m_dcSourceType1->GetSelection() ).Upper()[ 0 ];
907}
908
909
910void DIALOG_SIM_COMMAND::onDCSource2Selected( wxCommandEvent& event )
911{
912 wxChar type = m_dcSourceType2->GetString( m_dcSourceType2->GetSelection() ).Upper()[ 0 ];
915}
916
917
919{
920 bool is2ndSrcEnabled = m_dcEnable2->IsChecked();
921 wxChar type = '?';
922
923 if( is2ndSrcEnabled )
924 {
925 wxString fullType = getStringSelection( m_dcSourceType2 ).Upper();
926
927 if( fullType.Length() > 0 )
928 type = fullType.GetChar( 0 );
929 }
930
931 m_dcSourceType2->Enable( is2ndSrcEnabled );
932 m_dcSource2->Enable( is2ndSrcEnabled && type != 'T' );
933 m_dcStart2->Enable( is2ndSrcEnabled );
934 m_dcStop2->Enable( is2ndSrcEnabled );
935 m_dcIncr2->Enable( is2ndSrcEnabled );
936}
937
938
939void DIALOG_SIM_COMMAND::updateDCUnits( wxChar aType, wxStaticText* aStartValUnit,
940 wxStaticText* aEndValUnit, wxStaticText* aStepUnit )
941{
942 wxString unit;
943
944 switch( aType )
945 {
946 case 'V': unit = wxS( "V" ); break;
947 case 'I': unit = wxS( "A" ); break;
948 case 'R': unit = wxS( "Ω" ); break;
949 case 'T': unit = wxS( "°C" ); break;
950 }
951
952 aStartValUnit->SetLabel( unit );
953 aEndValUnit->SetLabel( unit );
954 aStepUnit->SetLabel( unit );
955
956 m_pgDC->Refresh();
957}
958
959
961{
962 if( m_circuitModel )
963 m_customTxt->SetValue( m_circuitModel->GetSchTextSimCommand() );
964}
965
966
967void DIALOG_SIM_COMMAND::OnFilterText( wxCommandEvent& aEvent )
968{
969 for( int ii = 0; ii < (int) m_inputSignalsList->GetCount(); ++ii )
970 {
971 if( m_inputSignalsList->IsChecked( ii ) )
972 m_fftInputSignals.insert( m_inputSignalsList->GetString( ii ) );
973 else
974 m_fftInputSignals.erase( m_inputSignalsList->GetString( ii ) );
975 }
976
977 m_inputSignalsList->Clear();
978
979 wxString aFilter = m_inputSignalsFilter->GetValue();
980
981 if( aFilter.IsEmpty() )
982 aFilter = wxS( "*" );
983
984 EDA_COMBINED_MATCHER matcher( aFilter.Upper(), CTX_SIGNAL );
985
986 for( const wxString& signal : m_simulatorFrame->Signals() )
987 {
988 if( matcher.Find( signal.Upper() ) )
989 {
990 m_inputSignalsList->Append( signal );
991
992 if( m_fftInputSignals.count( signal ) )
993 m_inputSignalsList->Check( m_inputSignalsList->GetCount() - 1 );
994 }
995 }
996}
997
998
999void DIALOG_SIM_COMMAND::OnFilterMouseMoved( wxMouseEvent& aEvent )
1000{
1001 wxPoint pos = aEvent.GetPosition();
1002 wxRect ctrlRect = m_inputSignalsFilter->GetScreenRect();
1003 int buttonWidth = ctrlRect.GetHeight(); // Presume buttons are square
1004
1005 if( m_inputSignalsFilter->IsSearchButtonVisible() && pos.x < buttonWidth )
1006 SetCursor( wxCURSOR_ARROW );
1007 else if( m_inputSignalsFilter->IsCancelButtonVisible() && pos.x > ctrlRect.GetWidth() - buttonWidth )
1008 SetCursor( wxCURSOR_ARROW );
1009 else
1010 SetCursor( wxCURSOR_IBEAM );
1011}
1012
1013
void SetupStandardButtons(std::map< int, wxString > aLabels={})
Class DIALOG_SIM_COMMAND_BASE.
wxString evaluateDCControls(wxChoice *aDcSource, wxTextCtrl *aDcStart, wxTextCtrl *aDcStop, wxTextCtrl *aDcIncr)
Read values from one DC sweep source to form a part of simulation command.
void onSwapDCSources(wxCommandEvent &event) override
static wxString scaleToString(int aOption)
void SetPlotSettings(const SIM_TAB *aSimTab)
void OnCommandType(wxCommandEvent &event) override
void OnFilterMouseMoved(wxMouseEvent &event) override
wxIntegerValidator< int > m_posIntValidator
void onDCSource2Selected(wxCommandEvent &event) override
void OnUpdateUILockY3(wxUpdateUIEvent &event) override
bool TransferDataFromWindow() override
void updateDCSources(wxChar aType, wxChoice *aSource)
Update DC sweep source with symbols from schematic.
void OnUpdateUILockY1(wxUpdateUIEvent &event) override
std::set< wxString > m_fftInputSignals
SPICE_VALIDATOR m_spiceValidator
void onDCEnableSecondSource(wxCommandEvent &event) override
void ApplySettings(SIM_TAB *aTab)
SIMULATOR_FRAME * m_simulatorFrame
void onDCSource1Selected(wxCommandEvent &event) override
void parseCommand(const wxString &aCommand)
Parse a Spice directive.
bool TransferDataToWindow() override
void updateDCUnits(wxChar aType, wxStaticText *aStartValUnit, wxStaticText *aEndValUnit, wxStaticText *aStepUnit)
Update units on labels depending on selected source.
void OnFilterText(wxCommandEvent &event) override
std::shared_ptr< SPICE_CIRCUIT_MODEL > m_circuitModel
DIALOG_SIM_COMMAND(SIMULATOR_FRAME *aParent, std::shared_ptr< SPICE_CIRCUIT_MODEL > aCircuitModel, std::shared_ptr< SPICE_SETTINGS > &aSettings)
std::shared_ptr< SPICE_SETTINGS > m_settings
void OnUpdateUILockY2(wxUpdateUIEvent &event) override
SPICE_VALIDATOR m_spiceEmptyValidator
bool Find(const wxString &aTerm, int &aMatchersTriggered, int &aPosition)
Container for Ngspice simulator settings.
The SIMULATOR_FRAME holds the main user-interface for running simulations.
const std::vector< wxString > Signals()
void ReloadSimulator(const wxString &aSimCommand, unsigned aSimOptions)
Re-send the current command and settings to the simulator.
void SetSimOptions(int aOptions)
Definition: sim_tab.h:53
static std::string Normalize(double aValue)
Definition: sim_value.cpp:405
static double ToDouble(const std::string &aString, double aDefault=NAN)
Definition: sim_value.cpp:418
static SIM_TYPE CommandToSimType(const wxString &aCmd)
Return simulation type basing on a simulation command directive.
static wxString TypeToName(SIM_TYPE aType, bool aShortName)
Return a string with simulation name based on enum.
Helper class to recognize Spice formatted values.
Definition: spice_value.h:56
wxString ToSpiceString() const
Return string value in Spice format (e.g.
double ToDouble() const
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:280
This file is part of the common library.
static void setStringSelection(wxChoice *aCtrl, const wxString &aStr)
static wxString getStringSelection(const wxChoice *aCtrl)
#define TO_INT(ctrl)
static bool empty(const wxTextEntryBase *aCtrl)
#define GET_STR(val)
#define _(s)
@ CTX_SIGNAL
SIM_TYPE
< Possible simulation types
Definition: sim_types.h:32
@ ST_SP
Definition: sim_types.h:43
@ ST_LAST
Definition: sim_types.h:45
@ ST_TRAN
Definition: sim_types.h:42
@ ST_UNKNOWN
Definition: sim_types.h:33
@ ST_NOISE
Definition: sim_types.h:37
@ ST_AC
Definition: sim_types.h:34
@ ST_DC
Definition: sim_types.h:35
@ ST_OP
Definition: sim_types.h:38
@ ST_FFT
Definition: sim_types.h:44
@ ST_PZ
Definition: sim_types.h:39
const int scale
SPICE_VALUE m_vincrement