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 void SetReporter (SPICE_REPORTER *aReporter)
 

Sets a SPICE_REPORTER object to receive the simulation log.

More...
 

Static Public Member Functions

static 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 Member Functions

void init ()
 
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 dump ()
 

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)
 

Static Private Attributes

static bool m_initialized = false
 

NGspice should be initialized only once

More...
 

Detailed Description

Definition at line 34 of file ngspice.h.

Constructor & Destructor Documentation

NGSPICE::NGSPICE ( )

Definition at line 37 of file ngspice.cpp.

38 {
39  init();
40 }
void init()
Definition: ngspice.cpp:270
NGSPICE::~NGSPICE ( )
virtual

Definition at line 43 of file ngspice.cpp.

44 {
45 }

Member Function Documentation

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

Definition at line 411 of file ngspice.cpp.

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

412 {
413  NGSPICE* sim = reinterpret_cast<NGSPICE*>( user );
414 
415  if( sim->m_reporter )
416  // I know the test below seems like an error, but well, it works somehow..
417  sim->m_reporter->OnSimStateChange( sim, is_running ? SIM_IDLE : SIM_RUNNING );
418 
419  return 0;
420 }
SPICE_REPORTER * m_reporter
Reporter object to receive simulation log
virtual void OnSimStateChange(SPICE_SIMULATOR *aObject, SIM_STATE aNewState)=0
int NGSPICE::cbControlledExit ( int  status,
bool  immediate,
bool  exit_upon_quit,
int  id,
void *  user 
)
staticprivate

Definition at line 423 of file ngspice.cpp.

424 {
425  // Something went wrong, reload the dll
426  //NGSPICE* sim = reinterpret_cast<NGSPICE*>( user );
427  //sim->m_initialized = false;
428  //printf("stat %d immed %d quit %d\n", status, !!immediate, !!exit_upon_quit);
429 
430  return 0;
431 }
int NGSPICE::cbSendChar ( char *  what,
int  id,
void *  user 
)
staticprivate

Definition at line 383 of file ngspice.cpp.

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

384 {
385  NGSPICE* sim = reinterpret_cast<NGSPICE*>( user );
386 
387  if( sim->m_reporter )
388  {
389  // strip stdout/stderr from the line
390  if( ( strncasecmp( what, "stdout ", 7 ) == 0 )
391  || ( strncasecmp( what, "stderr ", 7 ) == 0 ) )
392  what += 7;
393 
394  sim->m_reporter->Report( what );
395  }
396 
397  return 0;
398 }
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.
int NGSPICE::cbSendStat ( char *  what,
int  id,
void *  user 
)
staticprivate

Definition at line 401 of file ngspice.cpp.

402 {
403 /* NGSPICE* sim = reinterpret_cast<NGSPICE*>( user );
404  if( sim->m_consoleReporter )
405  sim->m_consoleReporter->Report( what );*/
406 
407  return 0;
408 }
bool NGSPICE::Command ( const std::string &  aCmd)
overridevirtual

>

Executes a Spice command as if it was typed into console.

Parameters
aCmdis the command to be issued.

Implements SPICE_SIMULATOR.

Definition at line 236 of file ngspice.cpp.

237 {
238  LOCALE_IO c_locale; // ngspice works correctly only with C locale
239  ngSpice_Command( (char*) aCmd.c_str() );
240 
241  return true;
242 }
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:166
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.

References DisplayError().

Referenced by SIM_PLOT_FRAME::SIM_PLOT_FRAME().

