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_ngCM_Input_Path( nullptr ),
63 m_ngSpice_CurPlot( nullptr ),
64 m_ngSpice_AllPlots( nullptr ),
65 m_ngSpice_AllVecs( nullptr ),
66 m_ngSpice_Running( nullptr ),
67 m_ngSpice_LockRealloc( nullptr ),
68 m_ngSpice_UnlockRealloc( nullptr ),
82 wxLogTrace(
traceNgspice,
"Sending Ngspice configuration command '%s'.", command );
108 std::vector<std::string> retVal;
110 if( allVectors !=
nullptr )
112 for(
char**
plot = allVectors; *
plot !=
nullptr;
plot++ )
115 retVal.reserve( noOfVectors );
117 for(
int i = 0; i < noOfVectors; i++, allVectors++ )
119 std::string vec = *allVectors;
120 retVal.push_back( vec );
132 std::vector<COMPLEX> data;
140 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
141 data.reserve( length );
145 for(
int i = 0; i < length; i++ )
146 data.emplace_back( vi->v_realdata[i], 0.0 );
148 else if( vi->v_compdata )
150 for(
int i = 0; i < length; i++ )
151 data.emplace_back( vi->v_compdata[i].cx_real, vi->v_compdata[i].cx_imag );
162 std::vector<double> data;
170 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
171 data.reserve( length );
175 for(
int i = 0; i < length; i++ )
176 data.push_back( vi->v_realdata[i] );
178 else if( vi->v_compdata )
180 for(
int i = 0; i < length; i++ )
182 wxASSERT( vi->v_compdata[i].cx_imag == 0.0 );
183 data.push_back( vi->v_compdata[i].cx_real );
195 std::vector<double> data;
203 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
204 data.reserve( length );
208 for(
int i = 0; i < length; i++ )
209 data.push_back( vi->v_compdata[i].cx_imag );
220 std::vector<double> data;
228 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
229 data.reserve( length );
233 for(
int i = 0; i < length; i++ )
234 data.push_back( vi->v_realdata[i] );
236 else if( vi->v_compdata )
238 for(
int i = 0; i < length; i++ )
239 data.push_back( hypot( vi->v_compdata[i].cx_real, vi->v_compdata[i].cx_imag ) );
250 std::vector<double> data;
258 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
259 data.reserve( length );
263 for(
int i = 0; i < length; i++ )
264 data.push_back( 0.0 );
266 else if( vi->v_compdata )
268 for(
int i = 0; i < length; i++ )
269 data.push_back( atan2( vi->v_compdata[i].cx_imag, vi->v_compdata[i].cx_real ) );
277bool NGSPICE::Attach(
const std::shared_ptr<SIMULATION_MODEL>& aModel,
const wxString& aSimCommand,
278 unsigned aSimOptions,
const wxString& aInputPath,
REPORTER& aReporter )
285 if( model && model->
GetNetlist( aSimCommand, aSimOptions, &formatter, aReporter ) )
293 Command(
"echo Command: esave none" );
310 std::vector<char*> lines;
311 std::stringstream ss( aNetlist );
315 for( std::string line; std::getline( ss, line ); )
317 lines.push_back( strdup( line.data() ) );
322 lines.push_back(
nullptr );
327 for(
char* line : lines )
371 return wxS(
"frequency" );
377 if( vector.Lower().EndsWith( wxS(
"-sweep" ) ) )
381 return wxS(
"sweep" );
384 return wxS(
"time" );
387 return wxEmptyString;
396 std::vector<std::string> commands;
398 wxCHECK( settings, commands );
402 case NGSPICE_COMPATIBILITY_MODE::USER_CONFIG:
break;
403 case NGSPICE_COMPATIBILITY_MODE::NGSPICE: commands.emplace_back(
"unset ngbehavior" );
break;
404 case NGSPICE_COMPATIBILITY_MODE::PSPICE: commands.emplace_back(
"set ngbehavior=psa" );
break;
405 case NGSPICE_COMPATIBILITY_MODE::LTSPICE: commands.emplace_back(
"set ngbehavior=lta" );
break;
406 case NGSPICE_COMPATIBILITY_MODE::LT_PSPICE: commands.emplace_back(
"set ngbehavior=ltpsa" );
break;
407 case NGSPICE_COMPATIBILITY_MODE::HSPICE: commands.emplace_back(
"set ngbehavior=hsa" );
break;
408 default: wxFAIL_MSG( wxString::Format(
"Undefined NGSPICE_COMPATIBILITY_MODE %d.",
428 const wxStandardPaths& stdPaths = wxStandardPaths::Get();
430 if(
m_dll.IsLoaded() )
435 wxFileName dllFile(
"", NGSPICE_DLL_FILE );
436#if defined(__WINDOWS__)
437 #if defined( _MSC_VER )
438 const std::vector<std::string> dllPaths = {
"" };
440 const std::vector<std::string> dllPaths = {
"",
"/mingw64/bin",
"/mingw32/bin" };
442#elif defined(__WXMAC__)
443 const std::vector<std::string> dllPaths = {
444 PATHS::GetOSXKicadUserDataDir().ToStdString() +
"/PlugIns/ngspice",
445 PATHS::GetOSXKicadMachineDataDir().ToStdString() +
"/PlugIns/ngspice",
448 stdPaths.GetPluginsDir().ToStdString() +
"/sim",
451 wxFileName( stdPaths.GetExecutablePath() ).GetPath().ToStdString() +
452 "/../../../../../Contents/PlugIns/sim"
455 const std::vector<std::string> dllPaths = {
"/usr/local/lib" };
458#if defined(__WINDOWS__) || (__WXMAC__)
459 for(
const auto&
path : dllPaths )
461 dllFile.SetPath(
path );
462 wxLogTrace(
traceNgspice,
"libngspice search path: %s", dllFile.GetFullPath() );
463 m_dll.Load( dllFile.GetFullPath(), wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
465 if(
m_dll.IsLoaded() )
467 wxLogTrace(
traceNgspice,
"libngspice path found in: %s", dllFile.GetFullPath() );
472 if( !
m_dll.IsLoaded() )
473 m_dll.Load( wxDynamicLibrary::CanonicalizeName(
"ngspice" ) );
476 m_dll.Load( NGSPICE_DLL_FILE, wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
479 if( !
m_dll.IsLoaded() )
481 for(
const auto&
path : dllPaths )
483 dllFile.SetPath(
path );
484 wxLogTrace(
traceNgspice,
"libngspice search path: %s", dllFile.GetFullPath() );
485 m_dll.Load( dllFile.GetFullPath(), wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
487 if(
m_dll.IsLoaded() )
489 wxLogTrace(
traceNgspice,
"libngspice path found in: %s", dllFile.GetFullPath() );
496 if( !
m_dll.IsLoaded() )
497 throw std::runtime_error(
"Missing ngspice shared library" );
512 if(
m_dll.HasSymbol(
"ngSpice_LockRealloc" ) )
523 wxString cwd( wxGetCwd() );
524 wxFileName exeDir( stdPaths.GetExecutablePath() );
525 wxSetWorkingDirectory( exeDir.GetPath() );
531 if( !cmPath.empty() )
532 Command(
"set __CMPATH=\"" + cmPath +
"\"" );
535 const std::vector<std::string> spiceinitPaths =
539 stdPaths.GetPluginsDir().ToStdString() +
"/sim/ngspice/scripts",
540 wxFileName( stdPaths.GetExecutablePath() ).GetPath().ToStdString() +
541 "/../../../../../Contents/PlugIns/sim/ngspice/scripts"
549 bool foundSpiceinit =
false;
551 for(
const auto&
path : spiceinitPaths )
558 foundSpiceinit =
true;
565 if( !foundSpiceinit && !cmPath.empty() )
569 wxSetWorkingDirectory( cwd );
582 std::vector<char*> lines;
583 lines.push_back( strdup(
"*" ) );
584 lines.push_back( strdup(
".end" ) );
585 lines.push_back(
nullptr );
589 for(
auto line : lines )
598 if( !wxFileName::FileExists( aFileName ) )
603 if( !file.Open( aFileName ) )
606 for( wxString& cmd = file.GetFirstLine(); !file.Eof(); cmd = file.GetNextLine() )
615 const std::vector<std::string> cmPaths =
618 "/Applications/ngspice/lib/ngspice",
619 "Contents/Frameworks",
620 wxStandardPaths::Get().GetPluginsDir().ToStdString() +
"/sim/ngspice",
621 wxFileName( wxStandardPaths::Get().GetExecutablePath() ).GetPath().ToStdString() +
622 "/../../../../../Contents/PlugIns/sim/ngspice",
623 "../Plugins/sim/ngspice",
625 "../eeschema/ngspice",
632 for(
const auto&
path : cmPaths )
636 if( wxFileName::FileExists(
path +
"/spice2poly.cm" ) )
643 return std::string();
662 wxArrayString cmFiles;
663 size_t count = wxDir::GetAllFiles( aPath, &cmFiles );
665 for(
const auto& cm : cmFiles )
666 Command( fmt::format(
"codemodel '{}'", cm.ToStdString() ) );
679 if( ( strncasecmp( aWhat,
"stdout ", 7 ) == 0 )
680 || ( 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 *)
bool setCodemodelsInputPath(const std::string &aPath)
Load codemodel files from a directory.
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.
ngCM_Input_Path m_ngCM_Input_Path
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.
char *(* ngCM_Input_Path)(const char *path)
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, const wxString &aInputPath, REPORTER &aReporter) override final
Load a netlist for the simulation.
std::string findCmPath() const
Send additional search path for codemodels to ngspice.
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, const wxString &aInputPath, 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