KiCad PCB EDA Suite
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 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_SIMULATOR_SETTINGS* aSettings = nullptr ) override final;
60
62 bool Attach( const std::shared_ptr<SIMULATION_MODEL>& aModel ) override final;
63
65 bool LoadNetlist( const std::string& aNetlist ) override final;
66
68 bool Run() override final;
69
71 bool Stop() override final;
72
74 bool IsRunning() override final;
75
77 bool Command( const std::string& aCmd ) override final;
78
80 std::string GetXAxis( SIM_TYPE aType ) const override final;
81
83 std::vector<std::string> AllPlots() const override final;
84
86 std::vector<COMPLEX> GetPlot( const std::string& aName, int aMaxLen = -1 ) override final;
87
89 std::vector<double> GetRealPlot( const std::string& aName, int aMaxLen = -1 ) override final;
90
92 std::vector<double> GetImagPlot( const std::string& aName, int aMaxLen = -1 ) override final;
93
95 std::vector<double> GetMagPlot( const std::string& aName, int aMaxLen = -1 ) override final;
96
98 std::vector<double> GetPhasePlot( const std::string& aName, int aMaxLen = -1 ) override final;
99
100 std::vector<std::string> GetSettingCommands() const override final;
101
103 virtual const std::string GetNetlist() const override final;
104
106 void Clean() override final;
107
108private:
109 void init();
110
111 // Performs DLL initialization, obtains function pointers
112 void init_dll();
113
114 // ngspice library functions
115 typedef void ( *ngSpice_Init )( SendChar*, SendStat*, ControlledExit*, SendData*, SendInitData*,
116 BGThreadRunning*, void* );
117 typedef int ( *ngSpice_Circ )( char** circarray );
118 typedef int ( *ngSpice_Command )( char* command );
119 typedef pvector_info ( *ngGet_Vec_Info )( char* vecname );
120 typedef char* ( *ngSpice_CurPlot )( void );
121 typedef char** ( *ngSpice_AllPlots )( void );
122 typedef char** ( *ngSpice_AllVecs )( char* plotname );
123 typedef bool ( *ngSpice_Running )( void );
124
134
135 wxDynamicLibrary m_dll;
136
137
139 bool loadSpinit( const std::string& aFileName );
140
142 std::string findCmPath() const;
143
145 bool loadCodemodels( const std::string& aPath );
146
147 // Callback functions
148 static int cbSendChar( char* what, int aId, void* aUser );
149 static int cbSendStat( char* what, int aId, void* aUser );
150 static int cbBGThreadRunning( NG_BOOL aFinished, int aId, void* aUser );
151 static int cbControlledExit( int aStatus, NG_BOOL aImmediate, NG_BOOL aExitOnQuit, int aId,
152 void* aUser );
153
154 // Assure ngspice is in a valid state and reinitializes it if need be.
155 void validate();
156
159
161 static bool m_initialized;
162
164 std::string m_netlist;
165};
166
167#endif /* NGSPICE_H */
ngSpice_Circ m_ngSpice_Circ
Definition: ngspice.h:127
bool Command(const std::string &aCmd) override final
Set a SPICE_REPORTER object to receive the simulation log.
Definition: ngspice.cpp:342
char **(* ngSpice_AllVecs)(char *plotname)
Definition: ngspice.h:122
ngSpice_AllPlots m_ngSpice_AllPlots
Definition: ngspice.h:131
static int cbControlledExit(int aStatus, NG_BOOL aImmediate, NG_BOOL aExitOnQuit, int aId, void *aUser)
Definition: ngspice.cpp:683
int(* ngSpice_Circ)(char **circarray)
Definition: ngspice.h:117
std::vector< COMPLEX > GetPlot(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with real values.
Definition: ngspice.cpp:119
char **(* ngSpice_AllPlots)(void)
Definition: ngspice.h:121
bool IsRunning() override final
Execute a Spice command as if it was typed into console.
Definition: ngspice.cpp:335
void Init(const SPICE_SIMULATOR_SETTINGS *aSettings=nullptr) override final
Point out the model that will be used in future simulations.
Definition: ngspice.cpp:79
virtual const std::string GetNetlist() const override final
Cleans simulation data (i.e.
Definition: ngspice.cpp:428
char *(* ngSpice_CurPlot)(void)
Definition: ngspice.h:120
ngSpice_Init m_ngSpice_Init
Definition: ngspice.h:126
std::vector< double > GetPhasePlot(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with phase values.
Definition: ngspice.cpp:229
bool Attach(const std::shared_ptr< SIMULATION_MODEL > &aModel) override final
Load a netlist for the simulation.
Definition: ngspice.cpp:256
ngSpice_Command m_ngSpice_Command
Definition: ngspice.h:128
void(* ngSpice_Init)(SendChar *, SendStat *, ControlledExit *, SendData *, SendInitData *, BGThreadRunning *, void *)
Definition: ngspice.h:115
ngGet_Vec_Info m_ngGet_Vec_Info
Definition: ngspice.h:129
bool m_error
Ngspice should be initialized only once.
Definition: ngspice.h:158
virtual ~NGSPICE()
std::vector< double > GetMagPlot(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with phase values.
Definition: ngspice.cpp:202
ngSpice_CurPlot m_ngSpice_CurPlot
Definition: ngspice.h:130
std::string findCmPath() const
Load codemodel files from a directory.
Definition: ngspice.cpp:603
std::vector< std::string > AllPlots() const override final
Return a requested vector with complex values.
Definition: ngspice.cpp:91
void init_dll()
Definition: ngspice.cpp:434
bool loadSpinit(const std::string &aFileName)
Check a few different locations for codemodel files and returns one if it exists.
Definition: ngspice.cpp:586
bool Run() override final
Halt the simulation.
Definition: ngspice.cpp:306
bool LoadNetlist(const std::string &aNetlist) override final
Execute the simulation with currently loaded netlist.
Definition: ngspice.cpp:278
static int cbBGThreadRunning(NG_BOOL aFinished, int aId, void *aUser)
Definition: ngspice.cpp:672
std::vector< double > GetRealPlot(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with imaginary values.
Definition: ngspice.cpp:146
bool Stop() override final
Check if simulation is running at the moment.
Definition: ngspice.cpp:328
int(* ngSpice_Command)(char *command)
Definition: ngspice.h:118
static bool m_initialized
Current netlist.
Definition: ngspice.h:161
static int cbSendStat(char *what, int aId, void *aUser)
Definition: ngspice.cpp:666
static int cbSendChar(char *what, int aId, void *aUser)
Definition: ngspice.cpp:648
std::vector< std::string > GetSettingCommands() const override final
Return current SPICE netlist used by the simulator.
Definition: ngspice.cpp:384
std::string m_netlist
Definition: ngspice.h:164
std::string GetXAxis(SIM_TYPE aType) const override final
Return a list with all vectors generated in current simulation.
Definition: ngspice.cpp:350
ngSpice_AllVecs m_ngSpice_AllVecs
Definition: ngspice.h:132
std::vector< double > GetImagPlot(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with magnitude values.
Definition: ngspice.cpp:178
pvector_info(* ngGet_Vec_Info)(char *vecname)
Definition: ngspice.h:119
ngSpice_Running m_ngSpice_Running
Definition: ngspice.h:133
void Clean() override final
Cleans simulation data (i.e.
Definition: ngspice.cpp:703
bool(* ngSpice_Running)(void)
Handle to DLL functions.
Definition: ngspice.h:123
wxDynamicLibrary m_dll
Execute commands from a file.
Definition: ngspice.h:135
NGSPICE()
Definition: ngspice.cpp:61
void validate()
Error flag indicating that ngspice needs to be reloaded.
Definition: ngspice.cpp:693
bool loadCodemodels(const std::string &aPath)
Definition: ngspice.cpp:636
void init()
Storage for simulator specific settings.
Definition: bitmap.cpp:64
bool NG_BOOL
Definition: ngspice.h:46
SIM_TYPE
< Possible simulation types
Definition: sim_types.h:32
std::complex< double > COMPLEX