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 (C) 2021-2022 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// We have an issue here where NGSPICE incorrectly used bool for years
41// and defined it to be int when in C-mode. We cannot adjust the function
42// signatures without re-writing sharedspice.h for KiCad.
43// Instead, we maintain status-quo for older NGSPICE versions (<=34) and
44// use the new signatures for newer versions
45#ifndef NGSPICE_PACKAGE_VERSION
46 typedef bool NG_BOOL;
47#endif
48
49class wxDynamicLibrary;
50
51
53{
54public:
55 NGSPICE();
56 virtual ~NGSPICE();
57
59 void Init( const SPICE_SETTINGS* aSettings = nullptr ) override final;
60
62 bool Attach( const std::shared_ptr<SIMULATION_MODEL>& aModel, const wxString& aSimCommand,
63 unsigned aSimOptions, REPORTER& aReporter ) override final;
64
66 bool LoadNetlist( const std::string& aNetlist ) override final;
67
69 bool Run() override final;
70
72 bool Stop() override final;
73
75 bool IsRunning() override final;
76
78 bool Command( const std::string& aCmd ) override final;
79
81 wxString GetXAxis( SIM_TYPE aType ) const override final;
82
84 wxString CurrentPlotName() const override final;
85
87 std::vector<std::string> AllVectors() const override final;
88
90 std::vector<COMPLEX> GetComplexVector( const std::string& aName, int aMaxLen = -1 ) override final;
91
93 std::vector<double> GetRealVector( const std::string& aName, int aMaxLen = -1 ) override final;
94
96 std::vector<double> GetImaginaryVector( const std::string& aName, int aMaxLen = -1 ) override final;
97
99 std::vector<double> GetGainVector( const std::string& aName, int aMaxLen = -1 ) override final;
100
102 std::vector<double> GetPhaseVector( const std::string& aName, int aMaxLen = -1 ) override final;
103
104 std::vector<std::string> GetSettingCommands() const override final;
105
107 virtual const std::string GetNetlist() const override final;
108
110 void Clean() override final;
111
112private:
113 // Performs DLL initialization, obtains function pointers
114 void init_dll();
115
116 // ngspice library functions
117 typedef void ( *ngSpice_Init )( SendChar*, SendStat*, ControlledExit*, SendData*, SendInitData*,
118 BGThreadRunning*, void* );
119 typedef int ( *ngSpice_Circ )( char** circarray );
120 typedef int ( *ngSpice_Command )( char* command );
121 typedef pvector_info ( *ngGet_Vec_Info )( char* vecname );
122 typedef char* ( *ngSpice_CurPlot )( void );
123 typedef char** ( *ngSpice_AllPlots )( void );
124 typedef char** ( *ngSpice_AllVecs )( char* plotname );
125 typedef bool ( *ngSpice_Running )( void );
126 typedef int ( *ngSpice_LockRealloc )( void );
127 typedef int ( *ngSpice_UnlockRealloc )( void );
128
140
141 wxDynamicLibrary m_dll;
142
144 {
145 public:
147 m_ngspice( ngspice )
148 {
149 if( m_ngspice->m_ngSpice_LockRealloc )
150 m_ngspice->m_ngSpice_LockRealloc();
151 };
152
154 {
155 if( m_ngspice->m_ngSpice_UnlockRealloc )
156 m_ngspice->m_ngSpice_UnlockRealloc();
157 };
158
159 private:
161 };
162
164 bool loadSpinit( const std::string& aFileName );
165
167
169 std::string findCmPath() const;
170
172 bool loadCodemodels( const std::string& aPath );
173
174 // Callback functions
175 static int cbSendChar( char* what, int aId, void* aUser );
176 static int cbSendStat( char* what, int aId, void* aUser );
177 static int cbBGThreadRunning( NG_BOOL aFinished, int aId, void* aUser );
178 static int cbControlledExit( int aStatus, NG_BOOL aImmediate, NG_BOOL aExitOnQuit, int aId,
179 void* aUser );
180
181 // Assure ngspice is in a valid state and reinitializes it if need be.
182 void validate();
183
184private:
185 bool m_error;
186
187 static bool m_initialized;
188
189 std::string m_netlist;
190};
191
192#endif /* NGSPICE_H */
Execute commands from a file.
Definition: ngspice.h:144
NGSPICE_LOCK_REALLOC(NGSPICE *ngspice)
Definition: ngspice.h:146
std::vector< std::string > AllVectors() const override final
Return a requested vector with complex values.
Definition: ngspice.cpp:100
ngSpice_Circ m_ngSpice_Circ
Definition: ngspice.h:131
bool Command(const std::string &aCmd) override final
Set a SIMULATOR_REPORTER object to receive the simulation log.
Definition: ngspice.cpp:346
char **(* ngSpice_AllVecs)(char *plotname)
Definition: ngspice.h:124
ngSpice_AllPlots m_ngSpice_AllPlots
Definition: ngspice.h:135
static int cbControlledExit(int aStatus, NG_BOOL aImmediate, NG_BOOL aExitOnQuit, int aId, void *aUser)
Definition: ngspice.cpp:686
int(* ngSpice_Circ)(char **circarray)
Definition: ngspice.h:119
ngSpice_UnlockRealloc m_ngSpice_UnlockRealloc
Definition: ngspice.h:139
char **(* ngSpice_AllPlots)(void)
Definition: ngspice.h:123
bool IsRunning() override final
Execute a Spice command as if it was typed into console.
Definition: ngspice.cpp:339
std::vector< double > GetImaginaryVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with magnitude values.
Definition: ngspice.cpp:191
virtual const std::string GetNetlist() const override final
Cleans simulation data (i.e.
Definition: ngspice.cpp:407
char *(* ngSpice_CurPlot)(void)
Definition: ngspice.h:122
ngSpice_Init m_ngSpice_Init
Definition: ngspice.h:130
void updateNgspiceSettings()
Check a few different locations for codemodel files and returns one if it exists.
Definition: ngspice.cpp:77
ngSpice_Command m_ngSpice_Command
Definition: ngspice.h:132
int(* ngSpice_LockRealloc)(void)
Definition: ngspice.h:126
void(* ngSpice_Init)(SendChar *, SendStat *, ControlledExit *, SendData *, SendInitData *, BGThreadRunning *, void *)
Definition: ngspice.h:117
wxString GetXAxis(SIM_TYPE aType) const override final
Definition: ngspice.cpp:354
wxString CurrentPlotName() const override final
Definition: ngspice.cpp:94
ngGet_Vec_Info m_ngGet_Vec_Info
Definition: ngspice.h:133
bool m_error
Error flag indicating that ngspice needs to be reloaded.
Definition: ngspice.h:185
std::vector< double > GetPhaseVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with phase values.
Definition: ngspice.cpp:246
virtual ~NGSPICE()
ngSpice_LockRealloc m_ngSpice_LockRealloc
Definition: ngspice.h:138
ngSpice_CurPlot m_ngSpice_CurPlot
Definition: ngspice.h:134
void init_dll()
Definition: ngspice.cpp:413
bool loadSpinit(const std::string &aFileName)
Definition: ngspice.cpp:586
bool Run() override final
Halt the simulation.
Definition: ngspice.cpp:325
bool LoadNetlist(const std::string &aNetlist) override final
Execute the simulation with currently loaded netlist.
Definition: ngspice.cpp:297
static int cbBGThreadRunning(NG_BOOL aFinished, int aId, void *aUser)
Definition: ngspice.cpp:675
std::vector< double > GetGainVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with phase values.
Definition: ngspice.cpp:216
bool Stop() override final
Check if simulation is running at the moment.
Definition: ngspice.cpp:332
int(* ngSpice_Command)(char *command)
Definition: ngspice.h:120
static bool m_initialized
Ngspice should be initialized only once.
Definition: ngspice.h:187
static int cbSendStat(char *what, int aId, void *aUser)
Definition: ngspice.cpp:669
static int cbSendChar(char *what, int aId, void *aUser)
Definition: ngspice.cpp:649
std::vector< double > GetRealVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with imaginary values.
Definition: ngspice.cpp:158
std::vector< std::string > GetSettingCommands() const override final
Return current SPICE netlist used by the simulator.
Definition: ngspice.cpp:383
std::string m_netlist
Current netlist.
Definition: ngspice.h:189
ngSpice_AllVecs m_ngSpice_AllVecs
Definition: ngspice.h:136
int(* ngSpice_UnlockRealloc)(void)
Handle to DLL functions.
Definition: ngspice.h:127
pvector_info(* ngGet_Vec_Info)(char *vecname)
Definition: ngspice.h:121
ngSpice_Running m_ngSpice_Running
Definition: ngspice.h:137
void Clean() override final
Cleans simulation data (i.e.
Definition: ngspice.cpp:707
bool(* ngSpice_Running)(void)
Definition: ngspice.h:125
wxDynamicLibrary m_dll
Definition: ngspice.h:141
NGSPICE()
Definition: ngspice.cpp:57
void Init(const SPICE_SETTINGS *aSettings=nullptr) override final
Point out the model that will be used in future simulations.
Definition: ngspice.cpp:87
void validate()
Definition: ngspice.cpp:697
bool Attach(const std::shared_ptr< SIMULATION_MODEL > &aModel, const wxString &aSimCommand, unsigned aSimOptions, REPORTER &aReporter) override final
Load a netlist for the simulation.
Definition: ngspice.cpp:276
std::string findCmPath() const
Load codemodel files from a directory.
Definition: ngspice.cpp:603
bool loadCodemodels(const std::string &aPath)
Definition: ngspice.cpp:637
std::vector< COMPLEX > GetComplexVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with real values.
Definition: ngspice.cpp:128
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:71
Storage for simulator specific settings.
STL namespace.
bool NG_BOOL
Definition: ngspice.h:46
SIM_TYPE
< Possible simulation types
Definition: sim_types.h:32
std::complex< double > COMPLEX