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 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
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 "spice_simulator.h"
31 
32 #include <wx/dynlib.h>
33 #include <ngspice/sharedspice.h>
34 
35 // We have an issue here where NGSPICE incorrectly used bool for years
36 // and defined it to be int when in C-mode. We cannot adjust the function
37 // signatures without re-writing sharedspice.h for KiCad.
38 // Instead, we maintain status-quo for older NGSPICE versions (<=34) and
39 // use the new signatures for newer versions
40 #ifndef NGSPICE_PACKAGE_VERSION
41  typedef bool NG_BOOL;
42 #endif
43 
44 class wxDynamicLibrary;
45 
46 class NGSPICE : public SPICE_SIMULATOR
47 {
48 public:
49  NGSPICE();
50  virtual ~NGSPICE();
51 
53  void Init( const SPICE_SIMULATOR_SETTINGS* aSettings = nullptr ) override;
54 
56  bool LoadNetlist( const std::string& aNetlist ) override;
57 
59  bool Run() override;
60 
62  bool Stop() override;
63 
65  bool IsRunning() override;
66 
68  bool Command( const std::string& aCmd ) override;
69 
71  std::string GetXAxis( SIM_TYPE aType ) const override;
72 
74  std::vector<std::string> AllPlots() const override;
75 
77  std::vector<COMPLEX> GetPlot( const std::string& aName, int aMaxLen = -1 ) override;
78 
80  std::vector<double> GetRealPlot( const std::string& aName, int aMaxLen = -1 ) override;
81 
83  std::vector<double> GetImagPlot( const std::string& aName, int aMaxLen = -1 ) override;
84 
86  std::vector<double> GetMagPlot( const std::string& aName, int aMaxLen = -1 ) override;
87 
89  std::vector<double> GetPhasePlot( const std::string& aName, int aMaxLen = -1 ) override;
90 
91  std::vector<std::string> GetSettingCommands() const override;
92 
94  virtual const std::string GetNetlist() const override;
95 
96 private:
97  void init();
98 
99  // Performs DLL initialization, obtains function pointers
100  void init_dll();
101 
102  // ngspice library functions
103  typedef void ( *ngSpice_Init )( SendChar*, SendStat*, ControlledExit*, SendData*, SendInitData*,
104  BGThreadRunning*, void* );
105  typedef int ( *ngSpice_Circ )( char** circarray );
106  typedef int ( *ngSpice_Command )( char* command );
107  typedef pvector_info ( *ngGet_Vec_Info )( char* vecname );
108  typedef char* ( *ngSpice_CurPlot )( void );
109  typedef char** ( *ngSpice_AllPlots )( void );
110  typedef char** ( *ngSpice_AllVecs )( char* plotname );
111  typedef bool ( *ngSpice_Running )( void );
112 
122 
123  wxDynamicLibrary m_dll;
124 
125 
127  bool loadSpinit( const std::string& aFileName );
128 
130  std::string findCmPath() const;
131 
133  bool loadCodemodels( const std::string& aPath );
134 
135  // Callback functions
136  static int cbSendChar( char* what, int aId, void* aUser );
137  static int cbSendStat( char* what, int aId, void* aUser );
138  static int cbBGThreadRunning( NG_BOOL aFinished, int aId, void* aUser );
139  static int cbControlledExit( int aStatus, NG_BOOL aImmediate, NG_BOOL aExitOnQuit, int aId,
140  void* aUser );
141 
142  // Assure ngspice is in a valid state and reinitializes it if need be.
143  void validate();
144 
146  bool m_error;
147 
149  static bool m_initialized;
150 
152  std::string m_netlist;
153 };
154 
155 #endif /* NGSPICE_H */
std::vector< std::string > AllPlots() const override
Return a requested vector with complex values.
Definition: ngspice.cpp:92
static int cbBGThreadRunning(NG_BOOL aFinished, int aId, void *aUser)
Definition: ngspice.cpp:652
bool NG_BOOL
Definition: ngspice.h:41
virtual ~NGSPICE()
Definition: ngspice.cpp:75
bool Run() override
Halt the simulation.
Definition: ngspice.cpp:283
void validate()
Error flag indicating that ngspice needs to be reloaded.
Definition: ngspice.cpp:673
std::string findCmPath() const
Load codemodel files from a directory.
Definition: ngspice.cpp:583
std::vector< double > GetRealPlot(const std::string &aName, int aMaxLen=-1) override
Return a requested vector with imaginary values.
Definition: ngspice.cpp:147
char **(* ngSpice_AllVecs)(char *plotname)
Definition: ngspice.h:110
wxDynamicLibrary m_dll
Execute commands from a file.
Definition: ngspice.h:123
bool loadCodemodels(const std::string &aPath)
Definition: ngspice.cpp:616
std::vector< double > GetImagPlot(const std::string &aName, int aMaxLen=-1) override
Return a requested vector with magnitude values.
Definition: ngspice.cpp:179
ngSpice_Running m_ngSpice_Running
Definition: ngspice.h:121
std::vector< double > GetMagPlot(const std::string &aName, int aMaxLen=-1) override
Return a requested vector with phase values.
Definition: ngspice.cpp:203
static bool m_initialized
Current netlist.
Definition: ngspice.h:149
void(* ngSpice_Init)(SendChar *, SendStat *, ControlledExit *, SendData *, SendInitData *, BGThreadRunning *, void *)
Definition: ngspice.h:103
virtual const std::string GetNetlist() const override
Return current SPICE netlist used by the simulator.
Definition: ngspice.cpp:408
NGSPICE()
Definition: ngspice.cpp:60
ngGet_Vec_Info m_ngGet_Vec_Info
Definition: ngspice.h:117
bool m_error
Ngspice should be initialized only once.
Definition: ngspice.h:146
void init_dll()
Definition: ngspice.cpp:414
SIM_TYPE
< Possible simulation types
Definition: sim_types.h:31
pvector_info(* ngGet_Vec_Info)(char *vecname)
Definition: ngspice.h:107
char *(* ngSpice_CurPlot)(void)
Definition: ngspice.h:108
std::vector< COMPLEX > GetPlot(const std::string &aName, int aMaxLen=-1) override
Return a requested vector with real values.
Definition: ngspice.cpp:120
bool Stop() override
Check if simulation is running at the moment.
Definition: ngspice.cpp:307
ngSpice_Init m_ngSpice_Init
Definition: ngspice.h:114
std::string GetXAxis(SIM_TYPE aType) const override
Return a list with all vectors generated in current simulation.
Definition: ngspice.cpp:330
std::vector< double > GetPhasePlot(const std::string &aName, int aMaxLen=-1) override
Return a requested vector with phase values.
Definition: ngspice.cpp:230
int(* ngSpice_Command)(char *command)
Definition: ngspice.h:106
void init()
void Init(const SPICE_SIMULATOR_SETTINGS *aSettings=nullptr) override
Load a netlist for the simulation.
Definition: ngspice.cpp:80
bool LoadNetlist(const std::string &aNetlist) override
Execute the simulation with currently loaded netlist.
Definition: ngspice.cpp:257
bool(* ngSpice_Running)(void)
Handle to DLL functions.
Definition: ngspice.h:111
static int cbControlledExit(int aStatus, NG_BOOL aImmediate, NG_BOOL aExitOnQuit, int aId, void *aUser)
Definition: ngspice.cpp:663
bool Command(const std::string &aCmd) override
Set a SPICE_REPORTER object to receive the simulation log.
Definition: ngspice.cpp:321
static int cbSendStat(char *what, int aId, void *aUser)
Definition: ngspice.cpp:646
bool IsRunning() override
Execute a Spice command as if it was typed into console.
Definition: ngspice.cpp:314
ngSpice_AllVecs m_ngSpice_AllVecs
Definition: ngspice.h:120
std::vector< std::string > GetSettingCommands() const override
Return current SPICE netlist used by the simulator.
Definition: ngspice.cpp:364
ngSpice_Command m_ngSpice_Command
Definition: ngspice.h:116
std::string m_netlist
Definition: ngspice.h:152
bool loadSpinit(const std::string &aFileName)
Check a few different locations for codemodel files and returns one if it exists.
Definition: ngspice.cpp:566
static int cbSendChar(char *what, int aId, void *aUser)
Definition: ngspice.cpp:628
ngSpice_Circ m_ngSpice_Circ
Definition: ngspice.h:115
ngSpice_CurPlot m_ngSpice_CurPlot
Definition: ngspice.h:118
ngSpice_AllPlots m_ngSpice_AllPlots
Definition: ngspice.h:119
char **(* ngSpice_AllPlots)(void)
Definition: ngspice.h:109
int(* ngSpice_Circ)(char **circarray)
Definition: ngspice.h:105
Storage for simulator specific settings.