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 (C) 2018-2024 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( 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 {
183 wxASSERT( vi->v_compdata[i].cx_imag == 0.0 );
184 data.push_back( vi->v_compdata[i].cx_real );
185 }
186 }
187 }
188
189 return data;
190}
191
192
193std::vector<double> NGSPICE::GetImaginaryVector( const std::string& aName, int aMaxLen )
194{
195 LOCALE_IO c_locale; // ngspice works correctly only with C locale
196 std::vector<double> data;
197 NGSPICE_LOCK_REALLOC lock( this );
198
199 if( aMaxLen == 0 )
200 return data;
201
202 if( vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() ) )
203 {
204 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
205 data.reserve( length );
206
207 if( vi->v_compdata )
208 {
209 for( int i = 0; i < length; i++ )
210 data.push_back( vi->v_compdata[i].cx_imag );
211 }
212 }
213
214 return data;
215}
216
217
218std::vector<double> NGSPICE::GetGainVector( const std::string& aName, int aMaxLen )
219{
220 LOCALE_IO c_locale; // ngspice works correctly only with C locale
221 std::vector<double> data;
222 NGSPICE_LOCK_REALLOC lock( this );
223
224 if( aMaxLen == 0 )
225 return data;
226
227 if( vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() ) )
228 {
229 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
230 data.reserve( length );
231
232 if( vi->v_realdata )
233 {
234 for( int i = 0; i < length; i++ )
235 data.push_back( vi->v_realdata[i] );
236 }
237 else if( vi->v_compdata )
238 {
239 for( int i = 0; i < length; i++ )
240 data.push_back( hypot( vi->v_compdata[i].cx_real, vi->v_compdata[i].cx_imag ) );
241 }
242 }
243
244 return data;
245}
246
247
248std::vector<double> NGSPICE::GetPhaseVector( const std::string& aName, int aMaxLen )
249{
250 LOCALE_IO c_locale; // ngspice works correctly only with C locale
251 std::vector<double> data;
252 NGSPICE_LOCK_REALLOC lock( this );
253
254 if( aMaxLen == 0 )
255 return data;
256
257 if( vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() ) )
258 {
259 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
260 data.reserve( length );
261
262 if( vi->v_realdata )
263 {
264 for( int i = 0; i < length; i++ )
265 data.push_back( 0.0 ); // well, that's life
266 }
267 else if( vi->v_compdata )
268 {
269 for( int i = 0; i < length; i++ )
270 data.push_back( atan2( vi->v_compdata[i].cx_imag, vi->v_compdata[i].cx_real ) );
271 }
272 }
273
274 return data;
275}
276
277
278bool NGSPICE::Attach( const std::shared_ptr<SIMULATION_MODEL>& aModel, const wxString& aSimCommand,
279 unsigned aSimOptions, const wxString& aInputPath, REPORTER& aReporter )
280{
281 SPICE_CIRCUIT_MODEL* model = dynamic_cast<SPICE_CIRCUIT_MODEL*>( aModel.get() );
282 STRING_FORMATTER formatter;
283
284 setCodemodelsInputPath( aInputPath.ToStdString() );
285
286 if( model && model->GetNetlist( aSimCommand, aSimOptions, &formatter, aReporter ) )
287 {
288 SIMULATOR::Attach( aModel, aSimCommand, aSimOptions, aInputPath, aReporter );
290 LoadNetlist( formatter.GetString() );
291
293 {
294 Command( "echo Command: esave none" );
295 Command( "esave none" );
296 }
297
298 return true;
299 }
300 else
301 {
302 SIMULATOR::Attach( nullptr, wxEmptyString, 0, wxEmptyString, aReporter );
303 return false;
304 }
305}
306
307
308bool NGSPICE::LoadNetlist( const std::string& aNetlist )
309{
310 LOCALE_IO c_locale; // ngspice works correctly only with C locale
311 std::vector<char*> lines;
312 std::stringstream ss( aNetlist );
313
314 m_netlist.erase();
315
316 for( std::string line; std::getline( ss, line ); )
317 {
318 lines.push_back( strdup( line.data() ) );
319 m_netlist += line;
320 m_netlist += '\n';
321 }
322
323 lines.push_back( nullptr ); // sentinel, as requested in ngSpice_Circ description
324
325 Command( "remcirc" );
326 bool success = !m_ngSpice_Circ( lines.data() );
327
328 for( char* line : lines )
329 free( line );
330
331 return success;
332}
333
334
336{
337 LOCALE_IO toggle; // ngspice works correctly only with C locale
338 return Command( "bg_run" ); // bg_* commands execute in a separate thread
339}
340
341
343{
344 LOCALE_IO c_locale; // ngspice works correctly only with C locale
345 return Command( "bg_halt" ); // bg_* commands execute in a separate thread
346}
347
348
350{
351 // No need to use C locale here
352 return m_ngSpice_Running();
353}
354
355
356bool NGSPICE::Command( const std::string& aCmd )
357{
358 LOCALE_IO c_locale; // ngspice works correctly only with C locale
359 validate();
360 return !m_ngSpice_Command( (char*) aCmd.c_str() );
361}
362
363
364wxString NGSPICE::GetXAxis( SIM_TYPE aType ) const
365{
366 switch( aType )
367 {
368 case ST_AC:
369 case ST_SP:
370 case ST_NOISE:
371 case ST_FFT:
372 return wxS( "frequency" );
373
374 case ST_DC:
375 // find plot, which ends with "-sweep"
376 for( wxString vector : AllVectors() )
377 {
378 if( vector.Lower().EndsWith( wxS( "-sweep" ) ) )
379 return vector;
380 }
381
382 return wxS( "sweep" );
383
384 case ST_TRAN:
385 return wxS( "time" );
386
387 default:
388 return wxEmptyString;
389 }
390}
391
392
393std::vector<std::string> NGSPICE::GetSettingCommands() const
394{
395 const NGSPICE_SETTINGS* settings = dynamic_cast<const NGSPICE_SETTINGS*>( Settings().get() );
396
397 std::vector<std::string> commands;
398
399 wxCHECK( settings, commands );
400
401 switch( settings->GetCompatibilityMode() )
402 {
403 case NGSPICE_COMPATIBILITY_MODE::USER_CONFIG: break;
404 case NGSPICE_COMPATIBILITY_MODE::NGSPICE: commands.emplace_back( "unset ngbehavior" ); break;
405 case NGSPICE_COMPATIBILITY_MODE::PSPICE: commands.emplace_back( "set ngbehavior=psa" ); break;
406 case NGSPICE_COMPATIBILITY_MODE::LTSPICE: commands.emplace_back( "set ngbehavior=lta" ); break;
407 case NGSPICE_COMPATIBILITY_MODE::LT_PSPICE: commands.emplace_back( "set ngbehavior=ltpsa" ); break;
408 case NGSPICE_COMPATIBILITY_MODE::HSPICE: commands.emplace_back( "set ngbehavior=hsa" ); break;
409 default: wxFAIL_MSG( wxString::Format( "Undefined NGSPICE_COMPATIBILITY_MODE %d.",
410 settings->GetCompatibilityMode() ) ); break;
411 }
412
413 return commands;
414}
415
416
417const std::string NGSPICE::GetNetlist() const
418{
419 return m_netlist;
420}
421
422
424{
425 if( m_initialized )
426 return;
427
428 LOCALE_IO c_locale; // ngspice works correctly only with C locale
429 const wxStandardPaths& stdPaths = wxStandardPaths::Get();
430
431 if( m_dll.IsLoaded() ) // enable force reload
432 m_dll.Unload();
433
434 // Extra effort to find libngspice
435 // @todo Shouldn't we be using the normal KiCad path searching mechanism here?
436 wxFileName dllFile( "", NGSPICE_DLL_FILE );
437#if defined(__WINDOWS__)
438 #if defined( _MSC_VER )
439 const std::vector<std::string> dllPaths = { "" };
440 #else
441 const std::vector<std::string> dllPaths = { "", "/mingw64/bin", "/mingw32/bin" };
442 #endif
443#elif defined(__WXMAC__)
444 const std::vector<std::string> dllPaths = {
445 PATHS::GetOSXKicadUserDataDir().ToStdString() + "/PlugIns/ngspice",
446 PATHS::GetOSXKicadMachineDataDir().ToStdString() + "/PlugIns/ngspice",
447
448 // when running kicad.app
449 stdPaths.GetPluginsDir().ToStdString() + "/sim",
450
451 // when running eeschema.app
452 wxFileName( stdPaths.GetExecutablePath() ).GetPath().ToStdString() +
453 "/../../../../../Contents/PlugIns/sim"
454 };
455#else // Unix systems
456 const std::vector<std::string> dllPaths = { "/usr/local/lib" };
457#endif
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( "Missing ngspice shared library" );
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:49
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:356
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:349
std::vector< double > GetImaginaryVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with magnitude values.
Definition: ngspice.cpp:193
virtual const std::string GetNetlist() const override final
Cleans simulation data (i.e.
Definition: ngspice.cpp:417
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:364
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:248
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:423
bool loadSpinit(const std::string &aFileName)
Definition: ngspice.cpp:597
bool Run() override final
Halt the simulation.
Definition: ngspice.cpp:335
bool LoadNetlist(const std::string &aNetlist) override final
Execute the simulation with currently loaded netlist.
Definition: ngspice.cpp:308
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:218
bool Stop() override final
Check if simulation is running at the moment.
Definition: ngspice.cpp:342
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:393
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:278
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:72
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
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:436
const std::string & GetString()
Definition: richio.h:459
The common library.
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