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