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-2024 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, const wxString& aInputPath,
64 REPORTER& aReporter ) override final;
65
67 bool LoadNetlist( const std::string& aNetlist ) override final;
68
70 bool Run() override final;
71
73 bool Stop() override final;
74
76 bool IsRunning() override final;
77
79 bool Command( const std::string& aCmd ) override final;
80
82 wxString GetXAxis( SIM_TYPE aType ) const override final;
83
85 wxString CurrentPlotName() const override final;
86
88 std::vector<std::string> AllVectors() const override final;
89
91 std::vector<COMPLEX> GetComplexVector( const std::string& aName, int aMaxLen = -1 ) override final;
92
94 std::vector<double> GetRealVector( const std::string& aName, int aMaxLen = -1 ) override final;
95
97 std::vector<double> GetImaginaryVector( const std::string& aName, int aMaxLen = -1 ) override final;
98
100 std::vector<double> GetGainVector( const std::string& aName, int aMaxLen = -1 ) override final;
101
103 std::vector<double> GetPhaseVector( const std::string& aName, int aMaxLen = -1 ) override final;
104
105 std::vector<std::string> GetSettingCommands() const override final;
106
108 virtual const std::string GetNetlist() const override final;
109
111 void Clean() override final;
112
113private:
114 // Performs DLL initialization, obtains function pointers
115 void init_dll();
116
117 // ngspice library functions
118 typedef void ( *ngSpice_Init )( SendChar*, SendStat*, ControlledExit*, SendData*, SendInitData*,
119 BGThreadRunning*, void* );
120
121 typedef int ( *ngSpice_Circ )( char** circarray );
122 typedef int ( *ngSpice_Command )( char* command );
123 typedef pvector_info ( *ngGet_Vec_Info )( char* vecname );
124 typedef char* ( *ngCM_Input_Path )( const char* path );
125 typedef char* ( *ngSpice_CurPlot )( void );
126 typedef char** ( *ngSpice_AllPlots )( void );
127 typedef char** ( *ngSpice_AllVecs )( char* plotname );
128 typedef bool ( *ngSpice_Running )( void );
129 typedef int ( *ngSpice_LockRealloc )( void );
130 typedef int ( *ngSpice_UnlockRealloc )( void );
131
144
145 wxDynamicLibrary m_dll;
146
148 {
149 public:
151 m_ngspice( ngspice )
152 {
153 if( m_ngspice->m_ngSpice_LockRealloc )
154 m_ngspice->m_ngSpice_LockRealloc();
155 };
156
158 {
159 if( m_ngspice->m_ngSpice_UnlockRealloc )
160 m_ngspice->m_ngSpice_UnlockRealloc();
161 };
162
163 private:
165 };
166
168 bool loadSpinit( const std::string& aFileName );
169
171
173 std::string findCmPath() const;
174
176 bool setCodemodelsInputPath( const std::string& aPath );
177
179 bool loadCodemodels( const std::string& aPath );
180
181 // Callback functions
182 static int cbSendChar( char* what, int aId, void* aUser );
183 static int cbSendStat( char* what, int aId, void* aUser );
184 static int cbBGThreadRunning( NG_BOOL aFinished, int aId, void* aUser );
185 static int cbControlledExit( int aStatus, NG_BOOL aImmediate, NG_BOOL aExitOnQuit, int aId,
186 void* aUser );
187
188 // Assure ngspice is in a valid state and reinitializes it if need be.
189 void validate();
190
191private:
192 bool m_error;
193
194 static bool m_initialized;
195
196 std::string m_netlist;
197};
198
199#endif /* NGSPICE_H */
Execute commands from a file.
Definition: ngspice.h:148
NGSPICE_LOCK_REALLOC(NGSPICE *ngspice)
Definition: ngspice.h:150
std::vector< std::string > AllVectors() const override final
Return a requested vector with complex values.
Definition: ngspice.cpp:102
ngSpice_Circ m_ngSpice_Circ
Definition: ngspice.h:134
bool Command(const std::string &aCmd) override final
Set a SIMULATOR_REPORTER object to receive the simulation log.
Definition: ngspice.cpp:356
char **(* ngSpice_AllVecs)(char *plotname)
Definition: ngspice.h:127
ngSpice_AllPlots m_ngSpice_AllPlots
Definition: ngspice.h:139
static int cbControlledExit(int aStatus, NG_BOOL aImmediate, NG_BOOL aExitOnQuit, int aId, void *aUser)
Definition: ngspice.cpp:713
int(* ngSpice_Circ)(char **circarray)
Definition: ngspice.h:121
ngSpice_UnlockRealloc m_ngSpice_UnlockRealloc
Definition: ngspice.h:143
char **(* ngSpice_AllPlots)(void)
Definition: ngspice.h:126
bool IsRunning() override final
Execute a Spice command as if it was typed into console.
Definition: ngspice.cpp:349
std::vector< double > GetImaginaryVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with magnitude values.
Definition: ngspice.cpp:193
virtual const std::string GetNetlist() const override final
Cleans simulation data (i.e.
Definition: ngspice.cpp:417
char *(* ngSpice_CurPlot)(void)
Definition: ngspice.h:125
ngSpice_Init m_ngSpice_Init
Definition: ngspice.h:133
void updateNgspiceSettings()
Check a few different locations for codemodel files and returns one if it exists.
Definition: ngspice.cpp:79
ngSpice_Command m_ngSpice_Command
Definition: ngspice.h:135
int(* ngSpice_LockRealloc)(void)
Definition: ngspice.h:129
void(* ngSpice_Init)(SendChar *, SendStat *, ControlledExit *, SendData *, SendInitData *, BGThreadRunning *, void *)
Definition: ngspice.h:118
bool setCodemodelsInputPath(const std::string &aPath)
Load codemodel files from a directory.
Definition: ngspice.cpp:651
wxString GetXAxis(SIM_TYPE aType) const override final
Definition: ngspice.cpp:364
wxString CurrentPlotName() const override final
Definition: ngspice.cpp:96
ngGet_Vec_Info m_ngGet_Vec_Info
Definition: ngspice.h:136
bool m_error
Error flag indicating that ngspice needs to be reloaded.
Definition: ngspice.h:192
ngCM_Input_Path m_ngCM_Input_Path
Definition: ngspice.h:137
std::vector< double > GetPhaseVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with phase values.
Definition: ngspice.cpp:248
virtual ~NGSPICE()
ngSpice_LockRealloc m_ngSpice_LockRealloc
Definition: ngspice.h:142
ngSpice_CurPlot m_ngSpice_CurPlot
Definition: ngspice.h:138
void init_dll()
Definition: ngspice.cpp:423
bool loadSpinit(const std::string &aFileName)
Definition: ngspice.cpp:600
bool Run() override final
Halt the simulation.
Definition: ngspice.cpp:335
bool LoadNetlist(const std::string &aNetlist) override final
Execute the simulation with currently loaded netlist.
Definition: ngspice.cpp:308
static int cbBGThreadRunning(NG_BOOL aFinished, int aId, void *aUser)
Definition: ngspice.cpp:702
std::vector< double > GetGainVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with phase values.
Definition: ngspice.cpp:218
bool Stop() override final
Check if simulation is running at the moment.
Definition: ngspice.cpp:342
int(* ngSpice_Command)(char *command)
Definition: ngspice.h:122
static bool m_initialized
Ngspice should be initialized only once.
Definition: ngspice.h:194
static int cbSendStat(char *what, int aId, void *aUser)
Definition: ngspice.cpp:696
static int cbSendChar(char *what, int aId, void *aUser)
Definition: ngspice.cpp:676
std::vector< double > GetRealVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with imaginary values.
Definition: ngspice.cpp:160
char *(* ngCM_Input_Path)(const char *path)
Definition: ngspice.h:124
std::vector< std::string > GetSettingCommands() const override final
Return current SPICE netlist used by the simulator.
Definition: ngspice.cpp:393
std::string m_netlist
Current netlist.
Definition: ngspice.h:196
ngSpice_AllVecs m_ngSpice_AllVecs
Definition: ngspice.h:140
int(* ngSpice_UnlockRealloc)(void)
Handle to DLL functions.
Definition: ngspice.h:130
pvector_info(* ngGet_Vec_Info)(char *vecname)
Definition: ngspice.h:123
ngSpice_Running m_ngSpice_Running
Definition: ngspice.h:141
void Clean() override final
Cleans simulation data (i.e.
Definition: ngspice.cpp:734
bool(* ngSpice_Running)(void)
Definition: ngspice.h:128
wxDynamicLibrary m_dll
Definition: ngspice.h:145
NGSPICE()
Definition: ngspice.cpp:58
void Init(const SPICE_SETTINGS *aSettings=nullptr) override final
Point out the model that will be used in future simulations.
Definition: ngspice.cpp:89
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:278
void validate()
Definition: ngspice.cpp:724
std::string findCmPath() const
Send additional search path for codemodels to ngspice.
Definition: ngspice.cpp:617
bool loadCodemodels(const std::string &aPath)
Definition: ngspice.cpp:664
std::vector< COMPLEX > GetComplexVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with real values.
Definition: ngspice.cpp:130
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:72
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