KiCad PCB EDA Suite
NGSPICE Class Reference

#include <ngspice.h>

Inheritance diagram for NGSPICE:
SPICE_SIMULATOR

Public Member Functions

 NGSPICE ()
 
virtual ~NGSPICE ()
 
void Init () override
 

More...
 
bool LoadNetlist (const std::string &aNetlist) override
 

More...
 
bool Run () override
 

More...
 
bool Stop () override
 

More...
 
bool IsRunning () override
 

More...
 
bool Command (const std::string &aCmd) override
 

More...
 
std::string GetXAxis (SIM_TYPE aType) const override
 

More...
 
std::vector< COMPLEXGetPlot (const std::string &aName, int aMaxLen=-1) override
 

More...
 
std::vector< double > GetRealPlot (const std::string &aName, int aMaxLen=-1) override
 

More...
 
std::vector< double > GetImagPlot (const std::string &aName, int aMaxLen=-1) override
 

More...
 
std::vector< double > GetMagPlot (const std::string &aName, int aMaxLen=-1) override
 

More...
 
std::vector< double > GetPhasePlot (const std::string &aName, int aMaxLen=-1) override
 

More...
 
virtual const std::string GetNetlist () const override
 

More...
 
virtual void SetReporter (SPICE_REPORTER *aReporter)
 

Sets a SPICE_REPORTER object to receive the simulation log.

More...
 

Static Public Member Functions

static std::shared_ptr< SPICE_SIMULATORCreateInstance (const std::string &aName)
 

Creates a simulator instance of particular type (currently only ngspice is handled)

More...
 

Protected Attributes

SPICE_REPORTERm_reporter
 

Reporter object to receive simulation log

More...
 

Private Types

typedef void(* ngSpice_Init) (SendChar *, SendStat *, ControlledExit *, SendData *, SendInitData *, BGThreadRunning *, void *)
 
typedef int(* ngSpice_Circ) (char **circarray)
 
typedef int(* ngSpice_Command) (char *command)
 
typedef pvector_info(* ngGet_Vec_Info) (char *vecname)
 
typedef char **(* ngSpice_AllPlots) (void)
 
typedef char **(* ngSpice_AllVecs) (char *plotname)
 
typedef bool(* ngSpice_Running) (void)
 

Private Member Functions

void init ()
 
void init_dll ()
 
bool loadSpinit (const std::string &aFileName)
 

Executes commands from a file

More...
 
std::string findCmPath () const
 

Checks a few different locations for codemodel files and returns one if it exists

More...
 
bool loadCodemodels (const std::string &aPath)
 

Loads codemodel files from a directory

More...
 
void validate ()
 

Static Private Member Functions

static int cbSendChar (char *what, int id, void *user)
 
static int cbSendStat (char *what, int id, void *user)
 
static int cbBGThreadRunning (bool is_running, int id, void *user)
 
static int cbControlledExit (int status, bool immediate, bool exit_upon_quit, int id, void *user)
 

Private Attributes

ngSpice_Init m_ngSpice_Init
 

Handles to DLL functions

More...
 
ngSpice_Circ m_ngSpice_Circ
 
ngSpice_Command m_ngSpice_Command
 
ngGet_Vec_Info m_ngGet_Vec_Info
 
ngSpice_AllPlots m_ngSpice_AllPlots
 
ngSpice_AllVecs m_ngSpice_AllVecs
 
ngSpice_Running m_ngSpice_Running
 
wxDynamicLibrary m_dll
 
bool m_error
 

Error flag indicating that ngspice needs to be reloaded

More...
 
std::string m_netlist
 

current netlist

More...
 

Static Private Attributes

static bool m_initialized = false
 

NGspice should be initialized only once

More...
 

Detailed Description

Definition at line 35 of file ngspice.h.

Member Typedef Documentation

◆ ngGet_Vec_Info

typedef pvector_info(* NGSPICE::ngGet_Vec_Info) (char *vecname)
private

Definition at line 91 of file ngspice.h.

◆ ngSpice_AllPlots

typedef char**(* NGSPICE::ngSpice_AllPlots) (void)
private

Definition at line 92 of file ngspice.h.

◆ ngSpice_AllVecs

typedef char**(* NGSPICE::ngSpice_AllVecs) (char *plotname)
private

Definition at line 93 of file ngspice.h.

◆ ngSpice_Circ

