KiCad PCB EDA Suite
Loading...
Searching...
No Matches
ngspice.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 The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Tomasz Wlostowski <[email protected]>
8 * @author Maciej Suminski <[email protected]>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 3
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, you may find one here:
22 * https://www.gnu.org/licenses/gpl-3.0.html
23 * or you may search the http://www.gnu.org website for the version 3 license,
24 * or you may write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 */
27
28#include <config.h> // Needed for MSW compilation
29#include <common.h>
30#include <locale_io.h>
31#include <fmt/core.h>
32#include <paths.h>
33#include <richio.h>
34
35#include "spice_circuit_model.h"
36#include "ngspice.h"
37#include "simulator_reporter.h"
38#include "spice_settings.h"
39
40#include <wx/stdpaths.h>
41#include <wx/dir.h>
42#include <wx/log.h>
43
44#include <stdexcept>
45#include <algorithm>
46
47
55static const wxChar* const traceNgspice = wxT( "KICAD_NGSPICE" );
56
57
59 m_ngSpice_Init( nullptr ),
60 m_ngSpice_Circ( nullptr ),
61 m_ngSpice_Command( nullptr ),
62 m_ngGet_Vec_Info( nullptr ),
63 m_ngCM_Input_Path( nullptr ),
64 m_ngSpice_CurPlot( nullptr ),
65 m_ngSpice_AllPlots( nullptr ),
66 m_ngSpice_AllVecs( nullptr ),
67 m_ngSpice_Running( nullptr ),
68 m_ngSpice_LockRealloc( nullptr ),
69 m_ngSpice_UnlockRealloc( nullptr ),
70 m_error( false )
71{
72 init_dll();
73}
74
75
76NGSPICE::~NGSPICE() = default;
77
78
80{
81 for( const std::string& command : GetSettingCommands() )
82 {
83 wxLogTrace( traceNgspice, "Sending Ngspice configuration command '%s'.", command );
84 Command( command );
85 }
86}
87
88
89void NGSPICE::Init( const SPICE_SETTINGS* aSettings )
90{
91 Command( "reset" );
93}
94
95
97{
98 return wxString( m_ngSpice_CurPlot() );
99}
100
101
102std::vector<std::string> NGSPICE::AllVectors() const
103{
104 LOCALE_IO c_locale; // ngspice works correctly only with C locale
105 char* currentPlot = m_ngSpice_CurPlot();
106 char** allVectors = m_ngSpice_AllVecs( currentPlot );
107 int noOfVectors = 0;
108
109 std::vector<std::string> retVal;
110
111 if( allVectors != nullptr )
112 {
113 for( char** plot = allVectors; *plot != nullptr; plot++ )
114 noOfVectors++;
115
116 retVal.reserve( noOfVectors );
117
118 for( int i = 0; i < noOfVectors; i++, allVectors++ )
119 {
120 std::string vec = *allVectors;
121 retVal.push_back( std::move( vec ) );
122 }
123 }
124
125
126 return retVal;
127}
128
129
130std::vector<COMPLEX> NGSPICE::GetComplexVector( const std::string& aName, int aMaxLen )
131{
132 LOCALE_IO c_locale; // ngspice works correctly only with C locale
133 std::vector<COMPLEX> data;
134 NGSPICE_LOCK_REALLOC lock( this );
135
136 if( aMaxLen == 0 )
137 return data;
138
139 if( vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() ) )
140 {
141 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
142 data.reserve( length );
143
144 if( vi->v_realdata )
145 {
146 for( int i = 0; i < length; i++ )
147 data.emplace_back( vi->v_realdata[i], 0.0 );
148 }
149 else if( vi->v_compdata )
150 {
151 for( int i = 0; i < length; i++ )
152 data.emplace_back( vi->v_compdata[i].cx_real, vi->v_compdata[i].cx_imag );
153 }
154 }
155
156 return data;
157}
158
159
160std::vector<double> NGSPICE::GetRealVector( const std::string& aName, int aMaxLen )
161{
162 LOCALE_IO c_locale; // ngspice works correctly only with C locale
163 std::vector<double> data;
164 NGSPICE_LOCK_REALLOC lock( this );
165
166 if( aMaxLen == 0 )
167 return data;
168
169 if( vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() ) )
170 {
171 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
172 data.reserve( length );
173
174 if( vi->v_realdata )
175 {
176 for( int i = 0; i < length; i++ )
177 data.push_back( vi->v_realdata[i] );
178 }
179 else if( vi->v_compdata )
180 {
181 for( int i = 0; i < length; i++ )
182 data.push_back( vi->v_compdata[i].cx_real );
183 }
184 }
185
186 return data;
187}
188
189
190std::vector<double> NGSPICE::GetImaginaryVector( const std::string& aName, int aMaxLen )
191{
192 LOCALE_IO c_locale; // ngspice works correctly only with C locale
193 std::vector<double> data;
194 NGSPICE_LOCK_REALLOC lock( this );
195
196 if( aMaxLen == 0 )
197 return data;
198
199 if( vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() ) )
200 {
201 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
202 data.reserve( length );
203
204 if( vi->v_compdata )
205 {
206 for( int i = 0; i < length; i++ )
207 data.push_back( vi->v_compdata[i].cx_imag );
208 }
209 }
210
211 return data;
212}
213
214
215std::vector<double> NGSPICE::GetGainVector( const std::string& aName, int aMaxLen )
216{
217 LOCALE_IO c_locale; // ngspice works correctly only with C locale
218 std::vector<double> data;
219 NGSPICE_LOCK_REALLOC lock( this );
220
221 if( aMaxLen == 0 )
222 return data;
223
224 if( vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() ) )
225 {
226 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
227 data.reserve( length );
228
229 if( vi->v_realdata )
230 {
231 for( int i = 0; i < length; i++ )
232 data.push_back( vi->v_realdata[i] );
233 }
234 else if( vi->v_compdata )
235 {
236 for( int i = 0; i < length; i++ )
237 data.push_back( hypot( vi->v_compdata[i].cx_real, vi->v_compdata[i].cx_imag ) );
238 }
239 }
240
241 return data;
242}
243
244
245std::vector<double> NGSPICE::GetPhaseVector( const std::string& aName, int aMaxLen )
246{
247 LOCALE_IO c_locale; // ngspice works correctly only with C locale
248 std::vector<double> data;
249 NGSPICE_LOCK_REALLOC lock( this );
250
251 if( aMaxLen == 0 )
252 return data;
253
254 if( vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() ) )
255 {
256 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
257 data.reserve( length );
258
259 if( vi->v_realdata )
260 {
261 for( int i = 0; i < length; i++ )
262 data.push_back( 0.0 ); // well, that's life
263 }
264 else if( vi->v_compdata )
265 {
266 for( int i = 0; i < length; i++ )
267 data.push_back( atan2( vi->v_compdata[i].cx_imag, vi->v_compdata[i].cx_real ) );
268 }
269 }
270
271 return data;
272}
273
274
275bool NGSPICE::Attach( const std::shared_ptr<SIMULATION_MODEL>& aModel, const wxString& aSimCommand,
276 unsigned aSimOptions, const wxString& aInputPath, REPORTER& aReporter )
277{
278 SPICE_CIRCUIT_MODEL* model = dynamic_cast<SPICE_CIRCUIT_MODEL*>( aModel.get() );
279 STRING_FORMATTER formatter;
280
281 setCodemodelsInputPath( aInputPath.ToStdString() );
282
283 if( model && model->GetNetlist( aSimCommand, aSimOptions, &formatter, aReporter ) )
284 {
285 SIMULATOR::Attach( aModel, aSimCommand, aSimOptions, aInputPath, aReporter );
287 LoadNetlist( formatter.GetString() );
288
290 {
291 Command( "echo Command: esave none" );
292 Command( "esave none" );
293 }
294
295 return true;
296 }
297 else
298 {
299 SIMULATOR::Attach( nullptr, wxEmptyString, 0, wxEmptyString, aReporter );
300 return false;
301 }
302}
303
304
305bool NGSPICE::LoadNetlist( const std::string& aNetlist )
306{
307 LOCALE_IO c_locale; // ngspice works correctly only with C locale
308 std::vector<char*> lines;
309 std::stringstream ss( aNetlist );
310
311 m_netlist.erase();
312
313 for( std::string line; std::getline( ss, line ); )
314 {
315 lines.push_back( strdup( line.data() ) );
316 m_netlist += line;
317 m_netlist += '\n';
318 }
319
320 lines.push_back( nullptr ); // sentinel, as requested in ngSpice_Circ description
321
322 Command( "remcirc" );
323 bool success = !m_ngSpice_Circ( lines.data() );
324
325 for( char* line : lines )
326 free( line );
327
328 return success;
329}
330
331
333{
334 LOCALE_IO toggle; // ngspice works correctly only with C locale
335 return Command( "bg_run" ); // bg_* commands execute in a separate thread
336}
337
338
340{
341 LOCALE_IO c_locale; // ngspice works correctly only with C locale
342 return Command( "bg_halt" ); // bg_* commands execute in a separate thread
343}
344
345
347{
348 // No need to use C locale here
349 return m_ngSpice_Running();
350}
351
352
353bool NGSPICE::Command( const std::string& aCmd )
354{
355 LOCALE_IO c_locale; // ngspice works correctly only with C locale
356 validate();
357 return !m_ngSpice_Command( (char*) aCmd.c_str() );
358}
359
360
361wxString NGSPICE::GetXAxis( SIM_TYPE aType ) const
362{
363 switch( aType )
364 {
365 case ST_AC:
366 case ST_SP:
367 case ST_NOISE:
368 case ST_FFT:
369 return wxS( "frequency" );
370
371 case ST_DC:
372 // find plot, which ends with "-sweep"
373 for( wxString vector : AllVectors() )
374 {
375 if( vector.Lower().EndsWith( wxS( "-sweep" ) ) )
376 return vector;
377 }
378
379 return wxS( "sweep" );
380
381 case ST_TRAN:
382 return wxS( "time" );
383
384 default:
385 return wxEmptyString;
386 }
387}
388
389
390std::vector<std::string> NGSPICE::GetSettingCommands() const
391{
392 const NGSPICE_SETTINGS* settings = dynamic_cast<const NGSPICE_SETTINGS*>( Settings().get() );
393
394 std::vector<std::string> commands;
395
396 wxCHECK( settings, commands );
397
398 switch( settings->GetCompatibilityMode() )
399 {
400 case NGSPICE_COMPATIBILITY_MODE::USER_CONFIG: break;
401 case NGSPICE_COMPATIBILITY_MODE::NGSPICE: commands.emplace_back( "unset ngbehavior" ); break;
402 case NGSPICE_COMPATIBILITY_MODE::PSPICE: commands.emplace_back( "set ngbehavior=psa" ); break;
403 case NGSPICE_COMPATIBILITY_MODE::LTSPICE: commands.emplace_back( "set ngbehavior=lta" ); break;
404 case NGSPICE_COMPATIBILITY_MODE::LT_PSPICE: commands.emplace_back( "set ngbehavior=ltpsa" ); break;
405 case NGSPICE_COMPATIBILITY_MODE::HSPICE: commands.emplace_back( "set ngbehavior=hsa" ); break;
406 default: wxFAIL_MSG( wxString::Format( "Undefined NGSPICE_COMPATIBILITY_MODE %d.",
407 settings->GetCompatibilityMode() ) ); break;
408 }
409
410 return commands;
411}
412
413
414const std::string NGSPICE::GetNetlist() const
415{
416 return m_netlist;
417}
418
419
421{
422 if( m_initialized )
423 return;
424
425 LOCALE_IO c_locale; // ngspice works correctly only with C locale
426 const wxStandardPaths& stdPaths = wxStandardPaths::Get();
427
428 if( m_dll.IsLoaded() ) // enable force reload
429 m_dll.Unload();
430
431 // Extra effort to find libngspice
432 // @todo Shouldn't we be using the normal KiCad path searching mechanism here?
433 wxFileName dllFile( "", NGSPICE_DLL_FILE );
434#if defined(__WINDOWS__)
435 #if defined( _MSC_VER )
436 std::vector<std::string> dllPaths = { "" };
437 #else
438 std::vector<std::string> dllPaths = { "", "/mingw64/bin", "/mingw32/bin" };
439 #endif
440#elif defined(__WXMAC__)
441 std::vector<std::string> dllPaths = {
442 PATHS::GetOSXKicadUserDataDir().ToStdString() + "/PlugIns/ngspice",
443 PATHS::GetOSXKicadMachineDataDir().ToStdString() + "/PlugIns/ngspice",
444
445 // when running kicad.app
446 stdPaths.GetPluginsDir().ToStdString() + "/sim",
447
448 // when running eeschema.app
449 wxFileName( stdPaths.GetExecutablePath() ).GetPath().ToStdString() +
450 "/../../../../../Contents/PlugIns/sim"
451 };
452#else // Unix systems
453 std::vector<std::string> dllPaths = { "/usr/local/lib" };
454#endif
455
456 if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
457 dllPaths.emplace_back( NGSPICE_DLL_DIR );
458
459#if defined(__WINDOWS__) || (__WXMAC__)
460 for( const auto& path : dllPaths )
461 {
462 dllFile.SetPath( path );
463 wxLogTrace( traceNgspice, "libngspice search path: %s", dllFile.GetFullPath() );
464 m_dll.Load( dllFile.GetFullPath(), wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
465
466 if( m_dll.IsLoaded() )
467 {
468 wxLogTrace( traceNgspice, "libngspice path found in: %s", dllFile.GetFullPath() );
469 break;
470 }
471 }
472
473 if( !m_dll.IsLoaded() ) // try also the system libraries
474 m_dll.Load( wxDynamicLibrary::CanonicalizeName( "ngspice" ) );
475#else
476 // First, try the system libraries
477 m_dll.Load( NGSPICE_DLL_FILE, wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
478
479 // If failed, try some other paths:
480 if( !m_dll.IsLoaded() )
481 {
482 for( const auto& path : dllPaths )
483 {
484 dllFile.SetPath( path );
485 wxLogTrace( traceNgspice, "libngspice search path: %s", dllFile.GetFullPath() );
486 m_dll.Load( dllFile.GetFullPath(), wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
487
488 if( m_dll.IsLoaded() )
489 {
490 wxLogTrace( traceNgspice, "libngspice path found in: %s", dllFile.GetFullPath() );
491 break;
492 }
493 }
494 }
495#endif
496
497 if( !m_dll.IsLoaded() )
498 throw std::runtime_error( _( "Unable to load ngspice shared library. Please check your install." ).ToStdString() );
499
500 m_error = false;
501
502 // Obtain function pointers
503 m_ngSpice_Init = (ngSpice_Init) m_dll.GetSymbol( "ngSpice_Init" );
504 m_ngSpice_Circ = (ngSpice_Circ) m_dll.GetSymbol( "ngSpice_Circ" );
505 m_ngSpice_Command = (ngSpice_Command) m_dll.GetSymbol( "ngSpice_Command" );
506 m_ngGet_Vec_Info = (ngGet_Vec_Info) m_dll.GetSymbol( "ngGet_Vec_Info" );
507 m_ngCM_Input_Path = (ngCM_Input_Path) m_dll.GetSymbol( "ngCM_Input_Path" );
508 m_ngSpice_CurPlot = (ngSpice_CurPlot) m_dll.GetSymbol( "ngSpice_CurPlot" );
509 m_ngSpice_AllPlots = (ngSpice_AllPlots) m_dll.GetSymbol( "ngSpice_AllPlots" );
510 m_ngSpice_AllVecs = (ngSpice_AllVecs) m_dll.GetSymbol( "ngSpice_AllVecs" );
511 m_ngSpice_Running = (ngSpice_Running) m_dll.GetSymbol( "ngSpice_running" ); // it is not a typo
512
513 if( m_dll.HasSymbol( "ngSpice_LockRealloc" ) )
514 {
515 m_ngSpice_LockRealloc = (ngSpice_LockRealloc) m_dll.GetSymbol( "ngSpice_LockRealloc" );
516 m_ngSpice_UnlockRealloc = (ngSpice_UnlockRealloc) m_dll.GetSymbol( "ngSpice_UnlockRealloc" );
517 }
518
520 &cbBGThreadRunning, this );
521
522 // Load a custom spinit file, to fix the problem with loading .cm files
523 // Switch to the executable directory, so the relative paths are correct
524 wxString cwd( wxGetCwd() );
525 wxFileName exeDir( stdPaths.GetExecutablePath() );
526 wxSetWorkingDirectory( exeDir.GetPath() );
527
528 // Find *.cm files
529 std::string cmPath = findCmPath();
530
531 // __CMPATH is used in custom spinit file to point to the codemodels directory
532 if( !cmPath.empty() )
533 Command( "set __CMPATH=\"" + cmPath + "\"" );
534
535 // Possible relative locations for spinit file
536 const std::vector<std::string> spiceinitPaths =
537 {
538 ".",
539#ifdef __WXMAC__
540 stdPaths.GetPluginsDir().ToStdString() + "/sim/ngspice/scripts",
541 wxFileName( stdPaths.GetExecutablePath() ).GetPath().ToStdString() +
542 "/../../../../../Contents/PlugIns/sim/ngspice/scripts"
543#endif
544 "../share/kicad",
545 "../share",
546 "../../share/kicad",
547 "../../share"
548 };
549
550 bool foundSpiceinit = false;
551
552 for( const auto& path : spiceinitPaths )
553 {
554 wxLogTrace( traceNgspice, "ngspice init script search path: %s", path );
555
556 if( loadSpinit( path + "/spiceinit" ) )
557 {
558 wxLogTrace( traceNgspice, "ngspice path found in: %s", path );
559 foundSpiceinit = true;
560 break;
561 }
562 }
563
564 // Last chance to load codemodel files, we have not found
565 // spiceinit file, but we know the path to *.cm files
566 if( !foundSpiceinit && !cmPath.empty() )
567 loadCodemodels( cmPath );
568
569 // Restore the working directory
570 wxSetWorkingDirectory( cwd );
571
572 // Workarounds to avoid hang ups on certain errors
573 // These commands have to be called, no matter what is in the spinit file
574 // We have to allow interactive for user-defined signals. Hopefully whatever bug this was
575 // meant to address has gone away in the last 5 years...
576 //Command( "unset interactive" );
577 Command( "set noaskquit" );
578 Command( "set nomoremode" );
579
580 // reset and remcirc give an error if no circuit is loaded, so load an empty circuit at the
581 // start.
582
583 std::vector<char*> lines;
584 lines.push_back( strdup( "*" ) );
585 lines.push_back( strdup( ".end" ) );
586 lines.push_back( nullptr ); // Sentinel.
587
588 m_ngSpice_Circ( lines.data() );
589
590 for( auto line : lines )
591 free( line );
592
593 m_initialized = true;
594}
595
596
597bool NGSPICE::loadSpinit( const std::string& aFileName )
598{
599 if( !wxFileName::FileExists( aFileName ) )
600 return false;
601
602 wxTextFile file;
603
604 if( !file.Open( aFileName ) )
605 return false;
606
607 for( wxString& cmd = file.GetFirstLine(); !file.Eof(); cmd = file.GetNextLine() )
608 Command( cmd.ToStdString() );
609
610 return true;
611}
612
613
614std::string NGSPICE::findCmPath() const
615{
616 const std::vector<std::string> cmPaths =
617 {
618#ifdef __WXMAC__
619 "/Applications/ngspice/lib/ngspice",
620 "Contents/Frameworks",
621 wxStandardPaths::Get().GetPluginsDir().ToStdString() + "/sim/ngspice",
622 wxFileName( wxStandardPaths::Get().GetExecutablePath() ).GetPath().ToStdString() +
623 "/../../../../../Contents/PlugIns/sim/ngspice",
624 "../Plugins/sim/ngspice",
625#endif
626 "../eeschema/ngspice",
627 "../lib/ngspice",
628 "../../lib/ngspice",
629 "lib/ngspice",
630 "ngspice"
631 };
632
633 for( const auto& path : cmPaths )
634 {
635 wxLogTrace( traceNgspice, "ngspice code models search path: %s", path );
636
637 if( wxFileName::FileExists( path + "/spice2poly.cm" ) )
638 {
639 wxLogTrace( traceNgspice, "ngspice code models found in: %s", path );
640 return path;
641 }
642 }
643
644 return std::string();
645}
646
647
648bool NGSPICE::setCodemodelsInputPath( const std::string& aPath )
649{
650 if( !m_ngCM_Input_Path )
651 return false;
652
653 LOCALE_IO c_locale; // ngspice works correctly only with C locale
654
655 m_ngCM_Input_Path( aPath.c_str() );
656
657 return true;
658}
659
660
661bool NGSPICE::loadCodemodels( const std::string& aPath )
662{
663 wxArrayString cmFiles;
664 size_t count = wxDir::GetAllFiles( aPath, &cmFiles );
665
666 for( const auto& cm : cmFiles )
667 Command( fmt::format( "codemodel '{}'", cm.ToStdString() ) );
668
669 return count != 0;
670}
671
672
673int NGSPICE::cbSendChar( char* aWhat, int aId, void* aUser )
674{
675 NGSPICE* sim = reinterpret_cast<NGSPICE*>( aUser );
676
677 if( sim->m_reporter )
678 {
679 // strip stdout/stderr from the line
680 if( ( strncasecmp( aWhat, "stdout ", 7 ) == 0 )
681 || ( strncasecmp( aWhat, "stderr ", 7 ) == 0 ) )
682 {
683 aWhat += 7;
684 }
685
686 sim->m_reporter->Report( aWhat );
687 }
688
689 return 0;
690}
691
692
693int NGSPICE::cbSendStat( char *aWhat, int aId, void* aUser )
694{
695 return 0;
696}
697
698
699int NGSPICE::cbBGThreadRunning( NG_BOOL aFinished, int aId, void* aUser )
700{
701 NGSPICE* sim = reinterpret_cast<NGSPICE*>( aUser );
702
703 if( sim->m_reporter )
704 sim->m_reporter->OnSimStateChange( sim, aFinished ? SIM_IDLE : SIM_RUNNING );
705
706 return 0;
707}
708
709
710int NGSPICE::cbControlledExit( int aStatus, NG_BOOL aImmediate, NG_BOOL aExitOnQuit, int aId,
711 void* aUser )
712{
713 // Something went wrong, reload the dll
714 NGSPICE* sim = reinterpret_cast<NGSPICE*>( aUser );
715 sim->m_error = true;
716
717 return 0;
718}
719
720
722{
723 if( m_error )
724 {
725 m_initialized = false;
726 init_dll();
727 }
728}
729
730
732{
733 Command( "destroy all" );
734}
735
736
737bool NGSPICE::m_initialized = false;
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:41
Execute commands from a file.
Definition: ngspice.h:148
Container for Ngspice simulator settings.
NGSPICE_COMPATIBILITY_MODE GetCompatibilityMode() const
std::vector< std::string > AllVectors() const override final
Return a requested vector with complex values.
Definition: ngspice.cpp:102
ngSpice_Circ m_ngSpice_Circ
Definition: ngspice.h:134
bool Command(const std::string &aCmd) override final
Set a SIMULATOR_REPORTER object to receive the simulation log.
Definition: ngspice.cpp:353
char **(* ngSpice_AllVecs)(char *plotname)
Definition: ngspice.h:127
ngSpice_AllPlots m_ngSpice_AllPlots
Definition: ngspice.h:139
static int cbControlledExit(int aStatus, NG_BOOL aImmediate, NG_BOOL aExitOnQuit, int aId, void *aUser)
Definition: ngspice.cpp:710
int(* ngSpice_Circ)(char **circarray)
Definition: ngspice.h:121
ngSpice_UnlockRealloc m_ngSpice_UnlockRealloc
Definition: ngspice.h:143
char **(* ngSpice_AllPlots)(void)
Definition: ngspice.h:126
bool IsRunning() override final
Execute a Spice command as if it was typed into console.
Definition: ngspice.cpp:346
std::vector< double > GetImaginaryVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with magnitude values.
Definition: ngspice.cpp:190
virtual const std::string GetNetlist() const override final
Cleans simulation data (i.e.
Definition: ngspice.cpp:414
char *(* ngSpice_CurPlot)(void)
Definition: ngspice.h:125
ngSpice_Init m_ngSpice_Init
Definition: ngspice.h:133
void updateNgspiceSettings()
Check a few different locations for codemodel files and returns one if it exists.
Definition: ngspice.cpp:79
ngSpice_Command m_ngSpice_Command
Definition: ngspice.h:135
int(* ngSpice_LockRealloc)(void)
Definition: ngspice.h:129
void(* ngSpice_Init)(SendChar *, SendStat *, ControlledExit *, SendData *, SendInitData *, BGThreadRunning *, void *)
Definition: ngspice.h:118
bool setCodemodelsInputPath(const std::string &aPath)
Load codemodel files from a directory.
Definition: ngspice.cpp:648
wxString GetXAxis(SIM_TYPE aType) const override final
Definition: ngspice.cpp:361
wxString CurrentPlotName() const override final
Definition: ngspice.cpp:96
ngGet_Vec_Info m_ngGet_Vec_Info
Definition: ngspice.h:136
bool m_error
Error flag indicating that ngspice needs to be reloaded.
Definition: ngspice.h:192
ngCM_Input_Path m_ngCM_Input_Path
Definition: ngspice.h:137
std::vector< double > GetPhaseVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with phase values.
Definition: ngspice.cpp:245
virtual ~NGSPICE()
ngSpice_LockRealloc m_ngSpice_LockRealloc
Definition: ngspice.h:142
ngSpice_CurPlot m_ngSpice_CurPlot
Definition: ngspice.h:138
void init_dll()
Definition: ngspice.cpp:420
bool loadSpinit(const std::string &aFileName)
Definition: ngspice.cpp:597
bool Run() override final
Halt the simulation.
Definition: ngspice.cpp:332
bool LoadNetlist(const std::string &aNetlist) override final
Execute the simulation with currently loaded netlist.
Definition: ngspice.cpp:305
static int cbBGThreadRunning(NG_BOOL aFinished, int aId, void *aUser)
Definition: ngspice.cpp:699
std::vector< double > GetGainVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with phase values.
Definition: ngspice.cpp:215
bool Stop() override final
Check if simulation is running at the moment.
Definition: ngspice.cpp:339
int(* ngSpice_Command)(char *command)
Definition: ngspice.h:122
static bool m_initialized
Ngspice should be initialized only once.
Definition: ngspice.h:194
static int cbSendStat(char *what, int aId, void *aUser)
Definition: ngspice.cpp:693
static int cbSendChar(char *what, int aId, void *aUser)
Definition: ngspice.cpp:673
std::vector< double > GetRealVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with imaginary values.
Definition: ngspice.cpp:160
char *(* ngCM_Input_Path)(const char *path)
Definition: ngspice.h:124
std::vector< std::string > GetSettingCommands() const override final
Return current SPICE netlist used by the simulator.
Definition: ngspice.cpp:390
std::string m_netlist
Current netlist.
Definition: ngspice.h:196
ngSpice_AllVecs m_ngSpice_AllVecs
Definition: ngspice.h:140
int(* ngSpice_UnlockRealloc)(void)
Handle to DLL functions.
Definition: ngspice.h:130
pvector_info(* ngGet_Vec_Info)(char *vecname)
Definition: ngspice.h:123
ngSpice_Running m_ngSpice_Running
Definition: ngspice.h:141
void Clean() override final
Cleans simulation data (i.e.
Definition: ngspice.cpp:731
bool(* ngSpice_Running)(void)
Definition: ngspice.h:128
wxDynamicLibrary m_dll
Definition: ngspice.h:145
NGSPICE()
Definition: ngspice.cpp:58
void Init(const SPICE_SETTINGS *aSettings=nullptr) override final
Point out the model that will be used in future simulations.
Definition: ngspice.cpp:89
bool Attach(const std::shared_ptr< SIMULATION_MODEL > &aModel, const wxString &aSimCommand, unsigned aSimOptions, const wxString &aInputPath, REPORTER &aReporter) override final
Load a netlist for the simulation.
Definition: ngspice.cpp:275
void validate()
Definition: ngspice.cpp:721
std::string findCmPath() const
Send additional search path for codemodels to ngspice.
Definition: ngspice.cpp:614
bool loadCodemodels(const std::string &aPath)
Definition: ngspice.cpp:661
std::vector< COMPLEX > GetComplexVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with real values.
Definition: ngspice.cpp:130
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition: reporter.h:102
virtual void OnSimStateChange(SIMULATOR *aObject, SIM_STATE aNewState)=0
virtual bool Attach(const std::shared_ptr< SIMULATION_MODEL > &aModel, const wxString &aSimCommand, unsigned aSimOptions, const wxString &aInputPath, REPORTER &aReporter)
Point out the model that will be used in future simulations.
Definition: simulator.h:61
Special netlist exporter flavor that allows one to override simulation commands.
bool GetNetlist(const wxString &aCommand, unsigned aOptions, OUTPUTFORMATTER *aFormatter, REPORTER &aReporter)
Storage for simulator specific settings.
std::shared_ptr< SPICE_SETTINGS > & Settings()
Return the simulator configuration settings.
SIMULATOR_REPORTER * m_reporter
< Reporter object to receive simulation log.
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:449
const std::string & GetString()
Definition: richio.h:472
The common library.
#define _(s)
static const wxChar *const traceNgspice
Flag to enable debug output of Ngspice simulator.
Definition: ngspice.cpp:55
bool NG_BOOL
Definition: ngspice.h:46
SIM_TYPE
< Possible simulation types
Definition: sim_types.h:32
@ ST_SP
Definition: sim_types.h:43
@ ST_TRAN
Definition: sim_types.h:42
@ ST_NOISE
Definition: sim_types.h:37
@ ST_AC
Definition: sim_types.h:34
@ ST_DC
Definition: sim_types.h:35
@ ST_FFT
Definition: sim_types.h:44
@ SIM_IDLE
@ SIM_RUNNING