39#include <wx/stdpaths.h>
58 m_ngSpice_Init( nullptr ),
59 m_ngSpice_Circ( nullptr ),
60 m_ngSpice_Command( nullptr ),
61 m_ngGet_Vec_Info( nullptr ),
62 m_ngSpice_CurPlot( nullptr ),
63 m_ngSpice_AllPlots( nullptr ),
64 m_ngSpice_AllVecs( nullptr ),
65 m_ngSpice_Running( nullptr ),
81 wxLogTrace(
traceNgspice,
"Sending Ngspice configuration command '%s'.", command );
100 std::vector<std::string> retVal;
102 if( allVectors !=
nullptr )
104 for(
char**
plot = allVectors; *
plot !=
nullptr;
plot++ )
107 retVal.reserve( noOfVectors );
109 for(
int i = 0; i < noOfVectors; i++, allVectors++ )
111 std::string vec = *allVectors;
112 retVal.push_back( vec );
124 std::vector<COMPLEX> data;
129 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
130 data.reserve( length );
134 for(
int i = 0; i < length; i++ )
135 data.emplace_back( vi->v_realdata[i], 0.0 );
137 else if( vi->v_compdata )
139 for(
int i = 0; i < length; i++ )
140 data.emplace_back( vi->v_compdata[i].cx_real, vi->v_compdata[i].cx_imag );
151 std::vector<double> data;
156 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
157 data.reserve( length );
161 for(
int i = 0; i < length; i++ )
162 data.push_back( vi->v_realdata[i] );
164 else if( vi->v_compdata )
166 for(
int i = 0; i < length; i++ )
168 wxASSERT( vi->v_compdata[i].cx_imag == 0.0 );
169 data.push_back( vi->v_compdata[i].cx_real );
181 std::vector<double> data;
186 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
187 data.reserve( length );
191 for(
int i = 0; i < length; i++ )
192 data.push_back( vi->v_compdata[i].cx_imag );
203 std::vector<double> data;
208 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
209 data.reserve( length );
213 for(
int i = 0; i < length; i++ )
214 data.push_back( vi->v_realdata[i] );
216 else if( vi->v_compdata )
218 for(
int i = 0; i < length; i++ )
219 data.push_back( hypot( vi->v_compdata[i].cx_real, vi->v_compdata[i].cx_imag ) );
230 std::vector<double> data;
235 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
236 data.reserve( length );
240 for(
int i = 0; i < length; i++ )
241 data.push_back( 0.0 );
243 else if( vi->v_compdata )
245 for(
int i = 0; i < length; i++ )
246 data.push_back( atan2( vi->v_compdata[i].cx_imag, vi->v_compdata[i].cx_real ) );
254bool NGSPICE::Attach(
const std::shared_ptr<SIMULATION_MODEL>& aModel,
const wxString& aSimCommand,
255 unsigned aSimOptions,
REPORTER& aReporter )
260 if( model && model->
GetNetlist( aSimCommand, aSimOptions, &formatter, aReporter ) )
277 std::vector<char*> lines;
278 std::stringstream ss( aNetlist );
285 ss.getline( line,
sizeof( line ) );
286 lines.push_back( strdup( line ) );
287 m_netlist += std::string( line ) + std::string(
"\n" );
290 lines.push_back(
nullptr );
295 for(
char* line : lines )
339 return wxS(
"frequency" );
345 if( vector.Lower().EndsWith( wxS(
"-sweep" ) ) )
349 return wxS(
"sweep" );
352 return wxS(
"time" );
355 return wxEmptyString;
364 std::vector<std::string> commands;
366 wxCHECK( settings, commands );
370 case NGSPICE_COMPATIBILITY_MODE::USER_CONFIG:
break;
371 case NGSPICE_COMPATIBILITY_MODE::NGSPICE: commands.emplace_back(
"unset ngbehavior" );
break;
372 case NGSPICE_COMPATIBILITY_MODE::PSPICE: commands.emplace_back(
"set ngbehavior=psa" );
break;
373 case NGSPICE_COMPATIBILITY_MODE::LTSPICE: commands.emplace_back(
"set ngbehavior=lta" );
break;
374 case NGSPICE_COMPATIBILITY_MODE::LT_PSPICE: commands.emplace_back(
"set ngbehavior=ltpsa" );
break;
375 case NGSPICE_COMPATIBILITY_MODE::HSPICE: commands.emplace_back(
"set ngbehavior=hsa" );
break;
376 default: wxFAIL_MSG( wxString::Format(
"Undefined NGSPICE_COMPATIBILITY_MODE %d.",
396 const wxStandardPaths& stdPaths = wxStandardPaths::Get();
398 if(
m_dll.IsLoaded() )
403 wxFileName dllFile(
"", NGSPICE_DLL_FILE );
404#if defined(__WINDOWS__)
405 #if defined( _MSC_VER )
406 const std::vector<std::string> dllPaths = {
"" };
408 const std::vector<std::string> dllPaths = {
"",
"/mingw64/bin",
"/mingw32/bin" };
410#elif defined(__WXMAC__)
411 const std::vector<std::string> dllPaths = {
412 PATHS::GetOSXKicadUserDataDir().ToStdString() +
"/PlugIns/ngspice",
413 PATHS::GetOSXKicadMachineDataDir().ToStdString() +
"/PlugIns/ngspice",
416 stdPaths.GetPluginsDir().ToStdString() +
"/sim",
419 wxFileName( stdPaths.GetExecutablePath() ).GetPath().ToStdString() +
420 "/../../../../../Contents/PlugIns/sim"
423 const std::vector<std::string> dllPaths = {
"/usr/local/lib" };
426#if defined(__WINDOWS__) || (__WXMAC__)
427 for(
const auto&
path : dllPaths )
429 dllFile.SetPath(
path );
430 wxLogTrace(
traceNgspice,
"libngspice search path: %s", dllFile.GetFullPath() );
431 m_dll.Load( dllFile.GetFullPath(), wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
433 if(
m_dll.IsLoaded() )
435 wxLogTrace(
traceNgspice,
"libngspice path found in: %s", dllFile.GetFullPath() );
440 if( !
m_dll.IsLoaded() )
441 m_dll.Load( wxDynamicLibrary::CanonicalizeName(
"ngspice" ) );
444 m_dll.Load( NGSPICE_DLL_FILE, wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
447 if( !
m_dll.IsLoaded() )
449 for(
const auto&
path : dllPaths )
451 dllFile.SetPath(
path );
452 wxLogTrace(
traceNgspice,
"libngspice search path: %s", dllFile.GetFullPath() );
453 m_dll.Load( dllFile.GetFullPath(), wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
455 if(
m_dll.IsLoaded() )
457 wxLogTrace(
traceNgspice,
"libngspice path found in: %s", dllFile.GetFullPath() );
464 if( !
m_dll.IsLoaded() )
465 throw std::runtime_error(
"Missing ngspice shared library" );
484 wxString cwd( wxGetCwd() );
485 wxFileName exeDir( stdPaths.GetExecutablePath() );
486 wxSetWorkingDirectory( exeDir.GetPath() );
492 if( !cmPath.empty() )
493 Command(
"set __CMPATH=\"" + cmPath +
"\"" );
496 const std::vector<std::string> spiceinitPaths =
500 stdPaths.GetPluginsDir().ToStdString() +
"/sim/ngspice/scripts",
501 wxFileName( stdPaths.GetExecutablePath() ).GetPath().ToStdString() +
502 "/../../../../../Contents/PlugIns/sim/ngspice/scripts"
510 bool foundSpiceinit =
false;
512 for(
const auto&
path : spiceinitPaths )
519 foundSpiceinit =
true;
526 if( !foundSpiceinit && !cmPath.empty() )
530 wxSetWorkingDirectory( cwd );
543 std::vector<char*> lines;
544 lines.push_back( strdup(
"*" ) );
545 lines.push_back( strdup(
".end" ) );
546 lines.push_back(
nullptr );
550 for(
auto line : lines )
559 if( !wxFileName::FileExists( aFileName ) )
564 if( !file.Open( aFileName ) )
567 for( wxString& cmd = file.GetFirstLine(); !file.Eof(); cmd = file.GetNextLine() )
576 const std::vector<std::string> cmPaths =
579 "/Applications/ngspice/lib/ngspice",
580 "Contents/Frameworks",
581 wxStandardPaths::Get().GetPluginsDir().ToStdString() +
"/sim/ngspice",
582 wxFileName( wxStandardPaths::Get().GetExecutablePath() ).GetPath().ToStdString() +
583 "/../../../../../Contents/PlugIns/sim/ngspice",
584 "../Plugins/sim/ngspice",
592 for(
const auto&
path : cmPaths )
596 if( wxFileName::FileExists(
path +
"/spice2poly.cm" ) )
603 return std::string();
609 wxArrayString cmFiles;
610 size_t count = wxDir::GetAllFiles( aPath, &cmFiles );
612 for(
const auto& cm : cmFiles )
613 Command( fmt::format(
"codemodel '{}'", cm.ToStdString() ) );
626 if( ( strncasecmp( aWhat,
"stdout ", 7 ) == 0 )
627 || ( strncasecmp( aWhat,
"stderr ", 7 ) == 0 ) )
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
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.
ngSpice_Circ m_ngSpice_Circ
bool Command(const std::string &aCmd) override final
Set a SIMULATOR_REPORTER object to receive the simulation log.
char **(* ngSpice_AllVecs)(char *plotname)
ngSpice_AllPlots m_ngSpice_AllPlots
static int cbControlledExit(int aStatus, NG_BOOL aImmediate, NG_BOOL aExitOnQuit, int aId, void *aUser)
int(* ngSpice_Circ)(char **circarray)
char **(* ngSpice_AllPlots)(void)
bool IsRunning() override final
Execute a Spice command as if it was typed into console.
std::vector< double > GetImaginaryVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with magnitude values.
virtual const std::string GetNetlist() const override final
Cleans simulation data (i.e.
char *(* ngSpice_CurPlot)(void)
ngSpice_Init m_ngSpice_Init
ngSpice_Command m_ngSpice_Command
void(* ngSpice_Init)(SendChar *, SendStat *, ControlledExit *, SendData *, SendInitData *, BGThreadRunning *, void *)
wxString GetXAxis(SIM_TYPE aType) const override final
wxString CurrentPlotName() const override final
ngGet_Vec_Info m_ngGet_Vec_Info
bool m_error
Error flag indicating that ngspice needs to be reloaded.
std::vector< double > GetPhaseVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with phase values.
ngSpice_CurPlot m_ngSpice_CurPlot
bool loadSpinit(const std::string &aFileName)
Check a few different locations for codemodel files and returns one if it exists.
bool Run() override final
Halt the simulation.
bool LoadNetlist(const std::string &aNetlist) override final
Execute the simulation with currently loaded netlist.
static int cbBGThreadRunning(NG_BOOL aFinished, int aId, void *aUser)
std::vector< double > GetGainVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with phase values.
bool Stop() override final
Check if simulation is running at the moment.
int(* ngSpice_Command)(char *command)
static bool m_initialized
Ngspice should be initialized only once.
static int cbSendStat(char *what, int aId, void *aUser)
static int cbSendChar(char *what, int aId, void *aUser)
std::vector< double > GetRealVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with imaginary values.
std::vector< std::string > GetSettingCommands() const override final
Return current SPICE netlist used by the simulator.
std::string m_netlist
Current netlist.
ngSpice_AllVecs m_ngSpice_AllVecs
pvector_info(* ngGet_Vec_Info)(char *vecname)
ngSpice_Running m_ngSpice_Running
void Clean() override final
Cleans simulation data (i.e.
bool(* ngSpice_Running)(void)
Handle to DLL functions.
wxDynamicLibrary m_dll
Execute commands from a file.
void Init(const SPICE_SETTINGS *aSettings=nullptr) override final
Point out the model that will be used in future simulations.
bool Attach(const std::shared_ptr< SIMULATION_MODEL > &aModel, const wxString &aSimCommand, unsigned aSimOptions, REPORTER &aReporter) override final
Load a netlist for the simulation.
std::string findCmPath() const
Load codemodel files from a directory.
bool loadCodemodels(const std::string &aPath)
std::vector< COMPLEX > GetComplexVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with real values.
A pure virtual class used to derive REPORTER objects from.
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, REPORTER &aReporter)
Point out the model that will be used in future simulations.
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.
static const wxChar *const traceNgspice
Flag to enable debug output of Ngspice simulator.
SIM_TYPE
< Possible simulation types