typedef int(* NGSPICE::ngSpice_Circ) (char **circarray)
private

Definition at line 89 of file ngspice.h.

◆ ngSpice_Command

typedef int(* NGSPICE::ngSpice_Command) (char *command)
private

Definition at line 90 of file ngspice.h.

◆ ngSpice_Init

typedef void(* NGSPICE::ngSpice_Init) (SendChar *, SendStat *, ControlledExit *, SendData *, SendInitData *, BGThreadRunning *, void *)
private

Definition at line 87 of file ngspice.h.

◆ ngSpice_Running

typedef bool(* NGSPICE::ngSpice_Running) (void)
private

Definition at line 94 of file ngspice.h.

Constructor & Destructor Documentation

◆ NGSPICE()

NGSPICE::NGSPICE ( )

Definition at line 43 of file ngspice.cpp.

43  :
44  m_error( false )
45 {
46  init_dll();
47 }
bool m_error
Error flag indicating that ngspice needs to be reloaded
Definition: ngspice.h:127
void init_dll()
Definition: ngspice.cpp:279

References init_dll().

◆ ~NGSPICE()

NGSPICE::~NGSPICE ( )
virtual

Definition at line 50 of file ngspice.cpp.

51 {
52 }

Member Function Documentation

◆ cbBGThreadRunning()

int NGSPICE::cbBGThreadRunning ( bool  is_running,
int  id,
void *  user 
)
staticprivate

Definition at line 509 of file ngspice.cpp.

510 {
511  NGSPICE* sim = reinterpret_cast<NGSPICE*>( user );
512 
513  if( sim->m_reporter )
514  // I know the test below seems like an error, but well, it works somehow..
515  sim->m_reporter->OnSimStateChange( sim, is_running ? SIM_IDLE : SIM_RUNNING );
516 
517  return 0;
518 }
SPICE_REPORTER * m_reporter
Reporter object to receive simulation log
virtual void OnSimStateChange(SPICE_SIMULATOR *aObject, SIM_STATE aNewState)=0

References SPICE_SIMULATOR::m_reporter, SPICE_REPORTER::OnSimStateChange(), SIM_IDLE, and SIM_RUNNING.

Referenced by init_dll().

◆ cbControlledExit()

int NGSPICE::cbControlledExit ( int  status,
bool  immediate,
bool  exit_upon_quit,
int  id,
void *  user 
)
staticprivate

Definition at line 521 of file ngspice.cpp.

522 {
523  // Something went wrong, reload the dll
524  NGSPICE* sim = reinterpret_cast<NGSPICE*>( user );
525  sim->m_error = true;
526 
527  return 0;
528 }
bool m_error
Error flag indicating that ngspice needs to be reloaded
Definition: ngspice.h:127

References m_error.

Referenced by init_dll().

◆ cbSendChar()

int NGSPICE::cbSendChar ( char *  what,
int  id,
void *  user 
)
staticprivate

Definition at line 481 of file ngspice.cpp.

482 {
483  NGSPICE* sim = reinterpret_cast<NGSPICE*>( user );
484 
485  if( sim->m_reporter )
486  {
487  // strip stdout/stderr from the line
488  if( ( strncasecmp( what, "stdout ", 7 ) == 0 )
489  || ( strncasecmp( what, "stderr ", 7 ) == 0 ) )
490  what += 7;
491 
492  sim->m_reporter->Report( what );
493  }
494 
495  return 0;
496 }
SPICE_REPORTER * m_reporter
Reporter object to receive simulation log
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.

References SPICE_SIMULATOR::m_reporter, and REPORTER::Report().

Referenced by init_dll().

◆ cbSendStat()

int NGSPICE::cbSendStat ( char *  what,
int  id,
void *  user 
)
staticprivate

Definition at line 499 of file ngspice.cpp.

500 {
501 /* NGSPICE* sim = reinterpret_cast<NGSPICE*>( user );
502  if( sim->m_consoleReporter )
503  sim->m_consoleReporter->Report( what );*/
504 
505  return 0;
506 }

Referenced by init_dll().

◆ Command()

bool NGSPICE::Command ( const std::string &  aCmd)
overridevirtual

Implements SPICE_SIMULATOR.

Definition at line 245 of file ngspice.cpp.

