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_SIMULATOR_SETTINGS* aSettings = nullptr ) override final;
60
62 bool Attach( const std::shared_ptr<SIMULATION_MODEL>& aModel,
63 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 std::vector<std::string> AllPlots() const override final;
85
87 std::vector<COMPLEX> GetPlot( const std::string& aName, int aMaxLen = -1 ) override final;
88
90 std::vector<double> GetRealPlot( const std::string& aName, int aMaxLen = -1 ) override final;
91
93 std::vector<double> GetImagPlot( const std::string& aName, int aMaxLen = -1 ) override final;
94
96 std::vector<double> GetMagPlot( const std::string& aName, int aMaxLen = -1 ) override final;
97
99 std::vector<double> GetPhasePlot( const std::string& aName, int aMaxLen = -1 ) override final;
100
101 std::vector<std::string> GetSettingCommands() const override final;
102
104 virtual const std::string GetNetlist() const override final;
105
107 void Clean() override final;
108
109private:
110 void init();
111
112 // Performs DLL initialization, obtains function pointers
113 void init_dll();
114
115 // ngspice library functions
116 typedef void ( *ngSpice_Init )( SendChar*, SendStat*, ControlledExit*, SendData*, SendInitData*,
117 BGThreadRunning*, void* );
118 typedef int ( *ngSpice_Circ )( char** circarray );
119 typedef int ( *ngSpice_Command )( char* command );
120 typedef pvector_info ( *ngGet_Vec_Info )( char* vecname );
121 typedef char* ( *ngSpice_CurPlot )( void );
122 typedef char** ( *ngSpice_AllPlots )( void );
123 typedef char** ( *ngSpice_AllVecs )( char* plotname );
124 typedef bool ( *ngSpice_Running )( void );
125
135
136 wxDynamicLibrary m_dll;
137
138
140 bool loadSpinit( const std::string& aFileName );
141
143 std::string findCmPath() const;
144
146 bool loadCodemodels( const std::string& aPath );
147
148 // Callback functions
149 static int cbSendChar( char* what, int aId, void* aUser );
150 static int cbSendStat( char* what, int aId, void* aUser );
151 static int cbBGThreadRunning( NG_BOOL aFinished, int aId, void* aUser );
152 static int cbControlledExit( int aStatus, NG_BOOL aImmediate, NG_BOOL aExitOnQuit, int aId,
153 void* aUser );
154
155 // Assure ngspice is in a valid state and reinitializes it if need be.
156 void validate();
157
158private:
159 bool m_error;
160
161 static bool m_initialized;
162
163 std::string m_netlist;
164};
165
166#endif /* NGSPICE_H */
ngSpice_Circ m_ngSpice_Circ
Definition: ngspice.h:128
bool Attach(const std::shared_ptr< SIMULATION_MODEL > &aModel, REPORTER &aReporter) override final
Load a netlist for the simulation.
Definition: ngspice.cpp:248
std::vector< double > GetPhasePlot(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with phase values.
Definition: ngspice.cpp:221
bool Command(const std::string &aCmd) override final
Set a SPICE_REPORTER object to receive the simulation log.
Definition: ngspice.cpp:334
char **(* ngSpice_AllVecs)(char *plotname)
Definition: ngspice.h:123
ngSpice_AllPlots m_ngSpice_AllPlots
Definition: ngspice.h:132
static int cbControlledExit(int aStatus, NG_BOOL aImmediate, NG_BOOL aExitOnQuit, int aId, void *aUser)
Definition: ngspice.cpp:685
int(* ngSpice_Circ)(char **circarray)
Definition: ngspice.h:118
std::vector< double > GetImagPlot(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with magnitude values.
Definition: ngspice.cpp:172
char **(* ngSpice_AllPlots)(void)
Definition: ngspice.h:122
bool IsRunning() override final
Execute a Spice command as if it was typed into console.
Definition: ngspice.cpp:327
void Init(const SPICE_SIMULATOR_SETTINGS *aSettings=nullptr) override final
Point out the model that will be used in future simulations.
Definition: ngspice.cpp:75
virtual const std::string GetNetlist() const override final
Cleans simulation data (i.e.
Definition: ngspice.cpp:413
char *(* ngSpice_CurPlot)(void)
Definition: ngspice.h:121
ngSpice_Init m_ngSpice_Init
Definition: ngspice.h:127
ngSpice_Command m_ngSpice_Command
Definition: ngspice.h:129
void(* ngSpice_Init)(SendChar *, SendStat *, ControlledExit *, SendData *, SendInitData *, BGThreadRunning *, void *)
Definition: ngspice.h:116
wxString GetXAxis(SIM_TYPE aType) const override final
Return a list with all vectors generated in current simulation.
Definition: ngspice.cpp:342
ngGet_Vec_Info m_ngGet_Vec_Info
Definition: ngspice.h:130
bool m_error
Error flag indicating that ngspice needs to be reloaded.
Definition: ngspice.h:159
std::vector< std::string > AllPlots() const override final
Return a requested vector with complex values.
Definition: ngspice.cpp:87
virtual ~NGSPICE()
ngSpice_CurPlot m_ngSpice_CurPlot
Definition: ngspice.h:131
std::vector< double > GetRealPlot(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with imaginary values.
Definition: ngspice.cpp:142
std::vector< double > GetMagPlot(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with phase values.
Definition: ngspice.cpp:194
void init_dll()
Definition: ngspice.cpp:419
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:298
bool LoadNetlist(const std::string &aNetlist) override final
Execute the simulation with currently loaded netlist.
Definition: ngspice.cpp:270
static int cbBGThreadRunning(NG_BOOL aFinished, int aId, void *aUser)
Definition: ngspice.cpp:674
bool Stop() override final
Check if simulation is running at the moment.
Definition: ngspice.cpp:320
int(* ngSpice_Command)(char *command)
Definition: ngspice.h:119
static bool m_initialized
Ngspice should be initialized only once.
Definition: ngspice.h:161
static int cbSendStat(char *what, int aId, void *aUser)
Definition: ngspice.cpp:668
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:369
std::string m_netlist
Current netlist.
Definition: ngspice.h:163
ngSpice_AllVecs m_ngSpice_AllVecs
Definition: ngspice.h:133
pvector_info(* ngGet_Vec_Info)(char *vecname)
Definition: ngspice.h:120
ngSpice_Running m_ngSpice_Running
Definition: ngspice.h:134
void Clean() override final
Cleans simulation data (i.e.
Definition: ngspice.cpp:706
bool(* ngSpice_Running)(void)
Handle to DLL functions.
Definition: ngspice.h:124
std::vector< COMPLEX > GetPlot(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with real values.
Definition: ngspice.cpp:115
wxDynamicLibrary m_dll
Execute commands from a file.
Definition: ngspice.h:136
NGSPICE()
Definition: ngspice.cpp:57
void validate()
Definition: ngspice.cpp:696
std::string findCmPath() const
Load codemodel files from a directory.
Definition: ngspice.cpp:603
bool loadCodemodels(const std::string &aPath)
Definition: ngspice.cpp:636
void init()
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