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, 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( _( "Could not find parent model '%s' for \"A Kind Of\" model '%s'" ),
102 akoName,
103 modelName ) );
104 }
105
106 *aType = sourceModel->GetType();
107 return true;
108 }
109 else if( node->is_type<SIM_MODEL_SPICE_PARSER::dotModel>() )
110 {
111 std::string paramName;
112 std::string typeString;
113 std::string level;
114 std::string version;
115
116 for( const auto& subnode : node->children )
117 {
118 if( subnode->is_type<SIM_MODEL_SPICE_PARSER::modelName>() )
119 {
120 // Do nothing.
121 }
122 else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::dotModelType>() )
123 {
124 typeString = subnode->string();
125 SIM_MODEL::TYPE type = ReadTypeFromSpiceStrings( typeString );
126
127 if( type != SIM_MODEL::TYPE::RAWSPICE )
128 {
129 *aType = type;
130 return true;
131 }
132 }
133 else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::param>() )
134 {
135 paramName = subnode->string();
136 }
137 else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::paramValue>() )
138 {
139 wxASSERT( paramName != "" );
140
141 if( paramName == "level" )
142 level = subnode->string();
143 else if( paramName == "version" )
144 version = subnode->string();
145 }
146 else
147 {
148 wxFAIL_MSG( "Unhandled parse tree subnode" );
149 return true;
150 }
151 }
152
153 // Type was not determined from Spice type string alone, so now we take `level` and
154 // `version` variables into account too. This is suboptimal since we read the model
155 // twice this way, and moreover the code is now somewhat duplicated.
156
157 *aType = ReadTypeFromSpiceStrings( typeString, level, version, false );
158 return true;
159 }
160 else if( node->is_type<SIM_MODEL_SPICE_PARSER::dotSubckt>() )
161 {
162 // The first pass of model loading is done in parallel, so we can't (yet) refer to
163 // any other models in a subcircuit.
164 if( aFirstPass )
165 return false;
166
167 *aType = SIM_MODEL::TYPE::SUBCKT;
168 return true;
169 }
170 else
171 {
172 wxFAIL_MSG( "Unhandled parse tree node" );
173 return true;
174 }
175 }
176
177 wxFAIL_MSG( "Could not derive type from SPICE code" );
178 return true;
179}
180
181
183 const std::string& aSpiceCode )
184{
185 // The default behavior is to treat the Spice param=value pairs as the model parameters and
186 // values (for many models the correspondence is not exact, so this function is overridden).
187
188 tao::pegtl::string_input<> in( aSpiceCode, "Spice_Code" );
189 std::unique_ptr<tao::pegtl::parse_tree::node> root;
190
191 try
192 {
193 root = tao::pegtl::parse_tree::parse<SIM_MODEL_SPICE_PARSER::spiceUnitGrammar,
195 tao::pegtl::nothing,
197 ( in );
198 }
199 catch( tao::pegtl::parse_error& e )
200 {
201 THROW_IO_ERROR( e.what() );
202 }
203
204 for( const auto& node : root->children )
205 {
206 if( node->is_type<SIM_MODEL_SPICE_PARSER::dotModelAko>() )
207 {
208 std::string modelName = node->children.at( 0 )->string();
209 std::string akoName = node->children.at( 1 )->string();
210
211 const SIM_MODEL* sourceModel = aLibrary.FindModel( akoName );
212
213 if( !sourceModel )
214 {
215 THROW_IO_ERROR( wxString::Format( _( "Could not find parent model '%s' 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:57
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:727
int GetParamCount() const
Definition: sim_model.h:477
void SetParamValue(int aParamIndex, const std::string &aValue, SIM_VALUE::NOTATION aNotation=SIM_VALUE::NOTATION::SI)
Definition: sim_model.cpp:783
TYPE GetType() const
Definition: sim_model.h:460
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