246 {
247  LOCALE_IO c_locale; // ngspice works correctly only with C locale
248  validate();
249  m_ngSpice_Command( (char*) aCmd.c_str() );
250  return true;
251 }
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:175
void validate()
Definition: ngspice.cpp:531
ngSpice_Command m_ngSpice_Command
Definition: ngspice.h:99

References m_ngSpice_Command, and validate().

Referenced by Init(), init_dll(), loadCodemodels(), loadSpinit(), Run(), and Stop().

◆ CreateInstance()

std::shared_ptr< SPICE_SIMULATOR > SPICE_SIMULATOR::CreateInstance ( const std::string &  aName)
staticinherited

Creates a simulator instance of particular type (currently only ngspice is handled)

Definition at line 29 of file spice_simulator.cpp.

30 {
31  try
32  {
33  static std::shared_ptr<SPICE_SIMULATOR> ngspiceInstance;
34 
35  if( !ngspiceInstance )
36  ngspiceInstance.reset( new NGSPICE );
37 
38  return ngspiceInstance;
39  }
40  catch( std::exception& e )
41  {
42  DisplayError( NULL, e.what() );
43  }
44 
45  return NULL;
46 }
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:243

References DisplayError().

Referenced by SIM_PLOT_FRAME::SIM_PLOT_FRAME().

◆ findCmPath()

string NGSPICE::findCmPath ( ) const
private

Checks a few different locations for codemodel files and returns one if it exists

Definition at line 438 of file ngspice.cpp.

439 {
440  const vector<string> cmPaths =
441  {
442 #ifdef __WXMAC__
443  "/Applications/ngspice/lib/ngspice",
444  "Contents/Frameworks",
445  wxStandardPaths::Get().GetPluginsDir() + "/sim/ngspice",
446  wxFileName( wxStandardPaths::Get().GetExecutablePath() ).GetPath() + "/../../../../../Contents/PlugIns/sim/ngspice"
447 #endif /* __WXMAC__ */
448  "../lib/ngspice",
449  "../../lib/ngspice"
450  "lib/ngspice",
451  "ngspice"
452  };
453 
454  for( const auto& path : cmPaths )
455  {
456  wxLogTrace( traceNgspice, "ngspice code models search path: %s", path );
457 
458  if( wxFileName::FileExists( path + "/spice2poly.cm" ) )
459  {
460  wxLogTrace( traceNgspice, "ngspice code models found in: %s", path );
461  return path;
462  }
463  }
464 
465  return string();
466 }
static const wxChar *const traceNgspice
Definition: ngspice.cpp:41

References traceNgspice.

Referenced by init_dll().

◆ GetImagPlot()

vector< double > NGSPICE::GetImagPlot ( const std::string &  aName,
int  aMaxLen = -1 
)
overridevirtual

Implements SPICE_SIMULATOR.

Definition at line 120 of file ngspice.cpp.

121 {
122  LOCALE_IO c_locale; // ngspice works correctly only with C locale
123  vector<double> data;
124  vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() );
125 
126  if( vi )
127  {
128  int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
129  data.reserve( length );
130 
131  if( vi->v_compdata )
132  {
133  for( int i = 0; i < length; i++ )
134  {
135  data.push_back( vi->v_compdata[i].cx_imag );
136  }
137  }
138  }
139 
140  return data;
141 }
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:175
ngGet_Vec_Info m_ngGet_Vec_Info
Definition: ngspice.h:100
size_t i
Definition: json11.cpp:597
#define min(a, b)
Definition: auxiliary.h:85

References i, m_ngGet_Vec_Info, and min.

◆ GetMagPlot()

vector< double > NGSPICE::GetMagPlot ( const std::string &  aName,
int  aMaxLen = -1 
)
overridevirtual

Implements SPICE_SIMULATOR.

Definition at line 144 of file ngspice.cpp.

145 {
146  LOCALE_IO c_locale; // ngspice works correctly only with C locale
147  vector<double> data;
148  vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() );
149 
150  if( vi )
151  {
152  int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
153  data.reserve( length );
154 
155  if( vi->v_realdata )
156  {
157  for( int i = 0; i < length; i++ )
158  data.push_back( vi->v_realdata[i] );
159  }
160  else if( vi->v_compdata )
161  {
162  for( int i = 0; i < length; i++ )
163  data.push_back( hypot( vi->v_compdata[i].cx_real, vi->v_compdata[i].cx_imag ) );
164  }
165  }
166 
167  return data;
168 }
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:175
ngGet_Vec_Info m_ngGet_Vec_Info
Definition: ngspice.h:100
size_t i
Definition: json11.cpp:597
#define min(a, b)
Definition: auxiliary.h:85

