KiCad PCB EDA Suite
Loading...
Searching...
No Matches
ngspice.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2016-2022 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Tomasz Wlostowski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 3
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * https://www.gnu.org/licenses/gpl-3.0.html
22 * or you may search the http://www.gnu.org website for the version 3 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#ifndef NGSPICE_H
28#define NGSPICE_H
29
30#include <sim/spice_simulator.h>
31#include <sim/sim_model.h>
32#include <sim/sim_value.h>
33
34#include <wx/dynlib.h>
35
36#include <ngspice/sharedspice.h>
37
38#include <enum_vector.h>
39
40#include <atomic>
41
42#if defined(__WINDOWS__)
43struct _EXCEPTION_POINTERS;
44#endif
45
46// We have an issue here where NGSPICE incorrectly used bool for years
47// and defined it to be int when in C-mode. We cannot adjust the function
48// signatures without re-writing sharedspice.h for KiCad.
49// Instead, we maintain status-quo for older NGSPICE versions (<=34) and
50// use the new signatures for newer versions
51#ifndef NGSPICE_PACKAGE_VERSION
52 typedef bool NG_BOOL;
53#endif
54
55class wxDynamicLibrary;
56
57
59{
60public:
61 NGSPICE();
62 virtual ~NGSPICE();
63
65 void Init( const SPICE_SETTINGS* aSettings = nullptr ) override final;
66
68 bool Attach( const std::shared_ptr<SIMULATION_MODEL>& aModel, const wxString& aSimCommand,
69 unsigned aSimOptions, const wxString& aInputPath,
70 REPORTER& aReporter ) override final;
71
73 bool LoadNetlist( const std::string& aNetlist ) override final;
74
76 bool Run() override final;
77
79 bool Stop() override final;
80
82 bool IsRunning() override final;
83
85 bool Command( const std::string& aCmd ) override final;
86
88 wxString GetXAxis( SIM_TYPE aType ) const override final;
89
91 wxString CurrentPlotName() const override final;
92
94 std::vector<std::string> AllVectors() const override final;
95
97 std::vector<COMPLEX> GetComplexVector( const std::string& aName, int aMaxLen = -1 ) override final;
98
100 std::vector<double> GetRealVector( const std::string& aName, int aMaxLen = -1 ) override final;
101
103 std::vector<double> GetImaginaryVector( const std::string& aName, int aMaxLen = -1 ) override final;
104
106 std::vector<double> GetGainVector( const std::string& aName, int aMaxLen = -1 ) override final;
107
109 std::vector<double> GetPhaseVector( const std::string& aName, int aMaxLen = -1 ) override final;
110
111 std::vector<std::string> GetSettingCommands() const override final;
112
114 virtual const std::string GetNetlist() const override final;
115
117 void Clean() override final;
118
119private:
120 // Performs DLL initialization, obtains function pointers
121 void init_dll();
122
123 // ngspice library functions
124 typedef void ( *ngSpice_Init )( SendChar*, SendStat*, ControlledExit*, SendData*, SendInitData*,
125 BGThreadRunning*, void* );
126
127 typedef int ( *ngSpice_Circ )( char** circarray );
128 typedef int ( *ngSpice_Command )( char* command );
129 typedef pvector_info ( *ngGet_Vec_Info )( char* vecname );
130 typedef char* ( *ngCM_Input_Path )( const char* path );
131 typedef char* ( *ngSpice_CurPlot )( void );
132 typedef char** ( *ngSpice_AllPlots )( void );
133 typedef char** ( *ngSpice_AllVecs )( char* plotname );
134 typedef bool ( *ngSpice_Running )( void );
135 typedef int ( *ngSpice_LockRealloc )( void );
136 typedef int ( *ngSpice_UnlockRealloc )( void );
137
150
151 wxDynamicLibrary m_dll;
152
154 {
155 public:
157 m_ngspice( ngspice )
158 {
159 if( m_ngspice->m_ngSpice_LockRealloc )
160 m_ngspice->m_ngSpice_LockRealloc();
161 };
162
164 {
165 if( m_ngspice->m_ngSpice_UnlockRealloc )
166 m_ngspice->m_ngSpice_UnlockRealloc();
167 };
168
169 private:
171 };
172
174 bool loadSpinit( const std::string& aFileName );
175
177
179 std::string findCmPath() const;
180
182 bool setCodemodelsInputPath( const std::string& aPath );
183
185 bool loadCodemodels( const std::string& aPath );
186
187 // Callback functions
188 static int cbSendChar( char* what, int aId, void* aUser );
189 static int cbSendStat( char* what, int aId, void* aUser );
190 static int cbBGThreadRunning( NG_BOOL aFinished, int aId, void* aUser );
191 static int cbControlledExit( int aStatus, NG_BOOL aImmediate, NG_BOOL aExitOnQuit, int aId,
192 void* aUser );
193
194 // Assure ngspice is in a valid state and reinitializes it if need be.
195 void validate();
196
197 // Install signal handlers to catch ngspice crashes
199
200 // Restore original signal handlers
202
203 // Signal handler for crashes
204 static void signalHandler( int aSignal );
205
206#if defined(__WINDOWS__)
207 // Structured exception handler for Windows crashes
208 static long __stdcall sehHandler( struct _EXCEPTION_POINTERS* aException );
209#endif
210
211private:
212 bool m_error;
213
214 static bool m_initialized;
215
216 std::string m_netlist;
217
218 static std::atomic<bool> s_crashed;
219 static std::atomic<int> s_crashSignal;
221};
222
223#endif /* NGSPICE_H */
NGSPICE_LOCK_REALLOC(NGSPICE *ngspice)
Definition ngspice.h:156
std::vector< std::string > AllVectors() const override final
Return a requested vector with complex values.
Definition ngspice.cpp:112
static std::atomic< bool > s_crashed
Set by signal handler when ngspice crashes.
Definition ngspice.h:218
ngSpice_Circ m_ngSpice_Circ
Definition ngspice.h:140
bool Command(const std::string &aCmd) override final
Set a SIMULATOR_REPORTER object to receive the simulation log.
Definition ngspice.cpp:406
char **(* ngSpice_AllVecs)(char *plotname)
Definition ngspice.h:133
ngSpice_AllPlots m_ngSpice_AllPlots
Definition ngspice.h:145
static int cbControlledExit(int aStatus, NG_BOOL aImmediate, NG_BOOL aExitOnQuit, int aId, void *aUser)
Definition ngspice.cpp:767
void restoreSignalHandlers()
Definition ngspice.cpp:878
int(* ngSpice_Circ)(char **circarray)
Definition ngspice.h:127
ngSpice_UnlockRealloc m_ngSpice_UnlockRealloc
Definition ngspice.h:149
char **(* ngSpice_AllPlots)(void)
Definition ngspice.h:132
bool IsRunning() override final
Execute a Spice command as if it was typed into console.
Definition ngspice.cpp:365
std::vector< double > GetImaginaryVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with magnitude values.
Definition ngspice.cpp:200
virtual const std::string GetNetlist() const override final
Cleans simulation data (i.e.
Definition ngspice.cpp:467
char *(* ngSpice_CurPlot)(void)
Definition ngspice.h:131
ngSpice_Init m_ngSpice_Init
Definition ngspice.h:139
void updateNgspiceSettings()
Check a few different locations for codemodel files and returns one if it exists.
Definition ngspice.cpp:89
ngSpice_Command m_ngSpice_Command
Definition ngspice.h:141
int(* ngSpice_LockRealloc)(void)
Definition ngspice.h:135
void(* ngSpice_Init)(SendChar *, SendStat *, ControlledExit *, SendData *, SendInitData *, BGThreadRunning *, void *)
Definition ngspice.h:124
bool setCodemodelsInputPath(const std::string &aPath)
Load codemodel files from a directory.
Definition ngspice.cpp:701
wxString GetXAxis(SIM_TYPE aType) const override final
Definition ngspice.cpp:414
wxString CurrentPlotName() const override final
Definition ngspice.cpp:106
ngGet_Vec_Info m_ngGet_Vec_Info
Definition ngspice.h:142
bool m_error
Error flag indicating that ngspice needs to be reloaded.
Definition ngspice.h:212
ngCM_Input_Path m_ngCM_Input_Path
Definition ngspice.h:143
std::vector< double > GetPhaseVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with phase values.
Definition ngspice.cpp:255
virtual ~NGSPICE()
ngSpice_LockRealloc m_ngSpice_LockRealloc
Definition ngspice.h:148
ngSpice_CurPlot m_ngSpice_CurPlot
Definition ngspice.h:144
void init_dll()
Definition ngspice.cpp:473
bool loadSpinit(const std::string &aFileName)
Definition ngspice.cpp:650
bool Run() override final
Halt the simulation.
Definition ngspice.cpp:342
bool LoadNetlist(const std::string &aNetlist) override final
Execute the simulation with currently loaded netlist.
Definition ngspice.cpp:315
static int cbBGThreadRunning(NG_BOOL aFinished, int aId, void *aUser)
Definition ngspice.cpp:752
std::vector< double > GetGainVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with phase values.
Definition ngspice.cpp:225
bool Stop() override final
Check if simulation is running at the moment.
Definition ngspice.cpp:353
int(* ngSpice_Command)(char *command)
Definition ngspice.h:128
static bool m_initialized
Ngspice should be initialized only once.
Definition ngspice.h:214
static int cbSendStat(char *what, int aId, void *aUser)
Definition ngspice.cpp:746
static int cbSendChar(char *what, int aId, void *aUser)
Definition ngspice.cpp:726
std::vector< double > GetRealVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with imaginary values.
Definition ngspice.cpp:170
char *(* ngCM_Input_Path)(const char *path)
Definition ngspice.h:130
std::vector< std::string > GetSettingCommands() const override final
Return current SPICE netlist used by the simulator.
Definition ngspice.cpp:443
std::string m_netlist
Current netlist.
Definition ngspice.h:216
static void signalHandler(int aSignal)
Definition ngspice.cpp:808
ngSpice_AllVecs m_ngSpice_AllVecs
Definition ngspice.h:146
int(* ngSpice_UnlockRealloc)(void)
Handle to DLL functions.
Definition ngspice.h:136
pvector_info(* ngGet_Vec_Info)(char *vecname)
Definition ngspice.h:129
static std::atomic< int > s_crashSignal
Signal that caused the crash.
Definition ngspice.h:219
ngSpice_Running m_ngSpice_Running
Definition ngspice.h:147
void Clean() override final
Cleans simulation data (i.e.
Definition ngspice.cpp:788
bool(* ngSpice_Running)(void)
Definition ngspice.h:134
wxDynamicLibrary m_dll
Definition ngspice.h:151
static NGSPICE * s_currentInstance
Instance that is currently running ngspice.
Definition ngspice.h:220
NGSPICE()
Definition ngspice.cpp:68
void Init(const SPICE_SETTINGS *aSettings=nullptr) override final
Point out the model that will be used in future simulations.
Definition ngspice.cpp:99
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.
Definition ngspice.cpp:285
void validate()
Definition ngspice.cpp:778
std::string findCmPath() const
Send additional search path for codemodels to ngspice.
Definition ngspice.cpp:667
void installSignalHandlers()
Definition ngspice.cpp:855
bool loadCodemodels(const std::string &aPath)
Definition ngspice.cpp:714
std::vector< COMPLEX > GetComplexVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with real values.
Definition ngspice.cpp:140
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
Storage for simulator specific settings.
STL namespace.
bool NG_BOOL
Definition ngspice.h:52
SIM_TYPE
< Possible simulation types
Definition sim_types.h:32
std::complex< double > COMPLEX
std::string path