30 {
31  try
32  {
33  static NGSPICE* ngspiceInstance = nullptr;
34 
35  if( !ngspiceInstance )
36  ngspiceInstance = 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)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:71
void NGSPICE::dump ( )
private
string NGSPICE::findCmPath ( ) const
private

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

Definition at line 348 of file ngspice.cpp.

349 {
350  const vector<string> cmPaths =
351  {
352 #ifdef __APPLE__
353  "/Applications/ngspice/lib/ngspice",
354 #endif /* __APPLE__ */
355  "../lib/ngspice",
356  "../../lib/ngspice"
357  "lib/ngspice",
358  "ngspice"
359  };
360 
361  for( const auto& path : cmPaths )
362  {
363  if( wxFileName::DirExists( path ) )
364  return path;
365  }
366 
367  return string();
368 }
vector< double > NGSPICE::GetImagPlot ( const std::string &  aName,
int  aMaxLen = -1 
)
overridevirtual

>

Returns a requested vector with imaginary values. If the vector is complex, then the imaginary part is returned. If the vector is reql, then only zeroes are returned.

Parameters
aNameis the vector named in Spice convention (e.g. V(3), I(R1)).
aMaxLenis max count of returned values. if -1 (default) all available values are returned.
Returns
Requested vector. It might be empty if there is no vector with requested name.

Implements SPICE_SIMULATOR.

Definition at line 113 of file ngspice.cpp.

References min.

114 {
115  LOCALE_IO c_locale; // ngspice works correctly only with C locale
116  vector<double> data;
117  vector_info* vi = ngGet_Vec_Info( (char*) aName.c_str() );
118 
119  if( vi )
120  {
121  int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
122  data.reserve( length );
123 
124  if( vi->v_compdata )
125  {
126  for( int i = 0; i < length; i++ )
127  {
128  data.push_back( vi->v_compdata[i].cx_imag );
129  }
130  }
131  }
132 
133  return data;
134 }
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:166
#define min(a, b)
Definition: auxiliary.h:85
vector< double > NGSPICE::GetMagPlot ( const std::string &  aName,
int  aMaxLen = -1 
)
overridevirtual

>

Returns a requested vector with magnitude values.

Parameters
aNameis the vector named in Spice convention (e.g. V(3), I(R1)).
aMaxLenis max count of returned values. if -1 (default) all available values are returned.
Returns
Requested vector. It might be empty if there is no vector with requested name.

Implements SPICE_SIMULATOR.

Definition at line 137 of file ngspice.cpp.

References min.

138 {
139  LOCALE_IO c_locale; // ngspice works correctly only with C locale
140  vector<double> data;
141  vector_info* vi = ngGet_Vec_Info( (char*) aName.c_str() );
142 
143  if( vi )
144  {
145  int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
146  data.reserve( length );
147 
148  if( vi->v_realdata )
149  {
150  for( int i = 0; i < length; i++ )
151  data.push_back( vi->v_realdata[i] );
152  }
153  else if( vi->v_compdata )
154  {
155  for( int i = 0; i < length; i++ )
156  data.push_back( hypot( vi->v_compdata[i].cx_real, vi->v_compdata[i].cx_imag ) );
157  }
158  }
159 
160  return data;
161 }
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:166
#define min(a, b)
Definition: auxiliary.h:85
vector< double > NGSPICE::GetPhasePlot ( const std::string &  aName,
int  aMaxLen = -1 
)
overridevirtual

>

Returns a requested vector with phase values.

Parameters
aNameis the vector named in Spice convention (e.g. V(3), I(R1)).
aMaxLenis max count of returned values. if -1 (default) all available values are returned.
Returns
Requested vector. It might be empty if there is no vector with requested name.

Implements SPICE_SIMULATOR.

Definition at line 164 of file ngspice.cpp.

References min.

165 {
166  LOCALE_IO c_locale; // ngspice works correctly only with C locale
167  vector<double> data;
168  vector_info* vi = ngGet_Vec_Info( (char*) aName.c_str() );
169 
170  if( vi )
171  {
172  int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
173  data.reserve( length );
174 
175  if( vi->v_realdata )
176  {
177  for( int i = 0; i < length; i++ )
178  data.push_back( 0.0 ); // well, that's life
179  }
180  else if( vi->v_compdata )
181  {
182  for( int i = 0; i < length; i++ )
183  data.push_back( atan2( vi->v_compdata[i].cx_imag, vi->v_compdata[i].cx_real ) );
184  }
185  }
186 
187  return data;
188 }
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:166
#define min(a, b)
Definition: auxiliary.h:85
vector< COMPLEX > NGSPICE::GetPlot ( const std::string &  aName,
int  aMaxLen = -1 
)
overridevirtual

>

Returns a requested vector with complex values. If the vector is real, then the imaginary part is set to 0 in all values.

Parameters
aNameis the vector named in Spice convention (e.g. V(3), I(R1)).
aMaxLenis max count of returned values. if -1 (default) all available values are returned.
Returns
Requested vector. It might be empty if there is no vector with requested name.

Implements SPICE_SIMULATOR.

Definition at line 54 of file ngspice.cpp.

References min.

55 {
56  LOCALE_IO c_locale; // ngspice works correctly only with C locale
57  vector<COMPLEX> data;
58  vector_info* vi = ngGet_Vec_Info( (char*) aName.c_str() );
59 
60  if( vi )
61  {
62  int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
63  data.reserve( length );
64 
65  if( vi->v_realdata )
66  {
67  for( int i = 0; i < length; i++ )
68  data.push_back( COMPLEX( vi->v_realdata[i], 0.0 ) );
69  }
70  else if( vi->v_compdata )
71  {
72  for( int i = 0; i < length; i++ )
73  data.push_back( COMPLEX( vi->v_compdata[i].cx_real, vi->v_compdata[i].cx_imag ) );
74  }
75  }
76 
77  return data;
78 }
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:166
std::complex< double > COMPLEX
#define min(a, b)
Definition: auxiliary.h:85
vector< double > NGSPICE::GetRealPlot ( const std::string &  aName,
int  aMaxLen = -1 
)
overridevirtual

>

Returns a requested vector with real values. If the vector is complex, then the real part is returned.

Parameters
aNameis the vector named in Spice convention (e.g. V(3), I(R1)).
aMaxLenis max count of returned values. if -1 (default) all available values are returned.
Returns
Requested vector. It might be empty if there is no vector with requested name.

Implements SPICE_SIMULATOR.

Definition at line 81 of file ngspice.cpp.

References min.

82 {
83  LOCALE_IO c_locale; // ngspice works correctly only with C locale
84  vector<double> data;
85  vector_info* vi = ngGet_Vec_Info( (char*) aName.c_str() );
86 
87  if( vi )
88  {
89  int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
90  data.reserve( length );
91 
92  if( vi->v_realdata )
93  {
94  for( int i = 0; i < length; i++ )
95  {
96  data.push_back( vi->v_realdata[i] );
97  }
98  }
99  else if( vi->v_compdata )
100  {
101  for( int i = 0; i < length; i++ )
102  {
103  assert( vi->v_compdata[i].cx_imag == 0.0 );
104  data.push_back( vi->v_compdata[i].cx_real );
105  }
106  }
107  }
108 
109  return data;
110 }
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:166
#define min(a, b)
Definition: auxiliary.h:85
string NGSPICE::GetXAxis ( SIM_TYPE  aType) const
overridevirtual

>

Returns X axis name for a given simulation type

Implements SPICE_SIMULATOR.

Definition at line 245 of file ngspice.cpp.

References ST_AC, ST_DC, ST_NOISE, and ST_TRANSIENT.

246 {
247  switch( aType )
248  {
249  case ST_AC:
250  case ST_NOISE:
251  return string( "frequency" );
252  break;
253 
254  case ST_DC:
255  return string( "v-sweep" );
256  break;
257 
258  case ST_TRANSIENT:
259  return string( "time" );
260  break;
261 
262  default:
263  break;
264  }
265 
266  return string( "" );
267 }
void NGSPICE::Init ( )
overridevirtual

>

Intializes the simulator

Implements SPICE_SIMULATOR.

Definition at line 48 of file ngspice.cpp.

49 {
50  Command( "reset" );
51 }
bool Command(const std::string &aCmd) override
>
Definition: ngspice.cpp:236
void NGSPICE::init ( )
private

Definition at line 270 of file ngspice.cpp.

271 {
272  if( m_initialized )
273  return;
274 
275  LOCALE_IO c_locale; // ngspice works correctly only with C locale
276  ngSpice_Init( &cbSendChar, &cbSendStat, &cbControlledExit, NULL, NULL, &cbBGThreadRunning, this );
277 
278  // Load a custom spinit file, to fix the problem with loading .cm files
279  // Switch to the executable directory, so the relative paths are correct
280  const wxStandardPaths& paths = wxStandardPaths::Get();
281  wxString cwd( wxGetCwd() );
282  wxFileName exeDir( paths.GetExecutablePath() );
283  wxSetWorkingDirectory( exeDir.GetPath() );
284 
285  // Find *.cm files
286  string cmPath = findCmPath();
287 
288  // __CMPATH is used in custom spinit file to point to the codemodels directory
289  if( !cmPath.empty() )
290  Command( "set __CMPATH=\"" + cmPath + "\"" );
291 
292  // Possible relative locations for spinit file
293  const vector<string> spiceinitPaths =
294  {
295  ".",
296  "../share/kicad",
297  "../share",
298  "../../share/kicad",
299  "../../share"
300  };
301 
302  bool foundSpiceinit = false;
303 
304  for( const auto& path : spiceinitPaths )
305  {
306  if( loadSpinit( path + "/spiceinit" ) )
307  {
308  foundSpiceinit = true;
309  break;
310  }
311  }
312 
313  // Last chance to load codemodel files, we have not found
314  // spiceinit file, but we know the path to *.cm files
315  if( !foundSpiceinit && !cmPath.empty() )
316  loadCodemodels( cmPath );
317 
318  // Restore the working directory
319  wxSetWorkingDirectory( cwd );
320 
321  // Workarounds to avoid hang ups on certain errors,
322  // they have to be called, no matter what is in the spinit file
323  Command( "unset interactive" );
324  Command( "set noaskquit" );
325  Command( "set nomoremode" );
326 
327  m_initialized = true;
328 }
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:166
static int cbBGThreadRunning(bool is_running, int id, void *user)
Definition: ngspice.cpp:411
static int cbControlledExit(int status, bool immediate, bool exit_upon_quit, int id, void *user)
Definition: ngspice.cpp:423
static int cbSendChar(char *what, int id, void *user)
Definition: ngspice.cpp:383
bool loadCodemodels(const std::string &aPath)
Loads codemodel files from a directory
Definition: ngspice.cpp:371
static bool m_initialized
NGspice should be initialized only once
Definition: ngspice.h:98
std::string findCmPath() const
Checks a few different locations for codemodel files and returns one if it exists ...
Definition: ngspice.cpp:348
bool Command(const std::string &aCmd) override
>
Definition: ngspice.cpp:236
bool loadSpinit(const std::string &aFileName)
Executes commands from a file
Definition: ngspice.cpp:331
static int cbSendStat(char *what, int id, void *user)
Definition: ngspice.cpp:401
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 229 of file ngspice.cpp.

230 {
231  LOCALE_IO c_locale; // ngspice works correctly only with C locale
232  return ngSpice_running();
233 }
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:166
bool NGSPICE::loadCodemodels ( const std::string &  aPath)
private

Loads codemodel files from a directory

Definition at line 371 of file ngspice.cpp.

372 {
373  wxArrayString cmFiles;
374  size_t count = wxDir::GetAllFiles( aPath, &cmFiles );
375 
376  for( const auto& cm : cmFiles )
377  Command( "codemodel " + cm.ToStdString() );
378 
379  return count != 0;
380 }
bool Command(const std::string &aCmd) override
>
Definition: ngspice.cpp:236
bool NGSPICE::LoadNetlist ( const std::string &  aNetlist)
overridevirtual

>

Implements SPICE_SIMULATOR.

Definition at line 191 of file ngspice.cpp.

192 {
193  LOCALE_IO c_locale; // ngspice works correctly only with C locale
194  vector<char*> lines;
195  stringstream ss( aNetlist );
196 
197  while( !ss.eof() )
198  {
199  char line[1024];
200  ss.getline( line, sizeof( line ) );
201  lines.push_back( strdup( line ) );
202  }
203 
204  lines.push_back( nullptr );
205 
206  ngSpice_Circ( lines.data() );
207 
208  for( auto line : lines )
209  free( line );
210 
211  return true;
212 }
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:166
bool NGSPICE::loadSpinit ( const std::string &  aFileName)
private

Executes commands from a file

Definition at line 331 of file ngspice.cpp.

332 {
333  if( !wxFileName::FileExists( aFileName ) )
334  return false;
335 
336  wxTextFile file;
337 
338  if( !file.Open( aFileName ) )
339  return false;
340 
341  for( auto cmd = file.GetFirstLine(); !file.Eof(); cmd = file.GetNextLine() )
342  Command( cmd.ToStdString() );
343 
344  return true;
345 }
bool Command(const std::string &aCmd) override
>
Definition: ngspice.cpp:236
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 215 of file ngspice.cpp.

216 {
217  LOCALE_IO c_locale; // ngspice works correctly only with C locale
218  return Command( "bg_run" ); // bg_* commands execute in a separate thread
219 }
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:166
bool Command(const std::string &aCmd) override
>
Definition: ngspice.cpp:236
virtual void SPICE_SIMULATOR::SetReporter ( SPICE_REPORTER aReporter)
inlinevirtualinherited

Sets a SPICE_REPORTER object to receive the simulation log.

Definition at line 84 of file spice_simulator.h.

References SPICE_SIMULATOR::m_reporter.

Referenced by SIM_PLOT_FRAME::SIM_PLOT_FRAME(), and SIM_PLOT_FRAME::~SIM_PLOT_FRAME().

85  {
86  m_reporter = aReporter;
87  }
SPICE_REPORTER * m_reporter
Reporter object to receive simulation log
bool NGSPICE::Stop ( )
overridevirtual

>

Halts the simulation.

Returns
True in case of success, false otherwise.

Implements SPICE_SIMULATOR.

Definition at line 222 of file ngspice.cpp.

223 {
224  LOCALE_IO c_locale; // ngspice works correctly only with C locale
225  return Command( "bg_halt" ); // bg_* commands execute in a separate thread
226 }
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:166
bool Command(const std::string &aCmd) override
>
Definition: ngspice.cpp:236

Member Data Documentation

bool NGSPICE::m_initialized = false
staticprivate

NGspice should be initialized only once

Definition at line 98 of file ngspice.h.

SPICE_REPORTER* SPICE_SIMULATOR::m_reporter
protectedinherited

Reporter object to receive simulation log

Definition at line 139 of file spice_simulator.h.

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


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