References i, m_ngGet_Vec_Info, and min.

◆ GetNetlist()

const std::string NGSPICE::GetNetlist ( ) const
overridevirtual

Implements SPICE_SIMULATOR.

Definition at line 541 of file ngspice.cpp.

542 {
543  return m_netlist;
544 }
std::string m_netlist
current netlist
Definition: ngspice.h:133

References m_netlist.

◆ GetPhasePlot()

vector< double > NGSPICE::GetPhasePlot ( const std::string &  aName,
int  aMaxLen = -1 
)
overridevirtual

Implements SPICE_SIMULATOR.

Definition at line 171 of file ngspice.cpp.

172 {
173  LOCALE_IO c_locale; // ngspice works correctly only with C locale
174  vector<double> data;
175  vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() );
176 
177  if( vi )
178  {
179  int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
180  data.reserve( length );
181 
182  if( vi->v_realdata )
183  {
184  for( int i = 0; i < length; i++ )
185  data.push_back( 0.0 ); // well, that's life
186  }
187  else if( vi->v_compdata )
188  {
189  for( int i = 0; i < length; i++ )
190  data.push_back( atan2( vi->v_compdata[i].cx_imag, vi->v_compdata[i].cx_real ) );
191  }
192  }
193 
194  return data;
195 }
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:175
ngGet_Vec_Info m_ngGet_Vec_Info
Definition: ngspice.h:100
size_t i
Definition: json11.cpp:597
#define min(a, b)
Definition: auxiliary.h:85

References i, m_ngGet_Vec_Info, and min.

◆ GetPlot()

vector< COMPLEX > NGSPICE::GetPlot ( const std::string &  aName,
int  aMaxLen = -1 
)
overridevirtual

Implements SPICE_SIMULATOR.

Definition at line 61 of file ngspice.cpp.

62 {
63  LOCALE_IO c_locale; // ngspice works correctly only with C locale
64  vector<COMPLEX> data;
65  vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() );
66 
67  if( vi )
68  {
69  int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
70  data.reserve( length );
71 
72  if( vi->v_realdata )
73  {
74  for( int i = 0; i < length; i++ )
75  data.push_back( COMPLEX( vi->v_realdata[i], 0.0 ) );
76  }
77  else if( vi->v_compdata )
78  {
79  for( int i = 0; i < length; i++ )
80  data.push_back( COMPLEX( vi->v_compdata[i].cx_real, vi->v_compdata[i].cx_imag ) );
81  }
82  }
83 
84  return data;
85 }
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:175
std::complex< double > COMPLEX
ngGet_Vec_Info m_ngGet_Vec_Info
Definition: ngspice.h:100
size_t i
Definition: json11.cpp:597
#define min(a, b)
Definition: auxiliary.h:85

References i, m_ngGet_Vec_Info, and min.

◆ GetRealPlot()

vector< double > NGSPICE::GetRealPlot ( const std::string &  aName,
int  aMaxLen = -1 
)
overridevirtual

Implements SPICE_SIMULATOR.

Definition at line 88 of file ngspice.cpp.

89 {
90  LOCALE_IO c_locale; // ngspice works correctly only with C locale
91  vector<double> data;
92  vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() );
93 
94  if( vi )
95  {
96  int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
97  data.reserve( length );
98 
99  if( vi->v_realdata )
100  {
101  for( int i = 0; i < length; i++ )
102  {
103  data.push_back( vi->v_realdata[i] );
104  }
105  }
106  else if( vi->v_compdata )
107  {
108  for( int i = 0; i < length; i++ )
109  {
110  assert( vi->v_compdata[i].cx_imag == 0.0 );
111  data.push_back( vi->v_compdata[i].cx_real );
112  }
113  }
114  }
115 
116  return data;
117 }
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:175
ngGet_Vec_Info m_ngGet_Vec_Info
Definition: ngspice.h:100
size_t i
Definition: json11.cpp:597
#define min(a, b)
Definition: auxiliary.h:85

References i, m_ngGet_Vec_Info, and min.

◆ GetXAxis()

string NGSPICE::GetXAxis ( SIM_TYPE  aType) const
overridevirtual

Implements SPICE_SIMULATOR.

Definition at line 254 of file ngspice.cpp.

