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