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 class wxDynamicLibrary;
36 
37 class NGSPICE : public SPICE_SIMULATOR
38 {
39 public:
40  NGSPICE();
41  virtual ~NGSPICE();
42 
44  void Init( const SPICE_SIMULATOR_SETTINGS* aSettings = nullptr ) override;
45 
47  bool LoadNetlist( const std::string& aNetlist ) override;
48 
50  bool Run() override;
51 
53  bool Stop() override;
54 
56  bool IsRunning() override;
57 
59  bool Command( const std::string& aCmd ) override;
60 
62  std::string GetXAxis( SIM_TYPE aType ) const override;
63 
65  std::vector<std::string> AllPlots() const override;
66 
68  std::vector<COMPLEX> GetPlot( const std::string& aName, int aMaxLen = -1 ) override;
69 
71  std::vector<double> GetRealPlot( const std::string& aName, int aMaxLen = -1 ) override;
72 
74  std::vector<double> GetImagPlot( const std::string& aName, int aMaxLen = -1 ) override;
75 
77  std::vector<double> GetMagPlot( const std::string& aName, int aMaxLen = -1 ) override;
78 
80  std::vector<double> GetPhasePlot( const std::string& aName, int aMaxLen = -1 ) override;
81 
82  std::vector<std::string> GetSettingCommands() const override;
83 
85  virtual const std::string GetNetlist() const override;
86 
87 private:
88  void init();
89 
90  // Performs DLL initialization, obtains function pointers
91  void init_dll();
92 
93  // ngspice library functions
94  typedef void ( *ngSpice_Init )( SendChar*, SendStat*, ControlledExit*, SendData*, SendInitData*,
95  BGThreadRunning*, void* );
96  typedef int ( *ngSpice_Circ )( char** circarray );
97  typedef int ( *ngSpice_Command )( char* command );
98  typedef pvector_info ( *ngGet_Vec_Info )( char* vecname );
99  typedef char* ( *ngSpice_CurPlot )( void );
100  typedef char** ( *ngSpice_AllPlots )( void );
101  typedef char** ( *ngSpice_AllVecs )( char* plotname );
102  typedef bool ( *ngSpice_Running )( void );
103 
113 
114  wxDynamicLibrary m_dll;
115 
117  bool loadSpinit( const std::string& aFileName );
118 
120  std::string findCmPath() const;
121 
123  bool loadCodemodels( const std::string& aPath );
124 
125  // Callback functions
126  static int cbSendChar( char* what, int id, void* user );
127  static int cbSendStat( char* what, int id, void* user );
128  static int cbBGThreadRunning( bool is_running, int id, void* user );
129  static int cbControlledExit( int status, bool immediate, bool exit_upon_quit, int id,
130  void* user );
131 
132  // Assure ngspice is in a valid state and reinitializes it if need be
133  void validate();
134 
136  bool m_error;
137 
139  static bool m_initialized;
140 
142  std::string m_netlist;
143 };
144 
145 #endif /* NGSPICE_H */
std::vector< std::string > AllPlots() const override
Return a requested vector with complex values.
Definition: ngspice.cpp:92
virtual ~NGSPICE()
Definition: ngspice.cpp:75
bool Run() override
Halt the simulation.
Definition: ngspice.cpp:283
static int cbBGThreadRunning(bool is_running, int id, void *user)
Definition: ngspice.cpp:635
void validate()
Error flag indicating that ngspice needs to be reloaded.
Definition: ngspice.cpp:657
std::string findCmPath() const
Load codemodel files from a directory.
Definition: ngspice.cpp:566
std::vector< double > GetRealPlot(const std::string &aName, int aMaxLen=-1) override
Return a requested vector with imaginary values.
Definition: ngspice.cpp:147
static int cbControlledExit(int status, bool immediate, bool exit_upon_quit, int id, void *user)
Definition: ngspice.cpp:647
char **(* ngSpice_AllVecs)(char *plotname)
Definition: ngspice.h:101
static int cbSendChar(char *what, int id, void *user)
Definition: ngspice.cpp:611
wxDynamicLibrary m_dll
Execute commands from a file.
Definition: ngspice.h:114
bool loadCodemodels(const std::string &aPath)
Definition: ngspice.cpp:599
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:112
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:139
void(* ngSpice_Init)(SendChar *, SendStat *, ControlledExit *, SendData *, SendInitData *, BGThreadRunning *, void *)
Definition: ngspice.h:94
virtual const std::string GetNetlist() const override
Return current SPICE netlist used by the simulator.
Definition: ngspice.cpp:391
NGSPICE()
Definition: ngspice.cpp:60
ngGet_Vec_Info m_ngGet_Vec_Info
Definition: ngspice.h:108
bool m_error
Ngspice should be initialized only once.
Definition: ngspice.h:136
void init_dll()
Definition: ngspice.cpp:397
SIM_TYPE
< Possible simulation types
Definition: sim_types.h:31
pvector_info(* ngGet_Vec_Info)(char *vecname)
Definition: ngspice.h:98
char *(* ngSpice_CurPlot)(void)
Definition: ngspice.h:99
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:290
ngSpice_Init m_ngSpice_Init
Definition: ngspice.h:105
std::string GetXAxis(SIM_TYPE aType) const override
Return a list with all vectors generated in current simulation.
Definition: ngspice.cpp:313
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:97
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:102
bool Command(const std::string &aCmd) override
Set a SPICE_REPORTER object to receive the simulation log.
Definition: ngspice.cpp:304
bool IsRunning() override
Execute a Spice command as if it was typed into console.
Definition: ngspice.cpp:297
ngSpice_AllVecs m_ngSpice_AllVecs
Definition: ngspice.h:111
std::vector< std::string > GetSettingCommands() const override
Return current SPICE netlist used by the simulator.
Definition: ngspice.cpp:347
ngSpice_Command m_ngSpice_Command
Definition: ngspice.h:107
std::string m_netlist
Definition: ngspice.h:142
bool loadSpinit(const std::string &aFileName)
Check a few different locations for codemodel files and returns one if it exists.
Definition: ngspice.cpp:549
ngSpice_Circ m_ngSpice_Circ
Definition: ngspice.h:106
ngSpice_CurPlot m_ngSpice_CurPlot
Definition: ngspice.h:109
ngSpice_AllPlots m_ngSpice_AllPlots
Definition: ngspice.h:110
static int cbSendStat(char *what, int id, void *user)
Definition: ngspice.cpp:629
char **(* ngSpice_AllPlots)(void)
Definition: ngspice.h:100
int(* ngSpice_Circ)(char **circarray)
Definition: ngspice.h:96
Storage for simulator specific settings.