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