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