KiCad PCB EDA Suite
Loading...
Searching...
No Matches
spice_model_parser.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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <wx/log.h>
22
23#include <ki_exception.h>
25#include <sim/spice_grammar.h>
26#include <sim/sim_model_spice.h>
28
29#include <boost/algorithm/string/case_conv.hpp>
30#include <boost/algorithm/string/predicate.hpp>
31#include <pegtl.hpp>
32#include <pegtl/contrib/parse_tree.hpp>
33
34
40static const wxChar traceSpiceModelParser[] = wxT( "KICAD_SPICE_MODEL_PARSER" );
41
42
44{
45 using namespace SPICE_GRAMMAR;
46
47 template <typename Rule> struct spiceUnitSelector : std::false_type {};
48
49 template <> struct spiceUnitSelector<dotModelAko> : std::true_type {};
50 template <> struct spiceUnitSelector<dotModelCPL> : std::true_type {};
51 template <> struct spiceUnitSelector<dotModel> : std::true_type {};
52 template <> struct spiceUnitSelector<modelName> : std::true_type {};
53 template <> struct spiceUnitSelector<dotModelType> : std::true_type {};
54 template <> struct spiceUnitSelector<param> : std::true_type {};
55 template <> struct spiceUnitSelector<paramValue> : std::true_type {};
56
57 template <> struct spiceUnitSelector<dotSubckt> : std::true_type {};
58}
59
60
61bool SPICE_MODEL_PARSER::ReadType( const SIM_LIBRARY_SPICE& aLibrary, const std::string& aSpiceCode,
62 SIM_MODEL::TYPE* aType, bool aFirstPass )
63{
64 *aType = SIM_MODEL::TYPE::NONE;
65
66 tao::pegtl::string_input<> in( aSpiceCode, "Spice_Code" );
67 std::unique_ptr<tao::pegtl::parse_tree::node> root;
68
69 try
70 {
71 root = tao::pegtl::parse_tree::parse<SIM_MODEL_SPICE_PARSER::spiceUnitGrammar,
73 tao::pegtl::nothing,
75 ( in );
76 }
77 catch( const tao::pegtl::parse_error& e )
78 {
79 wxLogTrace( traceSpiceModelParser, wxS( "%s" ), e.what() );
80 return true;
81 }
82
83 for( const auto& node : root->children )
84 {
85 if( node->is_type<SIM_MODEL_SPICE_PARSER::dotModelAko>() )
86 {
87 // The first pass of model loading is done in parallel, so we can't (yet) look
88 // up our reference model.
89 if( aFirstPass )
90 return false;
91
92 std::string modelName = node->children.at( 0 )->string();
93 std::string akoName = node->children.at( 1 )->string();
94 const SIM_MODEL* sourceModel = aLibrary.FindModel( akoName );
95
96 if( !sourceModel )
97 {
98 THROW_IO_ERROR( wxString::Format( _( "Could not find parent model '%s' for \"A Kind Of\" model '%s'" ),
99 akoName,
100 modelName ) );
101 }
102
103 *aType = sourceModel->GetType();
104 return true;
105 }
106 else if( node->is_type<SIM_MODEL_SPICE_PARSER::dotModelCPL>() )
107 {
108 // CPL (Coupled Multiconductor Line) has no native KiCad model type.
109 // Treat it as raw SPICE so the code is passed through to ngspice.
110 *aType = SIM_MODEL::TYPE::RAWSPICE;
111 return true;
112 }
113 else if( node->is_type<SIM_MODEL_SPICE_PARSER::dotModel>() )
114 {
115 std::string paramName;
116 std::string typeString;
117 std::string level;
118 std::string version;
119
120 for( const auto& subnode : node->children )
121 {
122 if( subnode->is_type<SIM_MODEL_SPICE_PARSER::modelName>() )
123 {
124 // Do nothing.
125 }
126 else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::dotModelType>() )
127 {
128 typeString = subnode->string();
129 SIM_MODEL::TYPE type = ReadTypeFromSpiceStrings( typeString );
130
131 if( type != SIM_MODEL::TYPE::RAWSPICE )
132 {
133 *aType = type;
134 return true;
135 }
136 }
137 else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::param>() )
138 {
139 paramName = subnode->string();
140 }
141 else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::paramValue>() )
142 {
143 wxASSERT( paramName != "" );
144
145 if( paramName == "level" )
146 level = subnode->string();
147 else if( paramName == "version" )
148 version = subnode->string();
149 }
150 else
151 {
152 wxFAIL_MSG( "Unhandled parse tree subnode" );
153 return true;
154 }
155 }
156
157 // Type was not determined from Spice type string alone, so now we take `level` and
158 // `version` variables into account too. This is suboptimal since we read the model
159 // twice this way, and moreover the code is now somewhat duplicated.
160
161 *aType = ReadTypeFromSpiceStrings( typeString, level, version, false );
162 return true;
163 }
164 else if( node->is_type<SIM_MODEL_SPICE_PARSER::dotSubckt>() )
165 {
166 // The first pass of model loading is done in parallel, so we can't (yet) refer to
167 // any other models in a subcircuit.
168 if( aFirstPass )
169 return false;
170
171 *aType = SIM_MODEL::TYPE::SUBCKT;
172 return true;
173 }
174 else
175 {
176 wxFAIL_MSG( "Unhandled parse tree node" );
177 return true;
178 }
179 }
180
181 wxFAIL_MSG( "Could not derive type from SPICE code" );
182 return true;
183}
184
185
187 const std::string& aSpiceCode )
188{
189 // The default behavior is to treat the Spice param=value pairs as the model parameters and
190 // values (for many models the correspondence is not exact, so this function is overridden).
191
192 tao::pegtl::string_input<> in( aSpiceCode, "Spice_Code" );
193 std::unique_ptr<tao::pegtl::parse_tree::node> root;
194
195 try
196 {
197 root = tao::pegtl::parse_tree::parse<SIM_MODEL_SPICE_PARSER::spiceUnitGrammar,
199 tao::pegtl::nothing,
201 ( in );
202 }
203 catch( tao::pegtl::parse_error& e )
204 {
205 THROW_IO_ERROR( e.what() );
206 }
207
208 for( const auto& node : root->children )
209 {
210 if( node->is_type<SIM_MODEL_SPICE_PARSER::dotModelAko>() )
211 {
212 std::string modelName = node->children.at( 0 )->string();
213 std::string akoName = node->children.at( 1 )->string();
214
215 const SIM_MODEL* sourceModel = aLibrary.FindModel( akoName );
216
217 if( !sourceModel )
218 {
219 THROW_IO_ERROR( wxString::Format( _( "Could not find parent model '%s' for \"A Kind Of\" model '%s'" ),
220 akoName,
221 modelName ) );
222 }
223
224 for( int i = 0; i < static_cast<int>( sourceModel->GetParamCount() ); ++i )
225 m_model.SetParamValue( i, sourceModel->GetParam( i ).value );
226
227 std::string paramName;
228
229 for( const auto& subnode : node->children )
230 {
231 if( subnode->is_type<SIM_MODEL_SPICE_PARSER::modelName>() )
232 {
233 // Do nothing.
234 }
235 else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::dotModelType>() )
236 {
237 // Do nothing.
238 }
239 else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::param>() )
240 {
241 paramName = subnode->string();
242 }
243 else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::paramValue>() )
244 {
245 wxASSERT( paramName != "" );
246
247 m_model.SetParamFromSpiceCode( paramName, subnode->string() );
248 }
249 else
250 {
251 wxFAIL_MSG( "Unhandled parse tree subnode" );
252 }
253 }
254 }
255 else if( node->is_type<SIM_MODEL_SPICE_PARSER::dotModel>() )
256 {
257 std::string modelName;
258 std::string paramName;
259
260 for( const auto& subnode : node->children )
261 {
262 if( subnode->is_type<SIM_MODEL_SPICE_PARSER::modelName>() )
263 {
264 modelName = subnode->string();
265 }
266 else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::dotModelType>() )
267 {
268 // Do nothing.
269 }
270 else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::param>() )
271 {
272 paramName = subnode->string();
273 }
274 else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::paramValue>() )
275 {
276 wxASSERT( paramName != "" );
277
278 m_model.SetParamFromSpiceCode( paramName, subnode->string() );
279 }
280 else
281 {
282 wxFAIL_MSG( "Unhandled parse tree subnode" );
283 }
284 }
285 }
286 else
287 {
288 wxFAIL_MSG( "Unhandled parse tree node" );
289 }
290 }
291
292 m_model.m_spiceCode = aSpiceCode;
293}
294
295
296SIM_MODEL::TYPE SPICE_MODEL_PARSER::ReadTypeFromSpiceStrings( const std::string& aTypeString,
297 const std::string& aLevel,
298 const std::string& aVersion,
299 bool aSkipDefaultLevel )
300{
301 wxString input_level = wxString( aLevel ).BeforeFirst( '.' );
302 wxString input_type( aTypeString );
303 bool vdmos = false;
304 bool pchan = false;
305
306 input_type.UpperCase();
307
308 if( input_type.StartsWith( wxS( "VDMOS" ) ) )
309 {
310 vdmos = true;
311 pchan = input_type.Contains( wxS( "PCHAN" ) );
312 }
313
314 for( SIM_MODEL::TYPE candidate : SIM_MODEL::TYPE_ITERATOR() )
315 {
316 wxString candidate_type = SIM_MODEL::SpiceInfo( candidate ).modelType;
317
318 if( candidate_type.IsEmpty() )
319 continue;
320
321 if( candidate_type.StartsWith( wxS( "VDMOS" ) ) && vdmos )
322 {
323 if( vdmos && pchan && candidate_type.EndsWith( wxS( "PCHAN" ) ) )
324 return candidate;
325 else if( vdmos && !pchan && candidate_type.EndsWith( wxS( "NCHAN" ) ) )
326 return candidate;
327 }
328 else if( input_type.StartsWith( candidate_type ) )
329 {
330 if( SIM_MODEL::SpiceInfo( candidate ).version != aVersion )
331 continue;
332
333 if( SIM_MODEL::SpiceInfo( candidate ).level == input_level )
334 return candidate;
335
336 if( aSkipDefaultLevel )
337 continue;
338
339 if( SIM_MODEL::SpiceInfo( candidate ).isDefaultLevel && aLevel == "" )
340 return candidate;
341 }
342 }
343
344 // If the type string is not recognized, demote to a raw Spice element. This way the user won't
345 // have an error if there is a type KiCad does not recognize.
346 return SIM_MODEL::TYPE::RAWSPICE;
347}
348
SIM_MODEL * FindModel(const std::string &aModelName) const
static SPICE_INFO SpiceInfo(TYPE aType)
virtual const PARAM & GetParam(unsigned aParamIndex) const
int GetParamCount() const
Definition sim_model.h:488
TYPE GetType() const
Definition sim_model.h:471
virtual void ReadModel(const SIM_LIBRARY_SPICE &aLibrary, const std::string &aSpiceCode)
static bool ReadType(const SIM_LIBRARY_SPICE &aLibrary, const std::string &aSpiceCode, SIM_MODEL::TYPE *aType, bool aFirstPass)
static SIM_MODEL::TYPE ReadTypeFromSpiceStrings(const std::string &aTypeString, const std::string &aLevel="", const std::string &aVersion="", bool aSkipDefaultLevel=true)
SIM_MODEL_SPICE & m_model
#define _(s)
static const wxChar traceSpiceModelParser[]
Flag to enable SPICE model parser debugging output.
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
must_if< error >::control< Rule > control
std::string value
Definition sim_model.h:398
std::string modelType
Definition sim_model.h:303