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  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * https://www.gnu.org/licenses/gpl-3.0.html
20  * or you may search the http://www.gnu.org website for the version 3 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #ifndef NGSPICE_H
26 #define NGSPICE_H
27 
28 #include "spice_simulator.h"
29 
30 #include <wx/dynlib.h>
31 #include <ngspice/sharedspice.h>
32 
33 class wxDynamicLibrary;
34 
35 class NGSPICE : public SPICE_SIMULATOR {
36 
37 public:
38  NGSPICE();
39  virtual ~NGSPICE();
40 
42  void Init() override;
43 
45  bool LoadNetlist( const std::string& aNetlist ) override;
46 
48  bool Run() override;
49 
51  bool Stop() override;
52 
54  bool IsRunning() override;
55 
57  bool Command( const std::string& aCmd ) override;
58 
60  std::string GetXAxis( SIM_TYPE aType ) const override;
61 
63  std::vector<COMPLEX> GetPlot( const std::string& aName, int aMaxLen = -1 ) override;
64 
66  std::vector<double> GetRealPlot( const std::string& aName, int aMaxLen = -1 ) override;
67 
69  std::vector<double> GetImagPlot( const std::string& aName, int aMaxLen = -1 ) override;
70 
72  std::vector<double> GetMagPlot( const std::string& aName, int aMaxLen = -1 ) override;
73 
75  std::vector<double> GetPhasePlot( const std::string& aName, int aMaxLen = -1 ) override;
76 
78  virtual const std::string GetNetlist() const override;
79 
80 private:
81  void init();
82 
83  // Performs DLL initialization, obtains function pointers
84  void init_dll();
85 
86  // ngspice library functions
87  typedef void (*ngSpice_Init)( SendChar*, SendStat*, ControlledExit*,
88  SendData*, SendInitData*, BGThreadRunning*, void* );
89  typedef int (*ngSpice_Circ)( char** circarray );
90  typedef int (*ngSpice_Command)( char* command );
91  typedef pvector_info (*ngGet_Vec_Info)( char* vecname );
92  typedef char** (*ngSpice_AllPlots)( void );
93  typedef char** (*ngSpice_AllVecs)( char* plotname );
94  typedef bool (*ngSpice_Running)( void );
95 
104 
105  wxDynamicLibrary m_dll;
106 
108  bool loadSpinit( const std::string& aFileName );
109 
112  std::string findCmPath() const;
113 
115  bool loadCodemodels( const std::string& aPath );
116 
117  // Callback functions
118  static int cbSendChar( char* what, int id, void* user );
119  static int cbSendStat( char* what, int id, void* user );
120  static int cbBGThreadRunning( bool is_running, int id, void* user );
121  static int cbControlledExit( int status, bool immediate, bool exit_upon_quit, int id, void* user );
122 
123  // Assures ngspice is in a valid state and reinitializes it if need be
124  void validate();
125 
127  bool m_error;
128 
130  static bool m_initialized;
131 
133  std::string m_netlist;
134 };
135 
136 #endif /* NGSPICE_H */
virtual ~NGSPICE()
Definition: ngspice.cpp:46
bool Run() override
>
Definition: ngspice.cpp:220
static int cbBGThreadRunning(bool is_running, int id, void *user)
Definition: ngspice.cpp:484
void validate()
Definition: ngspice.cpp:506
int(* ngSpice_Command)(char *command)
Definition: ngspice.h:90
char **(* ngSpice_AllPlots)(void)
Definition: ngspice.h:92
std::vector< double > GetRealPlot(const std::string &aName, int aMaxLen=-1) override
>
Definition: ngspice.cpp:84
static int cbControlledExit(int status, bool immediate, bool exit_upon_quit, int id, void *user)
Definition: ngspice.cpp:496
static int cbSendChar(char *what, int id, void *user)
Definition: ngspice.cpp:456
wxDynamicLibrary m_dll
Definition: ngspice.h:105
bool loadCodemodels(const std::string &aPath)
Loads codemodel files from a directory
Definition: ngspice.cpp:444
std::vector< double > GetImagPlot(const std::string &aName, int aMaxLen=-1) override
>
Definition: ngspice.cpp:116
ngSpice_Running m_ngSpice_Running
Definition: ngspice.h:103
std::vector< double > GetMagPlot(const std::string &aName, int aMaxLen=-1) override
>
Definition: ngspice.cpp:140
static bool m_initialized
NGspice should be initialized only once
Definition: ngspice.h:130
bool(* ngSpice_Running)(void)
Definition: ngspice.h:94
void Init() override
>
Definition: ngspice.cpp:51
virtual const std::string GetNetlist() const override
>
Definition: ngspice.cpp:516
NGSPICE()
Definition: ngspice.cpp:40
ngGet_Vec_Info m_ngGet_Vec_Info
Definition: ngspice.h:100
bool m_error
Error flag indicating that ngspice needs to be reloaded
Definition: ngspice.h:127
void init_dll()
Definition: ngspice.cpp:275
SIM_TYPE
Possible simulation types
Definition: sim_types.h:29
pvector_info(* ngGet_Vec_Info)(char *vecname)
Definition: ngspice.h:91
std::vector< COMPLEX > GetPlot(const std::string &aName, int aMaxLen=-1) override
>
Definition: ngspice.cpp:57
bool Stop() override
>
Definition: ngspice.cpp:227
ngSpice_Init m_ngSpice_Init
Handles to DLL functions
Definition: ngspice.h:97
std::string findCmPath() const
Checks a few different locations for codemodel files and returns one if it exists ...
Definition: ngspice.cpp:413
std::string GetXAxis(SIM_TYPE aType) const override
>
Definition: ngspice.cpp:250
std::vector< double > GetPhasePlot(const std::string &aName, int aMaxLen=-1) override
>
Definition: ngspice.cpp:167
void init()
bool LoadNetlist(const std::string &aNetlist) override
>
Definition: ngspice.cpp:194
void(* ngSpice_Init)(SendChar *, SendStat *, ControlledExit *, SendData *, SendInitData *, BGThreadRunning *, void *)
Definition: ngspice.h:87
char **(* ngSpice_AllVecs)(char *plotname)
Definition: ngspice.h:93
bool Command(const std::string &aCmd) override
>
Definition: ngspice.cpp:241
bool IsRunning() override
>
Definition: ngspice.cpp:234
ngSpice_AllVecs m_ngSpice_AllVecs
Definition: ngspice.h:102
ngSpice_Command m_ngSpice_Command
Definition: ngspice.h:99
std::string m_netlist
current netlist
Definition: ngspice.h:133
bool loadSpinit(const std::string &aFileName)
Executes commands from a file
Definition: ngspice.cpp:396
int(* ngSpice_Circ)(char **circarray)
Definition: ngspice.h:89
ngSpice_Circ m_ngSpice_Circ
Definition: ngspice.h:98
ngSpice_AllPlots m_ngSpice_AllPlots
Definition: ngspice.h:101
static int cbSendStat(char *what, int id, void *user)
Definition: ngspice.cpp:474