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 for( int ii = 0; ii < (int) GetParamCount(); ++ii )
87 {
88 const PARAM& param = GetParam( ii );
89
90 if( param.Matches( aParamName ) )
91 return ii;
92 }
93
94 // Look for escaped param names as a second pass (as they're less common)
95 for( int ii = 0; ii < (int) GetParamCount(); ++ii )
96 {
97 const PARAM& param = GetParam( ii );
98
99 if( !param.info.name.ends_with( '_' ) )
100 continue;
101
102 if( param.Matches( aParamName + "_" ) )
103 return ii;
104 }
105
106 return -1;
107}
108
109
110void SIM_MODEL_NGSPICE::SetParamFromSpiceCode( const std::string& aParamName,
111 const std::string& aValue,
113{
114 // "level" and "version" are not really parameters - they're part of the type - so silently
115 // ignore them.
116 if( boost::iequals( aParamName, "level" ) || boost::iequals( aParamName, "version" ) )
117 return;
118
119 // First we try to use the name as is. Note that you can't set instance parameters from this
120 // function, it's for ".model" cards, not for instantiations.
121
122 for( int ii = 0; ii < (int) GetParamCount(); ++ii )
123 {
124 const PARAM& param = GetParam( ii );
125
127 continue;
128
129 if( param.Matches( aParamName ) )
130 {
131 SetParamValue( ii, aValue, aNotation );
132 return;
133 }
134 }
135
136 // Look for escaped param names as a second pass (as they're less common)
137 for( int ii = 0; ii < (int) GetParamCount(); ++ii )
138 {
139 const PARAM& param = GetParam( ii );
140
142 continue;
143
144 if( !param.info.name.ends_with( '_' ) )
145 continue;
146
147 if( param.Matches( aParamName + "_" ) )
148 {
149 SetParamValue( ii, aValue, aNotation );
150 return;
151 }
152 }
153
154 // One Spice param can have multiple names, we need to take this into account.
155
156 // Now we search the base model parameters without excluding superfluous parameters (which
157 // may be aliases to non-superfluous parameters).
158
159 for( const PARAM::INFO& ngspiceParamInfo : ModelInfo( getModelType() ).modelParams )
160 {
161 if( ngspiceParamInfo.Matches( aParamName ) )
162 {
163 // Find an actual parameter with the same id. Even if the ngspiceParam was
164 // superfluous, its alias target might not be.
165 for( int ii = 0; ii < (int) GetParamCount(); ++ii )
166 {
167 const PARAM::INFO& paramInfo = GetParam( ii ).info;
168
169 if( paramInfo.category == PARAM::CATEGORY::SUPERFLUOUS )
170 continue;
171
172 if( paramInfo.id == ngspiceParamInfo.id )
173 {
174 SetParamValue( ii, aValue, aNotation );
175 return;
176 }
177 }
178
179 break;
180 }
181 }
182
183 if( !canSilentlyIgnoreParam( aParamName ) )
184 THROW_IO_ERROR( wxString::Format( "Unknown simulation model parameter '%s'", aParamName ) );
185}
186
187
188bool SIM_MODEL_NGSPICE::canSilentlyIgnoreParam( const std::string& aParamName )
189{
190 // Ignore the purely informative LTspice-specific parameters "mfg" and "type".
191 if( boost::iequals( aParamName, "mfg" ) || boost::iequals( aParamName, "type" ) )
192 return true;
193
194 if( GetDeviceType() == DEVICE_T::D )
195 {
196 if( boost::iequals( aParamName, "perim" )
197 || boost::iequals( aParamName, "isw" )
198 || boost::iequals( aParamName, "ns" )
199 || boost::iequals( aParamName, "rsw" )
200 || boost::iequals( aParamName, "cjsw" )
201 || boost::iequals( aParamName, "vjsw" )
202 || boost::iequals( aParamName, "mjsw" )
203 || boost::iequals( aParamName, "fcs" ) )
204 {
205 return true;
206 }
207 }
208
209 if( GetDeviceType() == DEVICE_T::NPN || GetDeviceType() == DEVICE_T::PNP )
210 {
211 // Ignore the purely informative LTspice-specific parameters "icrating" and "vceo".
212 if( boost::iequals( aParamName, "icrating" ) || boost::iequals( aParamName, "vceo" ) )
213 return true;
214 }
215
216 if( GetType() == TYPE::NPN_GUMMELPOON || GetType() == TYPE::PNP_GUMMELPOON )
217 {
218 // Ignore unused parameters.
219 if( boost::iequals( aParamName, "bvcbo" )
220 || boost::iequals( aParamName, "nbvcbo" )
221 || boost::iequals( aParamName, "tbvcbo1" )
222 || boost::iequals( aParamName, "tbvcbo2" )
223 || boost::iequals( aParamName, "bvbe" )
224 || boost::iequals( aParamName, "ibvbe" )
225 || boost::iequals( aParamName, "nbvbe" ) )
226 {
227 return true;
228 }
229 }
230
231 if( GetType() == TYPE::NMOS_VDMOS || GetType() == TYPE::PMOS_VDMOS )
232 {
233 // Ignore the purely informative LTspice-specific parameters "Vds", "Ron" and "Qg".
234 if( boost::iequals( aParamName, "vds" )
235 || boost::iequals( aParamName, "ron" )
236 || boost::iequals( aParamName, "qg" ) )
237 {
238 return true;
239 }
240 }
241
242 return false;
243}
244
245
246std::vector<std::string> SIM_MODEL_NGSPICE::GetPinNames() const
247{
248 return ModelInfo( getModelType() ).pinNames;
249}
250
251
252SIM_MODEL_NGSPICE::MODEL_TYPE SIM_MODEL_NGSPICE::getModelType() const
253{
254 switch( GetType() )
255 {
256 case TYPE::NONE: return MODEL_TYPE::NONE;
257 case TYPE::D: return MODEL_TYPE::DIODE;
258
259 case TYPE::NPN_VBIC:
260 case TYPE::PNP_VBIC: return MODEL_TYPE::VBIC;
261 case TYPE::NPN_GUMMELPOON:
262 case TYPE::PNP_GUMMELPOON: return MODEL_TYPE::BJT;
263 case TYPE::NPN_HICUM2:
264 case TYPE::PNP_HICUM2: return MODEL_TYPE::HICUM2;
265
266 case TYPE::NJFET_SHICHMANHODGES:
267 case TYPE::PJFET_SHICHMANHODGES: return MODEL_TYPE::JFET;
268 case TYPE::NJFET_PARKERSKELLERN:
269 case TYPE::PJFET_PARKERSKELLERN: return MODEL_TYPE::JFET2;
270
271 case TYPE::NMES_STATZ:
272 case TYPE::PMES_STATZ: return MODEL_TYPE::MES;
273 case TYPE::NMES_YTTERDAL:
274 case TYPE::PMES_YTTERDAL: return MODEL_TYPE::MESA;
275 case TYPE::NMES_HFET1:
276 case TYPE::PMES_HFET1: return MODEL_TYPE::HFET1;
277 case TYPE::NMES_HFET2:
278 case TYPE::PMES_HFET2: return MODEL_TYPE::HFET2;
279
280 case TYPE::NMOS_VDMOS:
281 case TYPE::PMOS_VDMOS: return MODEL_TYPE::VDMOS;
282 case TYPE::NMOS_MOS1:
283 case TYPE::PMOS_MOS1: return MODEL_TYPE::MOS1;
284 case TYPE::NMOS_MOS2:
285 case TYPE::PMOS_MOS2: return MODEL_TYPE::MOS2;
286 case TYPE::NMOS_MOS3:
287 case TYPE::PMOS_MOS3: return MODEL_TYPE::MOS3;
288 case TYPE::NMOS_BSIM1:
289 case TYPE::PMOS_BSIM1: return MODEL_TYPE::BSIM1;
290 case TYPE::NMOS_BSIM2:
291 case TYPE::PMOS_BSIM2: return MODEL_TYPE::BSIM2;
292 case TYPE::NMOS_MOS6:
293 case TYPE::PMOS_MOS6: return MODEL_TYPE::MOS6;
294 case TYPE::NMOS_BSIM3:
295 case TYPE::PMOS_BSIM3: return MODEL_TYPE::BSIM3;
296 case TYPE::NMOS_MOS9:
297 case TYPE::PMOS_MOS9: return MODEL_TYPE::MOS9;
298 case TYPE::NMOS_B4SOI:
299 case TYPE::PMOS_B4SOI: return MODEL_TYPE::B4SOI;
300 case TYPE::NMOS_BSIM4:
301 case TYPE::PMOS_BSIM4: return MODEL_TYPE::BSIM4;
302 case TYPE::NMOS_B3SOIFD:
303 case TYPE::PMOS_B3SOIFD: return MODEL_TYPE::B3SOIFD;
304 case TYPE::NMOS_B3SOIDD:
305 case TYPE::PMOS_B3SOIDD: return MODEL_TYPE::B3SOIDD;
306 case TYPE::NMOS_B3SOIPD:
307 case TYPE::PMOS_B3SOIPD: return MODEL_TYPE::B3SOIPD;
308 case TYPE::NMOS_HISIM2:
309 case TYPE::PMOS_HISIM2: return MODEL_TYPE::HISIM2;
310 case TYPE::NMOS_HISIMHV1:
311 case TYPE::PMOS_HISIMHV1: return MODEL_TYPE::HISIMHV1;
312 case TYPE::NMOS_HISIMHV2:
313 case TYPE::PMOS_HISIMHV2: return MODEL_TYPE::HISIMHV2;
314
315 default:
316 wxFAIL_MSG( "Unhandled SIM_MODEL type in SIM_MODEL_NGSPICE" );
317 return MODEL_TYPE::NONE;
318 }
319}
320
321
322static std::unique_ptr<NGSPICE_MODEL_INFO_MAP> s_ModelInfoMap;
323
324const SIM_MODEL_NGSPICE::MODEL_INFO& SIM_MODEL_NGSPICE::ModelInfo( MODEL_TYPE aType )
325{
326 if( !s_ModelInfoMap )
327 s_ModelInfoMap = std::make_unique<NGSPICE_MODEL_INFO_MAP>();
328
329 return s_ModelInfoMap->modelInfos.at( aType );
330}
331
332
334{
335 modelInfos[SIM_MODEL_NGSPICE::MODEL_TYPE::NONE] = {};
336 addBJT();
337 addBSIM1();
338 addBSIM2();
339 addBSIM3();
340 addBSIM4();
341 addB3SOI();
342 addB4SOI();
343 addDIODE();
344 addHFET();
345 addHICUM2();
346 addHSIM();
347 addJFET();
348 addMES();
349 addMOS();
350 addMOS6();
351 addMOS9();
352 addVBIC();
353}
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:725
virtual const PARAM & GetParam(unsigned aParamIndex) const
Definition: sim_model.cpp:785
int GetParamCount() const
Definition: sim_model.h:481
DEVICE_T GetDeviceType() const
Definition: sim_model.h:463
INFO GetTypeInfo() const
Definition: sim_model.h:461
void SetParamValue(int aParamIndex, const std::string &aValue, SIM_VALUE::NOTATION aNotation=SIM_VALUE::NOTATION::SI)
Definition: sim_model.cpp:841
TYPE GetType() const
Definition: sim_model.h:464
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