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 ),
66 m_ngSpice_LockRealloc( nullptr ),
67 m_ngSpice_UnlockRealloc( nullptr ),
81 wxLogTrace(
traceNgspice,
"Sending Ngspice configuration command '%s'.", command );
107 std::vector<std::string> retVal;
109 if( allVectors !=
nullptr )
111 for(
char**
plot = allVectors; *
plot !=
nullptr;
plot++ )
114 retVal.reserve( noOfVectors );
116 for(
int i = 0; i < noOfVectors; i++, allVectors++ )
118 std::string vec = *allVectors;
119 retVal.push_back( vec );
131 std::vector<COMPLEX> data;
139 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
140 data.reserve( length );
144 for(
int i = 0; i < length; i++ )
145 data.emplace_back( vi->v_realdata[i], 0.0 );
147 else if( vi->v_compdata )
149 for(
int i = 0; i < length; i++ )
150 data.emplace_back( vi->v_compdata[i].cx_real, vi->v_compdata[i].cx_imag );
161 std::vector<double> data;
169 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
170 data.reserve( length );
174 for(
int i = 0; i < length; i++ )
175 data.push_back( vi->v_realdata[i] );
177 else if( vi->v_compdata )
179 for(
int i = 0; i < length; i++ )
181 wxASSERT( vi->v_compdata[i].cx_imag == 0.0 );
182 data.push_back( vi->v_compdata[i].cx_real );
194 std::vector<double> data;
202 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
203 data.reserve( length );
207 for(
int i = 0; i < length; i++ )
208 data.push_back( vi->v_compdata[i].cx_imag );
219 std::vector<double> data;
227 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
228 data.reserve( length );
232 for(
int i = 0; i < length; i++ )
233 data.push_back( vi->v_realdata[i] );
235 else if( vi->v_compdata )
237 for(
int i = 0; i < length; i++ )
238 data.push_back( hypot( vi->v_compdata[i].cx_real, vi->v_compdata[i].cx_imag ) );
249 std::vector<double> data;
257 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
258 data.reserve( length );
262 for(
int i = 0; i < length; i++ )
263 data.push_back( 0.0 );
265 else if( vi->v_compdata )
267 for(
int i = 0; i < length; i++ )
268 data.push_back( atan2( vi->v_compdata[i].cx_imag, vi->v_compdata[i].cx_real ) );
276bool NGSPICE::Attach(
const std::shared_ptr<SIMULATION_MODEL>& aModel,
const wxString& aSimCommand,
277 unsigned aSimOptions,
REPORTER& aReporter )
282 if( model && model->
GetNetlist( aSimCommand, aSimOptions, &formatter, aReporter ) )
300 std::vector<char*> lines;
301 std::stringstream ss( aNetlist );
308 ss.getline( line,
sizeof( line ) );
309 lines.push_back( strdup( line ) );
310 m_netlist += std::string( line ) + std::string(
"\n" );
313 lines.push_back(
nullptr );
318 for(
char* line : lines )
362 return wxS(
"frequency" );
368 if( vector.Lower().EndsWith( wxS(
"-sweep" ) ) )
372 return wxS(
"sweep" );
375 return wxS(
"time" );
378 return wxEmptyString;
387 std::vector<std::string> commands;
389 wxCHECK( settings, commands );
393 case NGSPICE_COMPATIBILITY_MODE::USER_CONFIG:
break;
394 case NGSPICE_COMPATIBILITY_MODE::NGSPICE: commands.emplace_back(
"unset ngbehavior" );
break;
395 case NGSPICE_COMPATIBILITY_MODE::PSPICE: commands.emplace_back(
"set ngbehavior=psa" );
break;
396 case NGSPICE_COMPATIBILITY_MODE::LTSPICE: commands.emplace_back(
"set ngbehavior=lta" );
break;
397 case NGSPICE_COMPATIBILITY_MODE::LT_PSPICE: commands.emplace_back(
"set ngbehavior=ltpsa" );
break;
398 case NGSPICE_COMPATIBILITY_MODE::HSPICE: commands.emplace_back(
"set ngbehavior=hsa" );
break;
399 default: wxFAIL_MSG( wxString::Format(
"Undefined NGSPICE_COMPATIBILITY_MODE %d.",
419 const wxStandardPaths& stdPaths = wxStandardPaths::Get();
421 if(
m_dll.IsLoaded() )
426 wxFileName dllFile(
"", NGSPICE_DLL_FILE );
427#if defined(__WINDOWS__)
428 #if defined( _MSC_VER )
429 const std::vector<std::string> dllPaths = {
"" };
431 const std::vector<std::string> dllPaths = {
"",
"/mingw64/bin",
"/mingw32/bin" };
433#elif defined(__WXMAC__)
434 const std::vector<std::string> dllPaths = {
435 PATHS::GetOSXKicadUserDataDir().ToStdString() +
"/PlugIns/ngspice",
436 PATHS::GetOSXKicadMachineDataDir().ToStdString() +
"/PlugIns/ngspice",
439 stdPaths.GetPluginsDir().ToStdString() +
"/sim",
442 wxFileName( stdPaths.GetExecutablePath() ).GetPath().ToStdString() +
443 "/../../../../../Contents/PlugIns/sim"
446 const std::vector<std::string> dllPaths = {
"/usr/local/lib" };
449#if defined(__WINDOWS__) || (__WXMAC__)
450 for(
const auto&
path : dllPaths )
452 dllFile.SetPath(
path );
453 wxLogTrace(
traceNgspice,
"libngspice search path: %s", dllFile.GetFullPath() );
454 m_dll.Load( dllFile.GetFullPath(), wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
456 if(
m_dll.IsLoaded() )
458 wxLogTrace(
traceNgspice,
"libngspice path found in: %s", dllFile.GetFullPath() );
463 if( !
m_dll.IsLoaded() )
464 m_dll.Load( wxDynamicLibrary::CanonicalizeName(
"ngspice" ) );
467 m_dll.Load( NGSPICE_DLL_FILE, wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
470 if( !
m_dll.IsLoaded() )
472 for(
const auto&
path : dllPaths )
474 dllFile.SetPath(
path );
475 wxLogTrace(
traceNgspice,
"libngspice search path: %s", dllFile.GetFullPath() );
476 m_dll.Load( dllFile.GetFullPath(), wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
478 if(
m_dll.IsLoaded() )
480 wxLogTrace(
traceNgspice,
"libngspice path found in: %s", dllFile.GetFullPath() );
487 if( !
m_dll.IsLoaded() )
488 throw std::runtime_error(
"Missing ngspice shared library" );
502 if(
m_dll.HasSymbol(
"ngSpice_LockRealloc" ) )
513 wxString cwd( wxGetCwd() );
514 wxFileName exeDir( stdPaths.GetExecutablePath() );
515 wxSetWorkingDirectory( exeDir.GetPath() );
521 if( !cmPath.empty() )
522 Command(
"set __CMPATH=\"" + cmPath +
"\"" );
525 const std::vector<std::string> spiceinitPaths =
529 stdPaths.GetPluginsDir().ToStdString() +
"/sim/ngspice/scripts",
530 wxFileName( stdPaths.GetExecutablePath() ).GetPath().ToStdString() +
531 "/../../../../../Contents/PlugIns/sim/ngspice/scripts"
539 bool foundSpiceinit =
false;
541 for(
const auto&
path : spiceinitPaths )
548 foundSpiceinit =
true;
555 if( !foundSpiceinit && !cmPath.empty() )
559 wxSetWorkingDirectory( cwd );
572 std::vector<char*> lines;
573 lines.push_back( strdup(
"*" ) );
574 lines.push_back( strdup(
".end" ) );
575 lines.push_back(
nullptr );
579 for(
auto line : lines )
588 if( !wxFileName::FileExists( aFileName ) )
593 if( !file.Open( aFileName ) )
596 for( wxString& cmd = file.GetFirstLine(); !file.Eof(); cmd = file.GetNextLine() )
605 const std::vector<std::string> cmPaths =
608 "/Applications/ngspice/lib/ngspice",
609 "Contents/Frameworks",
610 wxStandardPaths::Get().GetPluginsDir().ToStdString() +
"/sim/ngspice",
611 wxFileName( wxStandardPaths::Get().GetExecutablePath() ).GetPath().ToStdString() +
612 "/../../../../../Contents/PlugIns/sim/ngspice",
613 "../Plugins/sim/ngspice",
615 "../eeschema/ngspice",
622 for(
const auto&
path : cmPaths )
626 if( wxFileName::FileExists(
path +
"/spice2poly.cm" ) )
633 return std::string();
639 wxArrayString cmFiles;
640 size_t count = wxDir::GetAllFiles( aPath, &cmFiles );
642 for(
const auto& cm : cmFiles )
643 Command( fmt::format(
"codemodel '{}'", cm.ToStdString() ) );
656 if( ( strncasecmp( aWhat,
"stdout ", 7 ) == 0 )
657 || ( strncasecmp( aWhat,
"stderr ", 7 ) == 0 ) )
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Execute commands from a file.
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)
ngSpice_UnlockRealloc m_ngSpice_UnlockRealloc
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
void updateNgspiceSettings()
Check a few different locations for codemodel files and returns one if it exists.
ngSpice_Command m_ngSpice_Command
int(* ngSpice_LockRealloc)(void)
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_LockRealloc m_ngSpice_LockRealloc
ngSpice_CurPlot m_ngSpice_CurPlot
bool loadSpinit(const std::string &aFileName)
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
int(* ngSpice_UnlockRealloc)(void)
Handle to DLL functions.
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)
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