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/core.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 SIM_MODEL_PIN& pin : GetPins() )
66 currentNames.push_back( fmt::format( "I({}:{})", ItemName( aItem ), pin.modelPinName ) );
67 }
68
69 return currentNames;
70}
71
72
74 const std::string& aSpiceCode )
75{
76 tao::pegtl::string_input<> in( aSpiceCode, "from_content" );
77 std::unique_ptr<tao::pegtl::parse_tree::node> root;
78
79 try
80 {
81 root = tao::pegtl::parse_tree::parse<SIM_MODEL_SUBCKT_SPICE_PARSER::spiceUnitGrammar,
83 tao::pegtl::nothing,
85 }
86 catch( const tao::pegtl::parse_error& e )
87 {
88 THROW_IO_ERROR( e.what() );
89 }
90
92
93 for( const auto& node : root->children )
94 {
95 if( node->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::dotSubckt>() )
96 {
97 for( const auto& subnode : node->children )
98 {
99 if( subnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::modelName>() )
100 {
101 }
102 else if( subnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::dotSubcktPinName>() )
103 {
104 model.AddPin( { subnode->string(), fmt::format( "{}", model.GetPinCount() + 1 ) } );
105 }
106 else if( subnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::dotSubcktParams>() )
107 {
108 for( const auto& subsubnode : subnode->children )
109 {
110 if( subsubnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::param>() )
111 {
112 model.m_paramInfos.push_back( std::make_unique<SIM_MODEL::PARAM::INFO>() );
113 model.m_paramInfos.back()->name = subsubnode->string();
114 model.m_paramInfos.back()->isInstanceParam = true;
115 model.m_paramInfos.back()->isSpiceInstanceParam = true;
116
117 model.AddParam( *model.m_paramInfos.back() );
118 }
119 else if( subsubnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::paramValue>() )
120 {
121 wxASSERT( model.m_paramInfos.size() > 0 );
122 model.m_paramInfos.back()->defaultValue = subsubnode->string();
123 }
124 else
125 {
126 wxFAIL_MSG( "Unhandled parse tree subsubnode" );
127 }
128 }
129 }
130 }
131 }
132 else
133 {
134 wxFAIL_MSG( "Unhandled parse tree node" );
135 }
136 }
137
138 model.m_spiceCode = aSpiceCode;
139}
140
141
143 SIM_MODEL_SPICE( TYPE::SUBCKT, std::make_unique<SPICE_GENERATOR_SUBCKT>( *this ),
144 std::make_unique<SPICE_MODEL_PARSER_SUBCKT>( *this ) )
145{
146}
147
148
150{
151 SIM_MODEL::SetBaseModel( aBaseModel );
152
153 // Pins aren't constant for subcircuits, so they need to be copied from the base model.
154 for( const SIM_MODEL_PIN& pin : GetBaseModel()->GetPins() )
155 AddPin( pin );
156
157 // Same for parameters.
158 for( int ii = 0; ii < GetBaseModel()->GetParamCount(); ++ii )
160}
161
162
164{
165 if( !m_spiceCode.empty() )
166 return m_spiceCode;
167
168 if( const SIM_MODEL_SUBCKT* baseModel = dynamic_cast<const SIM_MODEL_SUBCKT*>( m_baseModel ) )
169 return baseModel->GetSpiceCode();
170
171 return "";
172}
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:459
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:474
std::vector< std::reference_wrapper< const SIM_MODEL_PIN > > GetPins() const
const SIM_MODEL * m_baseModel
Definition sim_model.h:533
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