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 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
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 if( GetPins().size() == 2 )
64 {
65 currentNames.push_back( fmt::format( "I({})", ItemName( aItem ) ) );
66 }
67 else
68 {
69 for( const SIM_MODEL_PIN& pin : GetPins() )
70 currentNames.push_back( fmt::format( "I({}:{})", ItemName( aItem ), pin.modelPinName ) );
71 }
72
73 return currentNames;
74}
75
76
78 const std::string& aSpiceCode )
79{
80 tao::pegtl::string_input<> in( aSpiceCode, "from_content" );
81 std::unique_ptr<tao::pegtl::parse_tree::node> root;
82
83 try
84 {
85 root = tao::pegtl::parse_tree::parse<SIM_MODEL_SUBCKT_SPICE_PARSER::spiceUnitGrammar,
87 tao::pegtl::nothing,
89 }
90 catch( const tao::pegtl::parse_error& e )
91 {
92 THROW_IO_ERROR( e.what() );
93 }
94
96
97 for( const auto& node : root->children )
98 {
99 if( node->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::dotSubckt>() )
100 {
101 for( const auto& subnode : node->children )
102 {
103 if( subnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::modelName>() )
104 {
105 }
106 else if( subnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::dotSubcktPinName>() )
107 {
108 model.AddPin( { subnode->string(), fmt::format( "{}", model.GetPinCount() + 1 ) } );
109 }
110 else if( subnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::dotSubcktParams>() )
111 {
112 for( const auto& subsubnode : subnode->children )
113 {
114 if( subsubnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::param>() )
115 {
116 model.m_paramInfos.push_back( std::make_unique<SIM_MODEL::PARAM::INFO>() );
117 model.m_paramInfos.back()->name = subsubnode->string();
118 model.m_paramInfos.back()->isInstanceParam = true;
119 model.m_paramInfos.back()->isSpiceInstanceParam = true;
120
121 model.AddParam( *model.m_paramInfos.back() );
122 }
123 else if( subsubnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::paramValue>() )
124 {
125 wxASSERT( model.m_paramInfos.size() > 0 );
126 model.m_paramInfos.back()->defaultValue = subsubnode->string();
127 }
128 else
129 {
130 wxFAIL_MSG( "Unhandled parse tree subsubnode" );
131 }
132 }
133 }
134 }
135 }
136 else
137 {
138 wxFAIL_MSG( "Unhandled parse tree node" );
139 }
140 }
141
142 model.m_spiceCode = aSpiceCode;
143}
144
145
147 SIM_MODEL_SPICE( TYPE::SUBCKT, std::make_unique<SPICE_GENERATOR_SUBCKT>( *this ),
148 std::make_unique<SPICE_MODEL_PARSER_SUBCKT>( *this ) )
149{
150}
151
152
154{
155 SIM_MODEL::SetBaseModel( aBaseModel );
156
157 // Pins aren't constant for subcircuits, so they need to be copied from the base model.
158 for( const SIM_MODEL_PIN& pin : GetBaseModel()->GetPins() )
159 AddPin( pin );
160
161 // Same for parameters.
162 for( int ii = 0; ii < GetBaseModel()->GetParamCount(); ++ii )
164}
165
166
168{
169 if( !m_spiceCode.empty() )
170 return m_spiceCode;
171
172 if( const SIM_MODEL_SUBCKT* baseModel = dynamic_cast<const SIM_MODEL_SUBCKT*>( m_baseModel ) )
173 return baseModel->GetSpiceCode();
174
175 return "";
176}
std::string m_spiceCode
SIM_MODEL_SPICE(TYPE aType, std::unique_ptr< SPICE_GENERATOR > aSpiceGenerator)
friend class SPICE_MODEL_PARSER_SUBCKT
std::string GetSpiceCode() const
void SetBaseModel(const SIM_MODEL &aBaseModel) override
const SIM_MODEL * GetBaseModel() const
Definition sim_model.h:462
void AddParam(const PARAM::INFO &aInfo)
void AddPin(const SIM_MODEL_PIN &aPin)
virtual const PARAM & GetParam(unsigned aParamIndex) const
virtual void SetBaseModel(const SIM_MODEL &aBaseModel)
SIM_MODEL()=delete
int GetParamCount() const
Definition sim_model.h:477
std::vector< std::reference_wrapper< const SIM_MODEL_PIN > > GetPins() const
const SIM_MODEL * m_baseModel
Definition sim_model.h:536
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)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
must_if< error >::control< Rule > control
STL namespace.
SIM_MODEL::TYPE TYPE
Definition sim_model.cpp:57
KIBIS_MODEL * model
KIBIS_PIN * pin