255 {
256  switch( aType )
257  {
258  case ST_AC:
259  case ST_NOISE:
260  return string( "frequency" );
261  break;
262 
263  case ST_DC:
264  return string( "v-sweep" );
265  break;
266 
267  case ST_TRANSIENT:
268  return string( "time" );
269  break;
270 
271  default:
272  break;
273  }
274 
275  return string( "" );
276 }

References ST_AC, ST_DC, ST_NOISE, and ST_TRANSIENT.

◆ Init()

void NGSPICE::Init ( )
overridevirtual

Intializes the simulator

Implements SPICE_SIMULATOR.

Definition at line 55 of file ngspice.cpp.

56 {
57  Command( "reset" );
58 }
bool Command(const std::string &aCmd) override
Definition: ngspice.cpp:245

References Command().

◆ init()

void NGSPICE::init ( )
private

◆ init_dll()

void NGSPICE::init_dll ( )
private

Definition at line 279 of file ngspice.cpp.

280 {
281  if( m_initialized )
282  return;
283 
284  LOCALE_IO c_locale; // ngspice works correctly only with C locale
285  const wxStandardPaths& stdPaths = wxStandardPaths::Get();
286 
287  if( m_dll.IsLoaded() ) // enable force reload
288  m_dll.Unload();
289 
290 // Extra effort to find libngspice
291  wxFileName dllFile( "", NGSPICE_DLL_FILE );
292 #if defined(__WINDOWS__)
293  const vector<string> dllPaths = { "", "/mingw64/bin", "/mingw32/bin" };
294 #elif defined(__WXMAC__)
295  const vector<string> dllPaths = {
296  GetOSXKicadUserDataDir() + "/PlugIns/ngspice",
297  GetOSXKicadMachineDataDir() + "/PlugIns/ngspice",
298  // when running kicad.app
299  stdPaths.GetPluginsDir() + "/sim",
300  // when running eeschema.app
301  wxFileName( stdPaths.GetExecutablePath() ).GetPath() + "/../../../../../Contents/PlugIns/sim"
302  };
303 #else // Unix systems
304  const vector<string> dllPaths = { "/usr/local/lib" };
305 #endif
306 
307 #if defined(__WINDOWS__) || (__WXMAC__)
308  for( const auto& path : dllPaths )
309  {
310  dllFile.SetPath( path );
311  wxLogTrace( traceNgspice, "libngspice search path: %s", dllFile.GetFullPath() );
312  m_dll.Load( dllFile.GetFullPath(), wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
313 
314  if( m_dll.IsLoaded() )
315  {
316  wxLogTrace( traceNgspice, "libngspice path found in: %s", dllFile.GetFullPath() );
317  break;
318  }
319  }
320 
321  if( !m_dll.IsLoaded() ) // try also the system libraries
322  m_dll.Load( wxDynamicLibrary::CanonicalizeName( "ngspice" ) );
323 
324 #else // here: __LINUX__
325  // First, try the system libraries
326  m_dll.Load( NGSPICE_DLL_FILE, wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
327 
328  // If failed, try some other paths:
329  if( !m_dll.IsLoaded() )
330  {
331  for( const auto& path : dllPaths )
332  {
333  dllFile.SetPath( path );
334  wxLogTrace( traceNgspice, "libngspice search path: %s", dllFile.GetFullPath() );
335  m_dll.Load( dllFile.GetFullPath(), wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
336 
337  if( m_dll.IsLoaded() )
338  {
339  wxLogTrace( traceNgspice, "libngspice path found in: %s", dllFile.GetFullPath() );
340  break;
341  }
342  }
343  }
344 #endif
345 
346  if( !m_dll.IsLoaded() )
347  throw std::runtime_error( "Missing ngspice shared library" );
348 
349  m_error = false;
350 
351  // Obtain function pointers
352  m_ngSpice_Init = (ngSpice_Init) m_dll.GetSymbol( "ngSpice_Init" );
353  m_ngSpice_Circ = (ngSpice_Circ) m_dll.GetSymbol( "ngSpice_Circ" );
354  m_ngSpice_Command = (ngSpice_Command) m_dll.GetSymbol( "ngSpice_Command" );
355  m_ngGet_Vec_Info = (ngGet_Vec_Info) m_dll.GetSymbol( "ngGet_Vec_Info" );
356  m_ngSpice_AllPlots = (ngSpice_AllPlots) m_dll.GetSymbol( "ngSpice_AllPlots" );
357  m_ngSpice_AllVecs = (ngSpice_AllVecs) m_dll.GetSymbol( "ngSpice_AllVecs" );
358  m_ngSpice_Running = (ngSpice_Running) m_dll.GetSymbol( "ngSpice_running" ); // it is not a typo
359 
361 
362  // Load a custom spinit file, to fix the problem with loading .cm files
363  // Switch to the executable directory, so the relative paths are correct
364  wxString cwd( wxGetCwd() );
365  wxFileName exeDir( stdPaths.GetExecutablePath() );
366  wxSetWorkingDirectory( exeDir.GetPath() );
367 
368  // Find *.cm files
369  string cmPath = findCmPath();
370 
371  // __CMPATH is used in custom spinit file to point to the codemodels directory
372  if( !cmPath.empty() )
373  Command( "set __CMPATH=\"" + cmPath + "\"" );
374 
375  // Possible relative locations for spinit file
376  const vector<string> spiceinitPaths =
377  {
378  ".",
379 #ifdef __WXMAC__
380  stdPaths.GetPluginsDir() + "/sim/ngspice/scripts",
381  wxFileName( stdPaths.GetExecutablePath() ).GetPath() + "/../../../../../Contents/PlugIns/sim/ngspice/scripts"
382 #endif /* __WXMAC__ */
383  "../share/kicad",
384  "../share",
385  "../../share/kicad",
386  "../../share"
387  };
388 
389  bool foundSpiceinit = false;
390 
391  for( const auto& path : spiceinitPaths )
392  {
393  wxLogTrace( traceNgspice, "ngspice init script search path: %s", path );
394 
395  if( loadSpinit( path + "/spiceinit" ) )
396  {
397  wxLogTrace( traceNgspice, "ngspice path found in: %s", path );
398  foundSpiceinit = true;
399  break;
400  }
401  }
402 
403  // Last chance to load codemodel files, we have not found
404  // spiceinit file, but we know the path to *.cm files
405  if( !foundSpiceinit && !cmPath.empty() )
406  loadCodemodels( cmPath );
407 
408  // Restore the working directory
409  wxSetWorkingDirectory( cwd );
410 
411  // Workarounds to avoid hang ups on certain errors
412  // These commands have to be called, no matter what is in the spinit file
413  Command( "unset interactive" );
414  Command( "set noaskquit" );
415  Command( "set nomoremode" );
416 
417  m_initialized = true;
418 }
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:175
static int cbBGThreadRunning(bool is_running, int id, void *user)
Definition: ngspice.cpp:509
int(* ngSpice_Command)(char *command)
Definition: ngspice.h:90
char **(* ngSpice_AllPlots)(void)
Definition: ngspice.h:92
std::string findCmPath() const
Checks a few different locations for codemodel files and returns one if it exists
Definition: ngspice.cpp:438
static int cbControlledExit(int status, bool immediate, bool exit_upon_quit, int id, void *user)
Definition: ngspice.cpp:521
static int cbSendChar(char *what, int id, void *user)
Definition: ngspice.cpp:481
wxDynamicLibrary m_dll
Definition: ngspice.h:105
bool loadCodemodels(const std::string &aPath)
Loads codemodel files from a directory
Definition: ngspice.cpp:469
ngSpice_Running m_ngSpice_Running
Definition: ngspice.h:103
static bool m_initialized
NGspice should be initialized only once
Definition: ngspice.h:130
bool(* ngSpice_Running)(void)
Definition: ngspice.h:94
ngGet_Vec_Info m_ngGet_Vec_Info
Definition: ngspice.h:100
bool m_error
Error flag indicating that ngspice needs to be reloaded
Definition: ngspice.h:127
static const wxChar *const traceNgspice
Definition: ngspice.cpp:41
pvector_info(* ngGet_Vec_Info)(char *vecname)
Definition: ngspice.h:91
ngSpice_Init m_ngSpice_Init
Handles to DLL functions
Definition: ngspice.h:97
void(* ngSpice_Init)(SendChar *, SendStat *, ControlledExit *, SendData *, SendInitData *, BGThreadRunning *, void *)
Definition: ngspice.h:87
char **(* ngSpice_AllVecs)(char *plotname)
Definition: ngspice.h:93
bool Command(const std::string &aCmd) override
Definition: ngspice.cpp:245
ngSpice_AllVecs m_ngSpice_AllVecs
Definition: ngspice.h:102
ngSpice_Command m_ngSpice_Command
Definition: ngspice.h:99
bool loadSpinit(const std::string &aFileName)
Executes commands from a file
Definition: ngspice.cpp:421
int(* ngSpice_Circ)(char **circarray)
Definition: ngspice.h:89
ngSpice_Circ m_ngSpice_Circ
Definition: ngspice.h:98
ngSpice_AllPlots m_ngSpice_AllPlots
Definition: ngspice.h:101
static int cbSendStat(char *what, int id, void *user)
Definition: ngspice.cpp:499

References cbBGThreadRunning(), cbControlledExit(), cbSendChar(), cbSendStat(), Command(), findCmPath(), loadCodemodels(), loadSpinit(), m_dll, m_error, m_initialized, m_ngGet_Vec_Info, m_ngSpice_AllPlots, m_ngSpice_AllVecs, m_ngSpice_Circ, m_ngSpice_Command, m_ngSpice_Init, m_ngSpice_Running, and traceNgspice.

Referenced by NGSPICE(), and validate().

◆ IsRunning()

bool NGSPICE::IsRunning ( )
overridevirtual

Checks if simulation is running at the moment.

Returns
True if simulation is currently executed.

Implements SPICE_SIMULATOR.

Definition at line 238 of file ngspice.cpp.

239 {
240  LOCALE_IO c_locale; // ngspice works correctly only with C locale
241  return m_ngSpice_Running();
242 }
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:175
ngSpice_Running m_ngSpice_Running
Definition: ngspice.h:103

References m_ngSpice_Running.

◆ loadCodemodels()

bool NGSPICE::loadCodemodels ( const std::string &  aPath)
private

Loads codemodel files from a directory

Definition at line 469 of file ngspice.cpp.

470 {
471  wxArrayString cmFiles;
472  size_t count = wxDir::GetAllFiles( aPath, &cmFiles );
473 
474  for( const auto& cm : cmFiles )
475  Command( "codemodel " + cm.ToStdString() );
476 
477  return count != 0;
478 }
bool Command(const std::string &aCmd) override
Definition: ngspice.cpp:245

References Command().

Referenced by init_dll().

◆ LoadNetlist()

bool NGSPICE::LoadNetlist ( const std::string &  aNetlist)
overridevirtual

Implements SPICE_SIMULATOR.

Definition at line 198 of file ngspice.cpp.

199 {
200  LOCALE_IO c_locale; // ngspice works correctly only with C locale
201  vector<char*> lines;
202  stringstream ss( aNetlist );
203 
204  m_netlist = "";
205 
206  while( !ss.eof() )
207  {
208  char line[1024];
209  ss.getline( line, sizeof( line ) );
210  lines.push_back( strdup( line ) );
211  m_netlist += std::string( line ) + std::string( "\n" );
212  }
213 
214  lines.push_back( nullptr ); // sentinel, as requested in ngSpice_Circ description
215  m_ngSpice_Circ( lines.data() );
216 
217  for( auto line : lines )
218  free( line );
219 
220  return true;
221 }
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:175
std::string m_netlist
current netlist
Definition: ngspice.h:133
ngSpice_Circ m_ngSpice_Circ
Definition: ngspice.h:98

References m_netlist, and m_ngSpice_Circ.

◆ loadSpinit()

bool NGSPICE::loadSpinit ( const std::string &  aFileName)
private

Executes commands from a file

Definition at line 421 of file ngspice.cpp.

422 {
423  if( !wxFileName::FileExists( aFileName ) )
424  return false;
425 
426  wxTextFile file;
427 
428  if( !file.Open( aFileName ) )
429  return false;
430 
431  for( auto cmd = file.GetFirstLine(); !file.Eof(); cmd = file.GetNextLine() )
432  Command( cmd.ToStdString() );
433 
434  return true;
435 }
bool Command(const std::string &aCmd) override
Definition: ngspice.cpp:245

References Command().

Referenced by init_dll().

◆ Run()

bool NGSPICE::Run ( )
overridevirtual

Executes the simulation with currently loaded netlist.

Returns
True in case of success, false otherwise.

Implements SPICE_SIMULATOR.

Definition at line 224 of file ngspice.cpp.

225 {
226  LOCALE_IO c_locale; // ngspice works correctly only with C locale
227  return Command( "bg_run" ); // bg_* commands execute in a separate thread
228 }
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:175
bool Command(const std::string &aCmd) override
Definition: ngspice.cpp:245

References Command().

◆ SetReporter()

virtual void SPICE_SIMULATOR::SetReporter ( SPICE_REPORTER aReporter)
inlinevirtualinherited

Sets a SPICE_REPORTER object to receive the simulation log.

Definition at line 86 of file spice_simulator.h.

87  {
88  m_reporter = aReporter;
89  }
SPICE_REPORTER * m_reporter
Reporter object to receive simulation log

References SPICE_SIMULATOR::m_reporter.

◆ Stop()

bool NGSPICE::Stop ( )
overridevirtual

Halts the simulation.

Returns
True in case of success, false otherwise.

Implements SPICE_SIMULATOR.

Definition at line 231 of file ngspice.cpp.

232 {
233  LOCALE_IO c_locale; // ngspice works correctly only with C locale
234  return Command( "bg_halt" ); // bg_* commands execute in a separate thread
235 }
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:175
bool Command(const std::string &aCmd) override
Definition: ngspice.cpp:245

References Command().

◆ validate()

void NGSPICE::validate ( )
private

Definition at line 531 of file ngspice.cpp.

532 {
533  if( m_error )
534  {
535  m_initialized = false;
536  init_dll();
537  }
538 }
static bool m_initialized
NGspice should be initialized only once
Definition: ngspice.h:130
bool m_error
Error flag indicating that ngspice needs to be reloaded
Definition: ngspice.h:127
void init_dll()
Definition: ngspice.cpp:279

References init_dll(), m_error, and m_initialized.

Referenced by Command().

Member Data Documentation

◆ m_dll

wxDynamicLibrary NGSPICE::m_dll
private

Definition at line 105 of file ngspice.h.

Referenced by init_dll().

◆ m_error

bool NGSPICE::m_error
private

Error flag indicating that ngspice needs to be reloaded

Definition at line 127 of file ngspice.h.

Referenced by cbControlledExit(), init_dll(), and validate().

◆ m_initialized

bool NGSPICE::m_initialized = false
staticprivate

NGspice should be initialized only once

Definition at line 130 of file ngspice.h.

Referenced by init_dll(), and validate().

◆ m_netlist

std::string NGSPICE::m_netlist
private

current netlist

Definition at line 133 of file ngspice.h.

Referenced by GetNetlist(), and LoadNetlist().

◆ m_ngGet_Vec_Info

ngGet_Vec_Info NGSPICE::m_ngGet_Vec_Info
private

Definition at line 100 of file ngspice.h.

Referenced by GetImagPlot(), GetMagPlot(), GetPhasePlot(), GetPlot(), GetRealPlot(), and init_dll().

◆ m_ngSpice_AllPlots

ngSpice_AllPlots NGSPICE::m_ngSpice_AllPlots
private

Definition at line 101 of file ngspice.h.

Referenced by init_dll().

◆ m_ngSpice_AllVecs

ngSpice_AllVecs NGSPICE::m_ngSpice_AllVecs
private

Definition at line 102 of file ngspice.h.

Referenced by init_dll().

◆ m_ngSpice_Circ

ngSpice_Circ NGSPICE::m_ngSpice_Circ
private

Definition at line 98 of file ngspice.h.

Referenced by init_dll(), and LoadNetlist().

◆ m_ngSpice_Command

ngSpice_Command NGSPICE::m_ngSpice_Command
private

Definition at line 99 of file ngspice.h.

Referenced by Command(), and init_dll().

◆ m_ngSpice_Init

ngSpice_Init NGSPICE::m_ngSpice_Init
private

Handles to DLL functions

Definition at line 97 of file ngspice.h.

Referenced by init_dll().

◆ m_ngSpice_Running

ngSpice_Running NGSPICE::m_ngSpice_Running
private

Definition at line 103 of file ngspice.h.

Referenced by init_dll(), and IsRunning().

◆ m_reporter

SPICE_REPORTER* SPICE_SIMULATOR::m_reporter
protectedinherited

Reporter object to receive simulation log

Definition at line 147 of file spice_simulator.h.

Referenced by cbBGThreadRunning(), cbSendChar(), and SPICE_SIMULATOR::SetReporter().


The documentation for this class was generated from the following files: