KiCad PCB EDA Suite
ngspice_circuit_model.cpp
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) 1992-2023 KiCad Developers, see AUTHORS.TXT for contributors.
6 * @author Maciej Suminski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 3
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * https://www.gnu.org/licenses/gpl-3.0.html
21 * or you may search the http://www.gnu.org website for the version 3 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
27#include <macros.h> // for TO_UTF8 def
28#include <wx/regex.h>
29#include <wx/tokenzr.h>
30#include <locale_io.h>
31
32
34 wxString& aSignal ) const
35{
36 static wxString BRANCH( wxS( "#branch" ) );
37 // See ngspice manual chapt. 31.1 "Accessing internal device parameters"
38 static wxRegEx internalDevParameter( wxS( "^@(\\w*[\\.\\w+]*)\\[(\\w*)\\]$" ), wxRE_ADVANCED );
39
40 wxString vector( aVector );
41
42 if( !internalDevParameter.Matches( vector ) )
43 {
44 if( vector.EndsWith( BRANCH ) )
45 {
46 aSignal = wxT( "I(" ) + vector.Left( vector.Length() - BRANCH.Length() ) + wxT( ")" );
47 return SPT_CURRENT;
48 }
49 else
50 {
51 aSignal = wxT( "V(" ) + vector + wxT( ")" );
52 return SPT_VOLTAGE;
53 }
54 }
55 else
56 {
57 wxString paramType = internalDevParameter.GetMatch( vector, 2 );
58
59 if( paramType.Lower()[0] == 'i' )
60 {
61 // this is a branch current
62 paramType[0] = 'I';
63 aSignal = paramType + wxT( "(" );
64 aSignal += internalDevParameter.GetMatch( vector, 1 ).Upper() + wxT( ")" );
65 return SPT_CURRENT;
66 }
67 else
68 {
69 return SPT_UNKNOWN;
70 }
71 }
72}
73
74
76{
77 wxString simCmd;
78
79 ReadDirectives( 0 );
80
81 for( const wxString& directive : GetDirectives() )
82 {
83 if( IsSimCommand( directive ) )
84 simCmd += wxString::Format( wxT( "%s\r\n" ), directive );
85 }
86
87 return simCmd;
88}
89
90
92{
94}
95
96
98{
99 wxString cmd = aCmd.Lower().Trim();
100
101 if( cmd.StartsWith( wxT( ".ac" ) ) )
102 return ST_AC;
103 else if( cmd.StartsWith( wxT( ".dc" ) ) )
104 return ST_DC;
105 else if( cmd.StartsWith( wxT( ".tran" ) ) )
106 return ST_TRANSIENT;
107 else if( cmd == wxT( ".op" ) )
108 return ST_OP;
109 else if( cmd.StartsWith( wxT( ".disto" ) ) )
110 return ST_DISTORTION;
111 else if( cmd.StartsWith( wxT( ".noise" ) ) )
112 return ST_NOISE;
113 else if( cmd.StartsWith( wxT( ".pz" ) ) )
114 return ST_POLE_ZERO;
115 else if( cmd.StartsWith( wxT( ".sens" ) ) )
116 return ST_SENSITIVITY;
117 else if( cmd.StartsWith( wxT( ".tf" ) ) )
118 return ST_TRANS_FUNC;
119 else
120 return ST_UNKNOWN;
121}
122
123
124bool NGSPICE_CIRCUIT_MODEL::ParseDCCommand( const wxString& aCmd, SPICE_DC_PARAMS* aSource1,
125 SPICE_DC_PARAMS* aSource2 )
126{
127 if( !aCmd.Lower().StartsWith( ".dc" ) )
128 return false;
129
130 wxString cmd = aCmd.Mid( 3 ).Trim().Trim( false );
131
132 wxStringTokenizer tokens( cmd );
133
134 size_t num = tokens.CountTokens();
135
136 if( num != 4 && num != 8 )
137 return false;
138
139 aSource1->m_source = tokens.GetNextToken();
140 aSource1->m_vstart = SPICE_VALUE( tokens.GetNextToken() );
141 aSource1->m_vend = SPICE_VALUE( tokens.GetNextToken() );
142 aSource1->m_vincrement = SPICE_VALUE( tokens.GetNextToken() );
143
144 if( num == 8 )
145 {
146 aSource2->m_source = tokens.GetNextToken();
147 aSource2->m_vstart = SPICE_VALUE( tokens.GetNextToken() );
148 aSource2->m_vend = SPICE_VALUE( tokens.GetNextToken() );
149 aSource2->m_vincrement = SPICE_VALUE( tokens.GetNextToken() );
150 }
151
152 return true;
153}
154
155
157 unsigned aNetlistOptions ) const
158{
159 if( GetSimCommandOverride().IsEmpty() )
160 aNetlistOptions |= OPTION_SIM_COMMAND;
161
162 NETLIST_EXPORTER_SPICE::WriteDirectives( aFormatter, aNetlistOptions );
163
164 if( !GetSimCommandOverride().IsEmpty() )
165 aFormatter.Print( 0, "%s\n", TO_UTF8( GetSimCommandOverride() ) );
166}
void ReadDirectives(unsigned aNetlistOptions)
virtual void WriteDirectives(OUTPUTFORMATTER &candidate, unsigned aNetlistOptions) const
const std::vector< wxString > & GetDirectives()
SIM_TRACE_TYPE VectorToSignal(const std::string &aVector, wxString &aSignal) const
Return name of Spice dataset for a specific trace.
SIM_TYPE GetSimType()
Return simulation type basing on the simulation command directives.
static SIM_TYPE CommandToSimType(const wxString &aCmd)
Return simulation type basing on a simulation command directive.
bool ParseDCCommand(const wxString &aCmd, SPICE_DC_PARAMS *aSource1, SPICE_DC_PARAMS *aSource2)
Parse a two-source .dc command directive into its symbols.
void WriteDirectives(OUTPUTFORMATTER &aFormatter, unsigned aNetlistOptions) const override
wxString GetSimCommandOverride() const
Return the simulation command directive if stored separately (not as a sheet directive).
wxString GetSchTextSimCommand()
Return simulation command directives placed in schematic sheets (if any).
static bool IsSimCommand(const wxString &aCmd)
Determine if a directive is a simulation command.
wxString GetSimCommand()
Return the command directive that is in use (either from the sheet or from m_simCommand)
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:310
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:433
Helper class to recognize Spice formatted values.
Definition: spice_value.h:56
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
SIM_TRACE_TYPE
Definition: sim_types.h:47
@ SPT_UNKNOWN
Definition: sim_types.h:63
@ SPT_VOLTAGE
Definition: sim_types.h:49
@ SPT_CURRENT
Definition: sim_types.h:50
SIM_TYPE
< Possible simulation types
Definition: sim_types.h:32
@ ST_TRANS_FUNC
Definition: sim_types.h:41
@ ST_TRANSIENT
Definition: sim_types.h:42
@ ST_DISTORTION
Definition: sim_types.h:36
@ ST_UNKNOWN
Definition: sim_types.h:33
@ ST_NOISE
Definition: sim_types.h:37
@ ST_AC
Definition: sim_types.h:34
@ ST_POLE_ZERO
Definition: sim_types.h:39
@ ST_DC
Definition: sim_types.h:35
@ ST_OP
Definition: sim_types.h:38
@ ST_SENSITIVITY
Definition: sim_types.h:40