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 std::string& 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( std::string( dcSource.ToUTF8() ) );
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 {
427 ref = wxS( "," )
428 + wxString( m_circuitModel->GetItemName( std::string( ref.ToUTF8() ) ) );
429 }
430
431 m_simCommand.Printf( ".noise v(%s%s) %s %s %s %s %s %s",
432 output,
433 ref,
434 noiseSource,
435 scaleToString( m_noiseScale->GetSelection() ),
436 m_noisePointsNumber->GetValue(),
439 m_saveAllNoise->GetValue() ? "1" : "" );
440 }
441 else if( page == m_pgOP ) // DC operating point analysis
442 {
443 m_simCommand = wxString( ".op" );
444 }
445 else if( page == m_pgTRAN ) // Transient analysis
446 {
447 if( !m_pgTRAN->Validate() )
448 return false;
449
450 const wxString spc = wxS( " " );
451 const SPICE_VALUE timeStep( m_transStep->GetValue() );
452 const SPICE_VALUE finalTime( m_transFinal->GetValue() );
453
454 SPICE_VALUE startTime( 0 );
455
456 if( !empty( m_transInitial ) )
457 startTime = SPICE_VALUE( m_transInitial->GetValue() );
458
459 wxString optionals;
460
461 if( m_useInitialConditions->GetValue() )
462 optionals = wxS( "uic" );
463
464 if( !empty( m_transMaxStep ) )
465 {
466 optionals = SPICE_VALUE( m_transMaxStep->GetValue() ).ToSpiceString() + spc + optionals;
467 }
468 else if( !optionals.IsEmpty() )
469 {
470 SPICE_VALUE maxStep = ( finalTime - startTime ) / 50.0;
471
472 if( maxStep > timeStep )
473 maxStep = timeStep;
474
475 optionals = maxStep.ToSpiceString() + spc + optionals;
476 }
477
478 if( !empty( m_transInitial ) )
479 optionals = startTime.ToSpiceString() + spc + optionals;
480 else if( !optionals.IsEmpty() )
481 optionals = wxS( "0 " ) + optionals;
482
483 m_simCommand.Printf( wxS( ".tran %s %s %s" ), timeStep.ToSpiceString(),
484 finalTime.ToSpiceString(), optionals );
485 }
486 else if( page == m_pgCustom ) // Custom directives
487 {
488 m_simCommand = m_customTxt->GetValue();
489 }
490 else
491 {
492 return false;
493 }
494
495 if( previousSimCommand != m_simCommand )
496 m_simCommand.Trim();
497
498 m_settings->SetFixIncludePaths( m_fixIncludePaths->GetValue() );
499
500 return true;
501}
502
503
505{
507
508 if( !m_fixIncludePaths->GetValue() )
509 options &= ~NETLIST_EXPORTER_SPICE::OPTION_ADJUST_INCLUDE_PATHS;
510
511 if( !m_saveAllVoltages->GetValue() )
512 options &= ~NETLIST_EXPORTER_SPICE::OPTION_SAVE_ALL_VOLTAGES;
513
514 if( !m_saveAllCurrents->GetValue() )
515 options &= ~NETLIST_EXPORTER_SPICE::OPTION_SAVE_ALL_CURRENTS;
516
517 if( !m_saveAllDissipations->GetValue() )
518 options &= ~NETLIST_EXPORTER_SPICE::OPTION_SAVE_ALL_DISSIPATIONS;
519
520 if( !m_saveAllEvents->GetValue() )
521 options &= ~NETLIST_EXPORTER_SPICE::OPTION_SAVE_ALL_EVENTS;
522
523 aTab->SetSimOptions( options );
525
526#define TO_INT( ctrl ) (int) EDA_UNIT_UTILS::UI::ValueFromString( unityScale, EDA_UNITS::UNSCALED, \
527 ctrl->GetValue() )
528
529 if( SIM_PLOT_TAB* plotTab = dynamic_cast<SIM_PLOT_TAB*>( aTab ) )
530 {
531 if( !plotTab->GetLabelY1().IsEmpty() )
532 {
533 plotTab->SetY1Scale( m_lockY1->GetValue(),
534 SIM_VALUE::ToDouble( m_y1Min->GetValue().ToStdString() ),
535 SIM_VALUE::ToDouble( m_y1Max->GetValue().ToStdString() ) );
536 }
537
538 if( !plotTab->GetLabelY2().IsEmpty() )
539 {
540 plotTab->SetY2Scale( m_lockY2->GetValue(),
541 SIM_VALUE::ToDouble( m_y2Min->GetValue().ToStdString() ),
542 SIM_VALUE::ToDouble( m_y2Max->GetValue().ToStdString() ) );
543 }
544
545 if( !plotTab->GetLabelY3().IsEmpty() )
546 {
547 plotTab->SetY3Scale( m_lockY3->GetValue(),
548 SIM_VALUE::ToDouble( m_y3Min->GetValue().ToStdString() ),
549 SIM_VALUE::ToDouble( m_y3Max->GetValue().ToStdString() ) );
550 }
551
552 plotTab->GetPlotWin()->LockY( m_lockY1->GetValue()
553 || m_lockY2->GetValue()
554 || m_lockY3->GetValue() );
555
556 plotTab->ShowGrid( m_grid->GetValue() );
557 plotTab->ShowLegend( m_legend->GetValue() );
558 plotTab->SetDottedSecondary( m_dottedSecondary->GetValue() );
559
560 plotTab->GetPlotWin()->SetMarginLeft( TO_INT( m_marginLeft ) );
561 plotTab->GetPlotWin()->SetMarginRight( TO_INT( m_marginRight ) );
562 plotTab->GetPlotWin()->SetMarginTop( TO_INT( m_marginTop ) );
563 plotTab->GetPlotWin()->SetMarginBottom( TO_INT( m_marginBottom ) );
564
565 plotTab->GetPlotWin()->AdjustLimitedView();
566 plotTab->GetPlotWin()->UpdateAll();
567 }
568}
569
570
571void DIALOG_SIM_COMMAND::updateDCSources( wxChar aType, wxChoice* aSource )
572{
573 wxString prevSelection;
574
575 if( !aSource->IsEmpty() )
576 prevSelection = aSource->GetString( aSource->GetSelection() );
577
578 std::set<wxString> sourcesList;
579 bool enableSrcSelection = true;
580
581 if( aType != 'T' )
582 {
583 for( const SPICE_ITEM& item : m_circuitModel->GetItems() )
584 {
585 if( ( aType == 'R' && item.model->GetDeviceType() == SIM_MODEL::DEVICE_T::R )
586 || ( aType == 'V' && item.model->GetDeviceType() == SIM_MODEL::DEVICE_T::V )
587 || ( aType == 'I' && item.model->GetDeviceType() == SIM_MODEL::DEVICE_T::I ) )
588 {
589 sourcesList.insert( item.refName );
590 }
591 }
592
593 if( aSource == m_dcSource2 && !m_dcEnable2->IsChecked() )
594 enableSrcSelection = false;
595 }
596 else
597 {
598 prevSelection = wxT( "TEMP" );
599 sourcesList.insert( prevSelection );
600 enableSrcSelection = false;
601 }
602
603 aSource->Enable( enableSrcSelection );
604
605 aSource->Clear();
606
607 for( const wxString& src : sourcesList )
608 aSource->Append( src );
609
610 // Try to restore the previous selection, if possible
611 aSource->SetStringSelection( prevSelection );
612}
613
614
615void DIALOG_SIM_COMMAND::parseCommand( const wxString& aCommand )
616{
617 if( aCommand.IsEmpty() )
618 return;
619
620 if( aCommand == wxT( "*" ) )
621 {
622 SetTitle( _( "New Simulation Tab" ) );
623
624 m_commandType->Clear();
625
626 for( SIM_TYPE type : { ST_OP, ST_DC, ST_AC, ST_TRAN, ST_PZ, ST_NOISE, ST_SP, ST_FFT } )
627 {
628 m_commandType->Append( SPICE_SIMULATOR::TypeToName( type, true )
629 + wxT( " \u2014 " )
630 + SPICE_SIMULATOR::TypeToName( type, false ) );
631 }
632
633 m_commandTypeSizer->Show( true );
634 m_commandType->SetSelection( 0 );
635 m_simPages->SetSelection( m_simPages->FindPage( m_pgOP ) );
636 m_simPages->Show();
637 return;
638 }
639
641
642 SetTitle( SPICE_SIMULATOR::TypeToName( simType, true )
643 + wxT( " \u2014 " )
644 + SPICE_SIMULATOR::TypeToName( simType, false ) );
645
646 m_commandTypeSizer->Show( false );
647
648 wxStringTokenizer tokenizer( aCommand, wxS( " \t\n\r" ), wxTOKEN_STRTOK );
649 wxString token = tokenizer.GetNextToken().Lower();
650
651 switch( simType )
652 {
653 case ST_AC:
654 m_simPages->SetSelection( m_simPages->FindPage( m_pgAC ) );
655
656 token = tokenizer.GetNextToken().Lower();
657
658 for( SCALE_TYPE candidate : { DECADE, OCTAVE, LINEAR } )
659 {
660 if( scaleToString( candidate ) == token )
661 {
662 m_acScale->SetSelection( candidate );
663 break;
664 }
665 }
666
667 m_acPointsNumber->SetValue( tokenizer.GetNextToken() );
668 m_acFreqStart->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
669 m_acFreqStop->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
670 break;
671
672 case ST_SP:
673 m_simPages->SetSelection( m_simPages->FindPage( m_pgSP ) );
674
675 token = tokenizer.GetNextToken().Lower();
676
677 for( SCALE_TYPE candidate : { DECADE, OCTAVE, LINEAR } )
678 {
679 if( scaleToString( candidate ) == token )
680 {
681 m_spScale->SetSelection( candidate );
682 break;
683 }
684 }
685
686 m_spPointsNumber->SetValue( tokenizer.GetNextToken() );
687 m_spFreqStart->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
688 m_spFreqStop->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
689
690 if( tokenizer.HasMoreTokens() )
691 m_spDoNoise->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() == "1" );
692
693 break;
694
695 case ST_DC:
696 {
697 m_simPages->SetSelection( m_simPages->FindPage( m_pgDC ) );
698
699 SPICE_DC_PARAMS src1, src2;
700 src2.m_vincrement = SPICE_VALUE( -1 );
701
702 m_circuitModel->ParseDCCommand( aCommand, &src1, &src2 );
703
704 if( src1.m_source.IsSameAs( wxT( "TEMP" ), false ) )
705 setStringSelection( m_dcSourceType1, wxT( "TEMP" ) );
706 else
707 setStringSelection( m_dcSourceType1, src1.m_source.GetChar( 0 ) );
708
709 updateDCSources( src1.m_source.GetChar( 0 ), m_dcSource1 );
710 m_dcSource1->SetStringSelection( src1.m_source );
711 m_dcStart1->SetValue( src1.m_vstart.ToSpiceString() );
712 m_dcStop1->SetValue( src1.m_vend.ToSpiceString() );
713 m_dcIncr1->SetValue( src1.m_vincrement.ToSpiceString() );
714
715 if( src2.m_vincrement.ToDouble() != -1 )
716 {
717 if( src2.m_source.IsSameAs( wxT( "TEMP" ), false ) )
718 setStringSelection( m_dcSourceType2, wxT( "TEMP" ) );
719 else
720 setStringSelection( m_dcSourceType2, src2.m_source.GetChar( 0 ) );
721
722 updateDCSources( src2.m_source.GetChar( 0 ), m_dcSource2 );
723 m_dcSource2->SetStringSelection( src2.m_source );
724 m_dcStart2->SetValue( src2.m_vstart.ToSpiceString() );
725 m_dcStop2->SetValue( src2.m_vend.ToSpiceString() );
726 m_dcIncr2->SetValue( src2.m_vincrement.ToSpiceString() );
727
728 m_dcEnable2->SetValue( true );
729 }
730
731 break;
732 }
733
734 case ST_PZ:
735 {
736 m_simPages->SetSelection( m_simPages->FindPage( m_pgPZ ) );
737
738 wxString transferFunction;
739 wxString input, inputRef;
740 wxString output, outputRef;
741 SPICE_PZ_ANALYSES analyses;
742
743 m_circuitModel->ParsePZCommand( aCommand, &transferFunction, &input, &inputRef, &output,
744 &outputRef, &analyses );
745
746 m_pzInput->SetStringSelection( input );
747 m_pzInputRef->SetStringSelection( inputRef );
748 m_pzOutput->SetStringSelection( output );
749 m_pzOutputRef->SetStringSelection( outputRef );
750
751 m_pzFunctionType->SetSelection( transferFunction.Lower() == "cur" ? 1 : 0 );
752
753 if( analyses.m_Poles && analyses.m_Zeros )
754 m_pzAnalyses->SetSelection( 0 );
755 else if( analyses.m_Poles )
756 m_pzAnalyses->SetSelection( 1 );
757 else
758 m_pzAnalyses->SetSelection( 2 );
759
760 break;
761 }
762
763 case ST_NOISE:
764 {
765 m_simPages->SetSelection( m_simPages->FindPage( m_pgNOISE ) );
766
767 wxString output;
768 wxString ref;
769 wxString source;
770 wxString scale;
771 SPICE_VALUE pts;
772 SPICE_VALUE fStart;
773 SPICE_VALUE fStop;
774 bool saveAll;
775
776 m_circuitModel->ParseNoiseCommand( aCommand, &output, &ref, &source, &scale, &pts,
777 &fStart, &fStop, &saveAll );
778
779 m_noiseMeas->SetStringSelection( output );
780 m_noiseRef->SetStringSelection( ref );
781 m_noiseSrc->SetStringSelection( source );
782
783 for( SCALE_TYPE candidate : { DECADE, OCTAVE, LINEAR } )
784 {
785 if( scaleToString( candidate ) == scale )
786 {
787 m_noiseScale->SetSelection( candidate );
788 break;
789 }
790 }
791
792 m_noisePointsNumber->SetValue( pts.ToSpiceString() );
793 m_noiseFreqStart->SetValue( fStart.ToSpiceString() );
794 m_noiseFreqStop->SetValue( fStop.ToSpiceString() );
795
796 m_saveAllNoise->SetValue( saveAll );
797 break;
798 }
799
800 case ST_TRAN:
801 m_simPages->SetSelection( m_simPages->FindPage( m_pgTRAN ) );
802
803 // If the fields below are empty, it will be caught by the exception handler
804 m_transStep->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
805 m_transFinal->SetValue( SPICE_VALUE( tokenizer.GetNextToken() ).ToSpiceString() );
806
807 // Initial time is an optional field
808 token = tokenizer.GetNextToken();
809
810 if( !token.IsEmpty() )
811 m_transInitial->SetValue( SPICE_VALUE( token ).ToSpiceString() );
812
813 // Max step is an optional field
814 token = tokenizer.GetNextToken();
815
816 if( !token.IsEmpty() )
817 m_transMaxStep->SetValue( SPICE_VALUE( token ).ToSpiceString() );
818
819 // uic is an optional field
820 token = tokenizer.GetNextToken();
821
822 if( token.IsSameAs( wxS( "uic" ) ) )
823 m_useInitialConditions->SetValue( true );
824
825 break;
826
827 case ST_OP:
828 m_simPages->SetSelection( m_simPages->FindPage( m_pgOP ) );
829 break;
830
831 case ST_FFT:
832 {
833 m_simPages->SetSelection( m_simPages->FindPage( m_pgFFT ) );
834
835 while( tokenizer.HasMoreTokens() )
836 m_fftInputSignals.insert( tokenizer.GetNextToken() );
837
838 break;
839 }
840
841 default:
842 m_simPages->SetSelection( m_simPages->FindPage( m_pgCustom ) );
843 break;
844 }
845
846 m_simPages->Show();
847}
848
849
850void DIALOG_SIM_COMMAND::OnCommandType( wxCommandEvent& event )
851{
852 int sel = ST_UNKNOWN;
853 wxString str = m_commandType->GetString( event.GetSelection() );
854
855 for( int type = ST_UNKNOWN; type < ST_LAST; ++type )
856 {
857 if( str.StartsWith( SPICE_SIMULATOR::TypeToName( (SIM_TYPE) type, true ) ) )
858 sel = type;
859 }
860
861 switch( sel )
862 {
863 case ST_AC: m_simPages->SetSelection( m_simPages->FindPage( m_pgAC ) ); break;
864 case ST_SP: m_simPages->SetSelection( m_simPages->FindPage( m_pgSP ) ); break;
865 case ST_DC: m_simPages->SetSelection( m_simPages->FindPage( m_pgDC ) ); break;
866 case ST_PZ: m_simPages->SetSelection( m_simPages->FindPage( m_pgPZ ) ); break;
867 case ST_NOISE: m_simPages->SetSelection( m_simPages->FindPage( m_pgNOISE ) ); break;
868 case ST_TRAN: m_simPages->SetSelection( m_simPages->FindPage( m_pgTRAN ) ); break;
869 case ST_OP: m_simPages->SetSelection( m_simPages->FindPage( m_pgOP ) ); break;
870 case ST_FFT: m_simPages->SetSelection( m_simPages->FindPage( m_pgFFT ) ); break;
871 default: m_simPages->SetSelection( m_simPages->FindPage( m_pgCustom ) ); break;
872 }
873}
874
875
876void DIALOG_SIM_COMMAND::onSwapDCSources( wxCommandEvent& event )
877{
878 std::vector<std::pair<wxTextEntry*, wxTextEntry*>> textCtrl = { { m_dcStart1, m_dcStart2 },
879 { m_dcStop1, m_dcStop2 },
880 { m_dcIncr1, m_dcIncr2 } };
881
882 for( auto& couple : textCtrl )
883 {
884 wxString tmp = couple.first->GetValue();
885 couple.first->SetValue( couple.second->GetValue() );
886 couple.second->SetValue( tmp );
887 }
888
889 int src1 = m_dcSource1->GetSelection();
890 int src2 = m_dcSource2->GetSelection();
891
892 int sel = m_dcSourceType1->GetSelection();
893 m_dcSourceType1->SetSelection( m_dcSourceType2->GetSelection() );
894 m_dcSourceType2->SetSelection( sel );
895
896 wxChar type1 = getStringSelection( m_dcSourceType1 ).Upper().GetChar( 0 );
898 wxChar type2 = getStringSelection( m_dcSourceType2 ).Upper().GetChar( 0 );
900
901 m_dcSource1->SetSelection( src2 );
902 m_dcSource2->SetSelection( src1 );
903
906}
907
908
909void DIALOG_SIM_COMMAND::onDCSource1Selected( wxCommandEvent& event )
910{
911 wxChar type = m_dcSourceType1->GetString( m_dcSourceType1->GetSelection() ).Upper()[ 0 ];
914}
915
916
917void DIALOG_SIM_COMMAND::onDCSource2Selected( wxCommandEvent& event )
918{
919 wxChar type = m_dcSourceType2->GetString( m_dcSourceType2->GetSelection() ).Upper()[ 0 ];
922}
923
924
926{
927 bool is2ndSrcEnabled = m_dcEnable2->IsChecked();
928 wxChar type = '?';
929
930 if( is2ndSrcEnabled )
931 {
932 wxString fullType = getStringSelection( m_dcSourceType2 ).Upper();
933
934 if( fullType.Length() > 0 )
935 type = fullType.GetChar( 0 );
936 }
937
938 m_dcSourceType2->Enable( is2ndSrcEnabled );
939 m_dcSource2->Enable( is2ndSrcEnabled && type != 'T' );
940 m_dcStart2->Enable( is2ndSrcEnabled );
941 m_dcStop2->Enable( is2ndSrcEnabled );
942 m_dcIncr2->Enable( is2ndSrcEnabled );
943}
944
945
946void DIALOG_SIM_COMMAND::updateDCUnits( wxChar aType, wxStaticText* aStartValUnit,
947 wxStaticText* aEndValUnit, wxStaticText* aStepUnit )
948{
949 wxString unit;
950
951 switch( aType )
952 {
953 case 'V': unit = wxS( "V" ); break;
954 case 'I': unit = wxS( "A" ); break;
955 case 'R': unit = wxS( "Ω" ); break;
956 case 'T': unit = wxS( "°C" ); break;
957 }
958
959 aStartValUnit->SetLabel( unit );
960 aEndValUnit->SetLabel( unit );
961 aStepUnit->SetLabel( unit );
962
963 m_pgDC->Refresh();
964}
965
966
968{
969 if( m_circuitModel )
970 m_customTxt->SetValue( m_circuitModel->GetSchTextSimCommand() );
971}
972
973
974void DIALOG_SIM_COMMAND::OnFilterText( wxCommandEvent& aEvent )
975{
976 for( int ii = 0; ii < (int) m_inputSignalsList->GetCount(); ++ii )
977 {
978 if( m_inputSignalsList->IsChecked( ii ) )
979 m_fftInputSignals.insert( m_inputSignalsList->GetString( ii ) );
980 else
981 m_fftInputSignals.erase( m_inputSignalsList->GetString( ii ) );
982 }
983
984 m_inputSignalsList->Clear();
985
986 wxString aFilter = m_inputSignalsFilter->GetValue();
987
988 if( aFilter.IsEmpty() )
989 aFilter = wxS( "*" );
990
991 EDA_COMBINED_MATCHER matcher( aFilter.Upper(), CTX_SIGNAL );
992
993 for( const wxString& signal : m_simulatorFrame->Signals() )
994 {
995 if( matcher.Find( signal.Upper() ) )
996 {
997 m_inputSignalsList->Append( signal );
998
999 if( m_fftInputSignals.count( signal ) )
1000 m_inputSignalsList->Check( m_inputSignalsList->GetCount() - 1 );
1001 }
1002 }
1003}
1004
1005
1006void DIALOG_SIM_COMMAND::OnFilterMouseMoved( wxMouseEvent& aEvent )
1007{
1008 wxPoint pos = aEvent.GetPosition();
1009 wxRect ctrlRect = m_inputSignalsFilter->GetScreenRect();
1010 int buttonWidth = ctrlRect.GetHeight(); // Presume buttons are square
1011
1012 if( m_inputSignalsFilter->IsSearchButtonVisible() && pos.x < buttonWidth )
1013 SetCursor( wxCURSOR_ARROW );
1014 else if( m_inputSignalsFilter->IsCancelButtonVisible() && pos.x > ctrlRect.GetWidth() - buttonWidth )
1015 SetCursor( wxCURSOR_ARROW );
1016 else
1017 SetCursor( wxCURSOR_IBEAM );
1018}
1019
1020
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: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:161
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