KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sim_model_subckt.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
26
27#include <ki_exception.h>
28#include <fmt/core.h>
29#include <pegtl.hpp>
30#include <pegtl/contrib/parse_tree.hpp>
31#include <sim/spice_grammar.h>
32
33
35{
36 using namespace SPICE_GRAMMAR;
37
38 template <typename Rule> struct spiceUnitSelector : std::false_type {};
39
40 template <> struct spiceUnitSelector<dotSubckt> : std::true_type {};
41 template <> struct spiceUnitSelector<modelName> : std::true_type {};
42 template <> struct spiceUnitSelector<dotSubcktPinName> : std::true_type {};
43 template <> struct spiceUnitSelector<dotSubcktParams> : std::true_type {};
44 template <> struct spiceUnitSelector<param> : std::true_type {};
45 template <> struct spiceUnitSelector<paramValue> : std::true_type {};
46 template <> struct spiceUnitSelector<number<SIM_VALUE::TYPE_INT, NOTATION::SPICE>>
47 : std::true_type {};
48 template <> struct spiceUnitSelector<number<SIM_VALUE::TYPE_FLOAT, NOTATION::SPICE>>
49 : std::true_type {};
50}
51
52
53std::string SPICE_GENERATOR_SUBCKT::ModelLine( const SPICE_ITEM& aItem ) const
54{
55 return "";
56}
57
58
59std::vector<std::string> SPICE_GENERATOR_SUBCKT::CurrentNames( const SPICE_ITEM& aItem ) const
60{
61 std::vector<std::string> currentNames;
62
63 for( const SIM_MODEL_PIN& pin : GetPins() )
64 currentNames.push_back( fmt::format( "I({}:{})", ItemName( aItem ), pin.modelPinName ) );
65
66 return currentNames;
67}
68
69
71 const std::string& aSpiceCode )
72{
73 tao::pegtl::string_input<> in( aSpiceCode, "from_content" );
74 std::unique_ptr<tao::pegtl::parse_tree::node> root;
75
76 try
77 {
78 root = tao::pegtl::parse_tree::parse<SIM_MODEL_SUBCKT_SPICE_PARSER::spiceUnitGrammar,
80 tao::pegtl::nothing,
82 }
83 catch( const tao::pegtl::parse_error& e )
84 {
85 THROW_IO_ERROR( e.what() );
86 }
87
88 SIM_MODEL_SUBCKT& model = static_cast<SIM_MODEL_SUBCKT&>( m_model );
89
90 for( const auto& node : root->children )
91 {
92 if( node->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::dotSubckt>() )
93 {
94 for( const auto& subnode : node->children )
95 {
96 if( subnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::modelName>() )
97 {
98 }
99 else if( subnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::dotSubcktPinName>() )
100 {
101 model.AddPin( { subnode->string(), fmt::format( "{}", model.GetPinCount() + 1 ) } );
102 }
103 else if( subnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::dotSubcktParams>() )
104 {
105 for( const auto& subsubnode : subnode->children )
106 {
107 if( subsubnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::param>() )
108 {
109 model.m_paramInfos.push_back( std::make_unique<SIM_MODEL::PARAM::INFO>() );
110 model.m_paramInfos.back()->name = subsubnode->string();
111 model.m_paramInfos.back()->isInstanceParam = true;
112 model.m_paramInfos.back()->isSpiceInstanceParam = true;
113
114 model.AddParam( *model.m_paramInfos.back() );
115 }
116 else if( subsubnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::paramValue>() )
117 {
118 wxASSERT( model.m_paramInfos.size() > 0 );
119 model.m_paramInfos.back()->defaultValue = subsubnode->string();
120 }
121 else
122 {
123 wxFAIL_MSG( "Unhandled parse tree subsubnode" );
124 }
125 }
126 }
127 }
128 }
129 else
130 {
131 wxFAIL_MSG( "Unhandled parse tree node" );
132 }
133 }
134
135 model.m_spiceCode = aSpiceCode;
136}
137
138
140 SIM_MODEL_SPICE( TYPE::SUBCKT, std::make_unique<SPICE_GENERATOR_SUBCKT>( *this ),
141 std::make_unique<SPICE_MODEL_PARSER_SUBCKT>( *this ) )
142{
143}
144
145
147{
148 SIM_MODEL::SetBaseModel( aBaseModel );
149
150 // Pins aren't constant for subcircuits, so they need to be copied from the base model.
151 for( const SIM_MODEL_PIN& pin : GetBaseModel()->GetPins() )
152 AddPin( pin );
153
154 // Same for parameters.
155 for( int ii = 0; ii < GetBaseModel()->GetParamCount(); ++ii )
157}
158
159
161{
162 if( !m_spiceCode.empty() )
163 return m_spiceCode;
164
165 if( const SIM_MODEL_SUBCKT* baseModel = dynamic_cast<const SIM_MODEL_SUBCKT*>( m_baseModel ) )
166 return baseModel->GetSpiceCode();
167
168 return "";
169}
std::string m_spiceCode
std::string GetSpiceCode() const
void SetBaseModel(const SIM_MODEL &aBaseModel) override
std::vector< std::unique_ptr< PARAM::INFO > > m_paramInfos
const SIM_MODEL * GetBaseModel() const
Definition: sim_model.h:466
void AddParam(const PARAM::INFO &aInfo)
Definition: sim_model.cpp:729
int GetPinCount() const
Definition: sim_model.h:471
void AddPin(const SIM_MODEL_PIN &aPin)
Definition: sim_model.cpp:705
virtual const PARAM & GetParam(unsigned aParamIndex) const
Definition: sim_model.cpp:789
virtual void SetBaseModel(const SIM_MODEL &aBaseModel)
Definition: sim_model.cpp:739
int GetParamCount() const
Definition: sim_model.h:481
std::vector< std::reference_wrapper< const SIM_MODEL_PIN > > GetPins() const
Definition: sim_model.cpp:748
const SIM_MODEL * m_baseModel
Definition: sim_model.h:540
std::vector< std::string > CurrentNames(const SPICE_ITEM &aItem) const override
std::string ModelLine(const SPICE_ITEM &aItem) const override
virtual std::string ItemName(const SPICE_ITEM &aItem) const
virtual std::vector< std::reference_wrapper< const SIM_MODEL_PIN > > GetPins() const
void ReadModel(const SIM_LIBRARY_SPICE &aLibrary, const std::string &aSpiceCode) override
SIM_MODEL_SPICE & m_model
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
must_if< error >::control< Rule > control
STL namespace.
SIM_MODEL::TYPE TYPE
Definition: sim_model.cpp:57
const INFO & info
Definition: sim_model.h:401