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