36#include <wx/stdpaths.h>
89 wxLogTrace(
traceNgspice,
"Sending Ngspice configuration command '%s'.", command );
115 std::vector<std::string> retVal;
117 if( allVectors !=
nullptr )
119 for(
char**
plot = allVectors; *
plot !=
nullptr;
plot++ )
122 retVal.reserve( noOfVectors );
124 for(
int i = 0; i < noOfVectors; i++, allVectors++ )
126 std::string vec = *allVectors;
127 retVal.push_back( std::move( vec ) );
139 std::vector<COMPLEX> data;
147 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
148 data.reserve( length );
152 for(
int i = 0; i < length; i++ )
153 data.emplace_back( vi->v_realdata[i], 0.0 );
155 else if( vi->v_compdata )
157 for(
int i = 0; i < length; i++ )
158 data.emplace_back( vi->v_compdata[i].cx_real, vi->v_compdata[i].cx_imag );
169 std::vector<double> data;
177 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
178 data.reserve( length );
182 for(
int i = 0; i < length; i++ )
183 data.push_back( vi->v_realdata[i] );
185 else if( vi->v_compdata )
187 for(
int i = 0; i < length; i++ )
188 data.push_back( vi->v_compdata[i].cx_real );
199 std::vector<double> data;
207 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
208 data.reserve( length );
212 for(
int i = 0; i < length; i++ )
213 data.push_back( vi->v_compdata[i].cx_imag );
224 std::vector<double> data;
232 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
233 data.reserve( length );
237 for(
int i = 0; i < length; i++ )
238 data.push_back( vi->v_realdata[i] );
240 else if( vi->v_compdata )
242 for(
int i = 0; i < length; i++ )
243 data.push_back( hypot( vi->v_compdata[i].cx_real, vi->v_compdata[i].cx_imag ) );
254 std::vector<double> data;
262 int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length );
263 data.reserve( length );
267 for(
int i = 0; i < length; i++ )
268 data.push_back( 0.0 );
270 else if( vi->v_compdata )
272 for(
int i = 0; i < length; i++ )
273 data.push_back( atan2( vi->v_compdata[i].cx_imag, vi->v_compdata[i].cx_real ) );
281bool NGSPICE::Attach(
const std::shared_ptr<SIMULATION_MODEL>& aModel,
const wxString& aSimCommand,
282 unsigned aSimOptions,
const wxString& aInputPath,
REPORTER& aReporter )
289 if(
model &&
model->GetNetlist( aSimCommand, aSimOptions, &formatter, aReporter ) )
297 Command(
"echo Command: esave none" );
314 std::vector<char*> lines;
315 std::stringstream ss( aNetlist );
319 for( std::string line; std::getline( ss, line ); )
321 lines.push_back( strdup( line.data() ) );
326 lines.push_back(
nullptr );
331 for(
char* line : lines )
383 case SIGSEGV: signalName = wxT(
"SIGSEGV (segmentation fault)" );
break;
384 case SIGABRT: signalName = wxT(
"SIGABRT (abort)" );
break;
385 case SIGFPE: signalName = wxT(
"SIGFPE (floating point exception)" );
break;
386 case SIGILL: signalName = wxT(
"SIGILL (illegal instruction)" );
break;
387 default: signalName = wxString::Format( wxT(
"signal %d" ), signal );
break;
391 _(
"Simulation crashed (%s). This is usually caused by a bug in ngspice "
392 "or an invalid netlist. The simulator will be reset." ),
420 return wxS(
"frequency" );
426 if( vector.Lower().EndsWith( wxS(
"-sweep" ) ) )
430 return wxS(
"sweep" );
433 return wxS(
"time" );
436 return wxEmptyString;
445 std::vector<std::string> commands;
447 wxCHECK( settings, commands );
457 default: wxFAIL_MSG( wxString::Format(
"Undefined NGSPICE_COMPATIBILITY_MODE %d.",
477 const wxStandardPaths& stdPaths = wxStandardPaths::Get();
479 if(
m_dll.IsLoaded() )
484 wxFileName dllFile(
"", NGSPICE_DLL_FILE );
485#if defined(__WINDOWS__)
486 #if defined( _MSC_VER )
487 std::vector<std::string> dllPaths = {
"" };
489 std::vector<std::string> dllPaths = {
"",
"/mingw64/bin",
"/mingw32/bin" };
491#elif defined(__WXMAC__)
492 std::vector<std::string> dllPaths = {
493 PATHS::GetOSXKicadUserDataDir().ToStdString() +
"/PlugIns/ngspice",
494 PATHS::GetOSXKicadMachineDataDir().ToStdString() +
"/PlugIns/ngspice",
497 stdPaths.GetPluginsDir().ToStdString() +
"/sim",
500 wxFileName( stdPaths.GetExecutablePath() ).GetPath().ToStdString() +
501 "/../../../../../Contents/PlugIns/sim"
504 std::vector<std::string> dllPaths = {
"/usr/local/lib" };
507 if( wxGetEnv( wxT(
"KICAD_RUN_FROM_BUILD_DIR" ),
nullptr ) )
508 dllPaths.emplace_back( NGSPICE_DLL_DIR );
510#if defined(__WINDOWS__) || (__WXMAC__)
511 for(
const auto&
path : dllPaths )
513 dllFile.SetPath(
path );
514 wxLogTrace(
traceNgspice,
"libngspice search path: %s", dllFile.GetFullPath() );
515 m_dll.Load( dllFile.GetFullPath(), wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
517 if(
m_dll.IsLoaded() )
519 wxLogTrace(
traceNgspice,
"libngspice path found in: %s", dllFile.GetFullPath() );
524 if( !
m_dll.IsLoaded() )
525 m_dll.Load( wxDynamicLibrary::CanonicalizeName(
"ngspice" ) );
528 m_dll.Load( NGSPICE_DLL_FILE, wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
531 if( !
m_dll.IsLoaded() )
533 for(
const auto&
path : dllPaths )
535 dllFile.SetPath(
path );
536 wxLogTrace(
traceNgspice,
"libngspice search path: %s", dllFile.GetFullPath() );
537 m_dll.Load( dllFile.GetFullPath(), wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
539 if(
m_dll.IsLoaded() )
541 wxLogTrace(
traceNgspice,
"libngspice path found in: %s", dllFile.GetFullPath() );
548 if( !
m_dll.IsLoaded() )
549 throw std::runtime_error(
_(
"Unable to load ngspice shared library. Please check your install." ).ToStdString() );
564 if(
m_dll.HasSymbol(
"ngSpice_LockRealloc" ) )
575 wxString cwd( wxGetCwd() );
576 wxFileName exeDir( stdPaths.GetExecutablePath() );
577 wxSetWorkingDirectory( exeDir.GetPath() );
583 if( !cmPath.empty() )
584 Command(
"set __CMPATH=\"" + cmPath +
"\"" );
587 const std::vector<std::string> spiceinitPaths =
591 stdPaths.GetPluginsDir().ToStdString() +
"/sim/ngspice/scripts",
592 wxFileName( stdPaths.GetExecutablePath() ).GetPath().ToStdString() +
593 "/../../../../../Contents/PlugIns/sim/ngspice/scripts"
601 bool foundSpiceinit =
false;
603 for(
const auto&
path : spiceinitPaths )
610 foundSpiceinit =
true;
617 if( !foundSpiceinit && !cmPath.empty() )
621 wxSetWorkingDirectory( cwd );
634 std::vector<char*> lines;
635 lines.push_back( strdup(
"*" ) );
636 lines.push_back( strdup(
".end" ) );
637 lines.push_back(
nullptr );
641 for(
auto line : lines )
650 if( !wxFileName::FileExists( aFileName ) )
655 if( !file.Open( aFileName ) )
658 for( wxString& cmd = file.GetFirstLine(); !file.Eof(); cmd = file.GetNextLine() )
667 const std::vector<std::string> cmPaths =
670 "/Applications/ngspice/lib/ngspice",
671 "Contents/Frameworks",
672 wxStandardPaths::Get().GetPluginsDir().ToStdString() +
"/sim/ngspice",
673 wxFileName( wxStandardPaths::Get().GetExecutablePath() ).GetPath().ToStdString() +
674 "/../../../../../Contents/PlugIns/sim/ngspice",
675 "../Plugins/sim/ngspice",
677 "../eeschema/ngspice",
684 for(
const auto&
path : cmPaths )
688 if( wxFileName::FileExists(
path +
"/spice2poly.cm" ) )
695 return std::string();
714 wxArrayString cmFiles;
715 size_t count = wxDir::GetAllFiles( aPath, &cmFiles );
717 for(
const auto& cm : cmFiles )
718 Command( fmt::format(
"codemodel '{}'", cm.ToStdString() ) );
728 std::lock_guard<std::mutex> lock(
sim->m_reporterMutex );
734 if( ( strncasecmp( aWhat,
"stdout ", 7 ) == 0 )
735 || ( strncasecmp( aWhat,
"stderr ", 7 ) == 0 ) )
759 sim->restoreSignalHandlers();
763 std::lock_guard<std::mutex> lock(
sim->m_reporterMutex );
781 std::lock_guard<std::mutex> lock(
sim->m_reporterMutex );
787 _(
"Simulation terminated by ngspice. This may be caused by insufficient "
788 "memory or an internal error. The simulator will be reset." ) );
834 struct sigaction* oldAction =
nullptr;
844 if( oldAction && oldAction->sa_handler != SIG_DFL && oldAction->sa_handler != SIG_IGN )
846 oldAction->sa_handler( aSignal );
851 signal( aSignal, SIG_DFL );
870 pthread_exit(
nullptr );
884 struct sigaction newAction;
886 sigemptyset( &newAction.sa_mask );
887 newAction.sa_flags = 0;
910static bool s_exceptionHandlersInstalled =
false;
911static PVOID s_vectoredHandler =
nullptr;
912static DWORD s_mainThreadId = 0;
914long __stdcall NGSPICE::sehHandler( _EXCEPTION_POINTERS* aException )
916 if( !aException || !aException->ExceptionRecord )
917 return EXCEPTION_CONTINUE_SEARCH;
919 if( GetCurrentThreadId() == s_mainThreadId )
920 return EXCEPTION_CONTINUE_SEARCH;
924 switch( aException->ExceptionRecord->ExceptionCode )
926 case EXCEPTION_ACCESS_VIOLATION:
927 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
928 case EXCEPTION_DATATYPE_MISALIGNMENT:
929 case EXCEPTION_STACK_OVERFLOW:
932 case EXCEPTION_ILLEGAL_INSTRUCTION:
933 case EXCEPTION_PRIV_INSTRUCTION:
936 case EXCEPTION_INT_DIVIDE_BY_ZERO:
937 case EXCEPTION_INT_OVERFLOW:
938 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
939 case EXCEPTION_FLT_INVALID_OPERATION:
940 case EXCEPTION_FLT_OVERFLOW:
941 case EXCEPTION_FLT_UNDERFLOW:
942 case EXCEPTION_FLT_INEXACT_RESULT:
943 case EXCEPTION_FLT_STACK_CHECK:
947 return EXCEPTION_CONTINUE_SEARCH;
957 if( aException->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW )
958 TerminateThread( GetCurrentThread(), 1 );
962 return EXCEPTION_CONTINUE_EXECUTION;
968 wxUnusedVar( aSignal );
974 if( s_exceptionHandlersInstalled )
977 s_mainThreadId = GetCurrentThreadId();
982 s_vectoredHandler = AddVectoredExceptionHandler( 1, &NGSPICE::sehHandler );
983 s_exceptionHandlersInstalled = ( s_vectoredHandler != nullptr );
989 if( s_exceptionHandlersInstalled )
991 RemoveVectoredExceptionHandler( s_vectoredHandler );
992 s_vectoredHandler =
nullptr;
993 s_exceptionHandlersInstalled =
false;
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.
static std::atomic< bool > s_crashed
Set by signal handler when ngspice crashes.
ngSpice_Circ m_ngSpice_Circ
bool Command(const std::string &aCmd) override final
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)
void restoreSignalHandlers()
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.
static void signalHandler(int aSignal)
ngSpice_AllVecs m_ngSpice_AllVecs
int(* ngSpice_UnlockRealloc)(void)
Handle to DLL functions.
pvector_info(* ngGet_Vec_Info)(char *vecname)
static std::atomic< int > s_crashSignal
Signal that caused the crash.
ngSpice_Running m_ngSpice_Running
void Clean() override final
Cleans simulation data (i.e.
bool(* ngSpice_Running)(void)
static NGSPICE * s_currentInstance
Instance that is currently running ngspice.
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.
void installSignalHandlers()
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.
Interface to receive simulation updates from SPICE_SIMULATOR class.
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.
Storage for simulator specific settings.
std::shared_ptr< SPICE_SETTINGS > & Settings()
Return the simulator configuration settings.
std::atomic< SIMULATOR_REPORTER * > m_reporter
< Reporter object to receive simulation log (not owned, accessed from BG threads).
std::mutex m_reporterMutex
We don't own this. We are just borrowing it from the SCHEMATIC_SETTINGS.
static const wxChar *const traceNgspice
Flag to enable debug output of Ngspice simulator.
static struct sigaction s_oldSigSegv
static bool s_signalHandlersInstalled
static struct sigaction s_oldSigFpe
static pthread_t s_mainThread
static struct sigaction s_oldSigAbrt
SIM_TYPE
< Possible simulation types
IbisParser parser & reporter
wxString result
Test unit parsing edge cases and error handling.