KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sim_model_ngspice.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) 2022 Mikolaj Wielgus
5 * Copyright (C) 2022-2023 KiCad Developers, see AUTHORS.txt for contributors.
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
26
27#include <boost/algorithm/string.hpp>
28#include <fmt/core.h>
29
30
31std::vector<std::string> SPICE_GENERATOR_NGSPICE::CurrentNames( const SPICE_ITEM& aItem ) const
32{
33 switch( m_model.GetTypeInfo().deviceType )
34 {
35 case SIM_MODEL::DEVICE_T::NPN:
36 case SIM_MODEL::DEVICE_T::PNP:
37 return { fmt::format( "I({}:c)", ItemName( aItem ) ),
38 fmt::format( "I({}:b)", ItemName( aItem ) ),
39 fmt::format( "I({}:e)", ItemName( aItem ) ) };
40
41 case SIM_MODEL::DEVICE_T::NJFET:
42 case SIM_MODEL::DEVICE_T::PJFET:
43 case SIM_MODEL::DEVICE_T::NMES:
44 case SIM_MODEL::DEVICE_T::PMES:
45 case SIM_MODEL::DEVICE_T::NMOS:
46 case SIM_MODEL::DEVICE_T::PMOS:
47 return { fmt::format( "I({}:d)", ItemName( aItem ) ),
48 fmt::format( "I({}:g)", ItemName( aItem ) ),
49 fmt::format( "I({}:s)", ItemName( aItem ) ) };
50
51 case SIM_MODEL::DEVICE_T::R:
52 case SIM_MODEL::DEVICE_T::C:
53 case SIM_MODEL::DEVICE_T::L:
54 case SIM_MODEL::DEVICE_T::D:
55 return SPICE_GENERATOR::CurrentNames( aItem );
56
57 default:
58 return {};
59 }
60}
61
62
64 SIM_MODEL_SPICE( aType, std::make_unique<SPICE_GENERATOR_NGSPICE>( *this ) )
65{
66 const MODEL_INFO& modelInfo = ModelInfo( getModelType() );
67
68 for( const SIM_MODEL::PARAM::INFO& paramInfo : modelInfo.instanceParams )
69 {
70 // For now, only the geometry and flags parameters.
74 {
75 AddParam( paramInfo );
76 }
77 }
78
79 for( const SIM_MODEL::PARAM::INFO& paramInfo : modelInfo.modelParams )
80 AddParam( paramInfo );
81}
82
83
84int SIM_MODEL_NGSPICE::doFindParam( const std::string& aParamName ) const
85{
86 // Special case to allow escaped model parameters (suffixed with "_")
87
88 std::vector<std::reference_wrapper<const PARAM>> params = GetParams();
89
90 for( int ii = 0; ii < (int) params.size(); ++ii )
91 {
92 const PARAM& param = params[ii];
93
94 if( param.Matches( aParamName ) || param.Matches( aParamName + "_" ) )
95 return ii;
96 }
97
98 return -1;
99}
100
101
102void SIM_MODEL_NGSPICE::SetParamFromSpiceCode( const std::string& aParamName,
103 const std::string& aValue,
105{
106 // "level" and "version" are not really parameters - they're part of the type - so silently
107 // ignore them.
108 if( boost::iequals( aParamName, "level" ) || boost::iequals( aParamName, "version" ) )
109 return;
110
111 // First we try to use the name as is. Note that you can't set instance parameters from this
112 // function, it's for ".model" cards, not for instantiations.
113
114 std::vector<std::reference_wrapper<const PARAM>> params = GetParams();
115
116 for( int ii = 0; ii < (int) params.size(); ++ii )
117 {
118 const PARAM& param = params[ii];
119
121 continue;
122
123 if( param.Matches( aParamName ) || param.Matches( aParamName + "_" ) )
124 {
125 SetParamValue( ii, aValue, aNotation );
126 return;
127 }
128 }
129
130 // One Spice param can have multiple names, we need to take this into account.
131
132 // Now we search the base model parameters without excluding superfluous parameters (which
133 // may be aliases to non-superfluous parameters).
134
135 for( const PARAM::INFO& ngspiceParamInfo : ModelInfo( getModelType() ).modelParams )
136 {
137 if( ngspiceParamInfo.Matches( aParamName ) )
138 {
139 // Find an actual parameter with the same id. Even if the ngspiceParam was
140 // superfluous, its alias target might not be.
141 for( int ii = 0; ii < (int) params.size(); ++ii )
142 {
143 const PARAM::INFO& paramInfo = params[ii].get().info;
144
145 if( paramInfo.category == PARAM::CATEGORY::SUPERFLUOUS )
146 continue;
147
148 if( paramInfo.id == ngspiceParamInfo.id )
149 {
150 SetParamValue( ii, aValue, aNotation );
151 return;
152 }
153 }
154
155 break;
156 }
157 }
158
159 if( !canSilentlyIgnoreParam( aParamName ) )
160 THROW_IO_ERROR( wxString::Format( "Unknown simulation model parameter '%s'", aParamName ) );
161}
162
163
164bool SIM_MODEL_NGSPICE::canSilentlyIgnoreParam( const std::string& aParamName )
165{
166 // Ignore the purely informative LTspice-specific parameters "mfg" and "type".
167 if( boost::iequals( aParamName, "mfg" ) || boost::iequals( aParamName, "type" ) )
168 return true;
169
170 if( GetDeviceType() == DEVICE_T::D )
171 {
172 if( boost::iequals( aParamName, "perim" )
173 || boost::iequals( aParamName, "isw" )
174 || boost::iequals( aParamName, "ns" )
175 || boost::iequals( aParamName, "rsw" )
176 || boost::iequals( aParamName, "cjsw" )
177 || boost::iequals( aParamName, "vjsw" )
178 || boost::iequals( aParamName, "mjsw" )
179 || boost::iequals( aParamName, "fcs" ) )
180 {
181 return true;
182 }
183 }
184
185 if( GetDeviceType() == DEVICE_T::NPN || GetDeviceType() == DEVICE_T::PNP )
186 {
187 // Ignore the purely informative LTspice-specific parameters "icrating" and "vceo".
188 if( boost::iequals( aParamName, "icrating" ) || boost::iequals( aParamName, "vceo" ) )
189 return true;
190 }
191
192 if( GetType() == TYPE::NPN_GUMMELPOON || GetType() == TYPE::PNP_GUMMELPOON )
193 {
194 // Ignore unused parameters.
195 if( boost::iequals( aParamName, "bvcbo" )
196 || boost::iequals( aParamName, "nbvcbo" )
197 || boost::iequals( aParamName, "tbvcbo1" )
198 || boost::iequals( aParamName, "tbvcbo2" )
199 || boost::iequals( aParamName, "bvbe" )
200 || boost::iequals( aParamName, "ibvbe" )
201 || boost::iequals( aParamName, "nbvbe" ) )
202 {
203 return true;
204 }
205 }
206
207 if( GetType() == TYPE::NMOS_VDMOS || GetType() == TYPE::PMOS_VDMOS )
208 {
209 // Ignore the purely informative LTspice-specific parameters "Vds", "Ron" and "Qg".
210 if( boost::iequals( aParamName, "vds" )
211 || boost::iequals( aParamName, "ron" )
212 || boost::iequals( aParamName, "qg" ) )
213 {
214 return true;
215 }
216 }
217
218 return false;
219}
220
221
222std::vector<std::string> SIM_MODEL_NGSPICE::GetPinNames() const
223{
224 return ModelInfo( getModelType() ).pinNames;
225}
226
227
228SIM_MODEL_NGSPICE::MODEL_TYPE SIM_MODEL_NGSPICE::getModelType() const
229{
230 switch( GetType() )
231 {
232 case TYPE::NONE: return MODEL_TYPE::NONE;
233 case TYPE::D: return MODEL_TYPE::DIODE;
234
235 case TYPE::NPN_VBIC:
236 case TYPE::PNP_VBIC: return MODEL_TYPE::VBIC;
237 case TYPE::NPN_GUMMELPOON:
238 case TYPE::PNP_GUMMELPOON: return MODEL_TYPE::BJT;
239 case TYPE::NPN_HICUM2:
240 case TYPE::PNP_HICUM2: return MODEL_TYPE::HICUM2;
241
242 case TYPE::NJFET_SHICHMANHODGES:
243 case TYPE::PJFET_SHICHMANHODGES: return MODEL_TYPE::JFET;
244 case TYPE::NJFET_PARKERSKELLERN:
245 case TYPE::PJFET_PARKERSKELLERN: return MODEL_TYPE::JFET2;
246
247 case TYPE::NMES_STATZ:
248 case TYPE::PMES_STATZ: return MODEL_TYPE::MES;
249 case TYPE::NMES_YTTERDAL:
250 case TYPE::PMES_YTTERDAL: return MODEL_TYPE::MESA;
251 case TYPE::NMES_HFET1:
252 case TYPE::PMES_HFET1: return MODEL_TYPE::HFET1;
253 case TYPE::NMES_HFET2:
254 case TYPE::PMES_HFET2: return MODEL_TYPE::HFET2;
255
256 case TYPE::NMOS_VDMOS:
257 case TYPE::PMOS_VDMOS: return MODEL_TYPE::VDMOS;
258 case TYPE::NMOS_MOS1:
259 case TYPE::PMOS_MOS1: return MODEL_TYPE::MOS1;
260 case TYPE::NMOS_MOS2:
261 case TYPE::PMOS_MOS2: return MODEL_TYPE::MOS2;
262 case TYPE::NMOS_MOS3:
263 case TYPE::PMOS_MOS3: return MODEL_TYPE::MOS3;
264 case TYPE::NMOS_BSIM1:
265 case TYPE::PMOS_BSIM1: return MODEL_TYPE::BSIM1;
266 case TYPE::NMOS_BSIM2:
267 case TYPE::PMOS_BSIM2: return MODEL_TYPE::BSIM2;
268 case TYPE::NMOS_MOS6:
269 case TYPE::PMOS_MOS6: return MODEL_TYPE::MOS6;
270 case TYPE::NMOS_BSIM3:
271 case TYPE::PMOS_BSIM3: return MODEL_TYPE::BSIM3;
272 case TYPE::NMOS_MOS9:
273 case TYPE::PMOS_MOS9: return MODEL_TYPE::MOS9;
274 case TYPE::NMOS_B4SOI:
275 case TYPE::PMOS_B4SOI: return MODEL_TYPE::B4SOI;
276 case TYPE::NMOS_BSIM4:
277 case TYPE::PMOS_BSIM4: return MODEL_TYPE::BSIM4;
278 case TYPE::NMOS_B3SOIFD:
279 case TYPE::PMOS_B3SOIFD: return MODEL_TYPE::B3SOIFD;
280 case TYPE::NMOS_B3SOIDD:
281 case TYPE::PMOS_B3SOIDD: return MODEL_TYPE::B3SOIDD;
282 case TYPE::NMOS_B3SOIPD:
283 case TYPE::PMOS_B3SOIPD: return MODEL_TYPE::B3SOIPD;
284 case TYPE::NMOS_HISIM2:
285 case TYPE::PMOS_HISIM2: return MODEL_TYPE::HISIM2;
286 case TYPE::NMOS_HISIMHV1:
287 case TYPE::PMOS_HISIMHV1: return MODEL_TYPE::HISIMHV1;
288 case TYPE::NMOS_HISIMHV2:
289 case TYPE::PMOS_HISIMHV2: return MODEL_TYPE::HISIMHV2;
290
291 default:
292 wxFAIL_MSG( "Unhandled SIM_MODEL type in SIM_MODEL_NGSPICE" );
293 return MODEL_TYPE::NONE;
294 }
295}
296
297
298static std::unique_ptr<NGSPICE_MODEL_INFO_MAP> s_ModelInfoMap;
299
300const SIM_MODEL_NGSPICE::MODEL_INFO& SIM_MODEL_NGSPICE::ModelInfo( MODEL_TYPE aType )
301{
302 if( !s_ModelInfoMap )
303 s_ModelInfoMap = std::make_unique<NGSPICE_MODEL_INFO_MAP>();
304
305 return s_ModelInfoMap->modelInfos.at( aType );
306}
307
308
310{
311 modelInfos[SIM_MODEL_NGSPICE::MODEL_TYPE::NONE] = {};
312 addBJT();
313 addBSIM1();
314 addBSIM2();
315 addBSIM3();
316 addBSIM4();
317 addB3SOI();
318 addB4SOI();
319 addDIODE();
320 addHFET();
321 addHICUM2();
322 addHSIM();
323 addJFET();
324 addMES();
325 addMOS();
326 addMOS6();
327 addMOS9();
328 addVBIC();
329}
bool canSilentlyIgnoreParam(const std::string &aParamName)
static const MODEL_INFO & ModelInfo(MODEL_TYPE aType)
std::vector< std::string > GetPinNames() const override
MODEL_TYPE getModelType() const
void SetParamFromSpiceCode(const std::string &aParamName, const std::string &aValue, SIM_VALUE_GRAMMAR::NOTATION aNotation) override
int doFindParam(const std::string &aParamName) const override
SIM_MODEL_NGSPICE(TYPE aType)
void AddParam(const PARAM::INFO &aInfo)
Definition: sim_model.cpp:726
DEVICE_T GetDeviceType() const
Definition: sim_model.h:468
INFO GetTypeInfo() const
Definition: sim_model.h:466
void SetParamValue(int aParamIndex, const std::string &aValue, SIM_VALUE::NOTATION aNotation=SIM_VALUE::NOTATION::SI)
Definition: sim_model.cpp:854
std::vector< std::reference_wrapper< const PARAM > > GetParams() const
Definition: sim_model.cpp:822
TYPE GetType() const
Definition: sim_model.h:469
std::vector< std::string > CurrentNames(const SPICE_ITEM &aItem) const override
virtual std::string ItemName(const SPICE_ITEM &aItem) const
const SIM_MODEL & m_model
virtual std::vector< std::string > CurrentNames(const SPICE_ITEM &aItem) const
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
STL namespace.
SIM_MODEL::TYPE TYPE
Definition: sim_model.cpp:53
static std::unique_ptr< NGSPICE_MODEL_INFO_MAP > s_ModelInfoMap
std::unordered_map< MODEL_TYPE, MODEL_INFO > modelInfos
bool Matches(const std::string &aName) const
Definition: sim_model.h:395
const INFO & info
Definition: sim_model.h:401