KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sim_model_raw_spice.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
23
24#include <boost/algorithm/string/predicate.hpp>
25#include <fmt/core.h>
26#include <pegtl/contrib/parse_tree.hpp>
27
28
30{
31 using namespace SIM_MODEL_SERIALIZER_GRAMMAR;
32
33 template <typename Rule> struct legacyPinSequenceSelector : std::false_type {};
34 template <> struct legacyPinSequenceSelector<legacyPinNumber> : std::true_type {};
35}
36
37
38std::string SPICE_GENERATOR_RAW_SPICE::ModelLine( const SPICE_ITEM& aItem ) const
39{
40 return "";
41}
42
43
44std::string SPICE_GENERATOR_RAW_SPICE::ItemName( const SPICE_ITEM& aItem ) const
45{
46 std::string type = m_model.GetParam( (int) SIM_MODEL_RAW_SPICE::SPICE_PARAM::TYPE ).value;
47
48 if( aItem.refName != "" && boost::starts_with( aItem.refName, type ) )
49 return aItem.refName;
50 else
51 return fmt::format( "{}{}", type, aItem.refName );
52}
53
54
55std::string SPICE_GENERATOR_RAW_SPICE::ItemPins( const SPICE_ITEM& aItem ) const
56{
57 std::string result;
58 int ncCounter = 0;
59
60 if( !GetPins().empty() )
61 {
62 for( const SIM_MODEL_PIN& pin : GetPins() )
63 {
64 auto it = std::find( aItem.pinNumbers.begin(), aItem.pinNumbers.end(),
65 pin.symbolPinNumber );
66
67 if( it != aItem.pinNumbers.end() )
68 {
69 long symbolPinIndex = std::distance( aItem.pinNumbers.begin(), it );
70 result.append( fmt::format( " {}", aItem.pinNetNames.at( symbolPinIndex ) ) );
71 }
72 else
73 {
74 result.append( fmt::format( " NC-{}-{}", aItem.refName, ncCounter++ ) );
75 }
76 }
77 }
78 else
79 {
80 // If we don't know what pins the model has, just output the symbol's pins
81
82 for( const std::string& pinNetName : aItem.pinNetNames )
83 result.append( fmt::format( " {}", pinNetName ) );
84 }
85
86 return result;
87}
88
89
91{
92 return "";
93}
94
95
97{
98 std::string result;
99
100 for( int ii = 0; ii < m_model.GetParamCount(); ++ii )
101 {
102 const SIM_MODEL::PARAM& param = m_model.GetParam( ii );
103
104 if( !param.info.isSpiceInstanceParam )
105 continue;
106
107 if( param.info.name == "model" )
108 result.append( " " + SIM_VALUE::ToSpice( param.value ) );
109 }
110
111 return result;
112}
113
114
115std::string SPICE_GENERATOR_RAW_SPICE::Preview( const SPICE_ITEM& aItem ) const
116{
117 return ItemParams();
118}
119
120
121SIM_MODEL_RAW_SPICE::SIM_MODEL_RAW_SPICE( const std::string& aSpiceSource ) :
122 SIM_MODEL( TYPE::RAWSPICE, std::make_unique<SPICE_GENERATOR_RAW_SPICE>( *this ) ),
123 m_spiceCode( aSpiceSource )
124{
125 static std::vector<PARAM::INFO> paramInfos = makeParamInfos();
126
127 for( const PARAM::INFO& paramInfo : paramInfos )
128 AddParam( paramInfo );
129
130 SetParamValue( "model", aSpiceSource );
131}
132
133
134void SIM_MODEL_RAW_SPICE::AssignSymbolPinNumberToModelPin( const std::string& aModelPinName,
135 const wxString& aSymbolPinNumber )
136{
137 // SPICE doesn't name model inputs so we have to assume they're indexes here.
138 int pinIndex = (int) strtol( aModelPinName.c_str(), nullptr, 10 );
139
140 if( pinIndex > 0 )
141 {
142 while( (int)m_modelPins.size() < pinIndex )
143 m_modelPins.push_back( { fmt::format( "{}", m_modelPins.size() + 1 ), wxEmptyString } );
144
145 m_modelPins[ --pinIndex /* convert to 0-based */ ].symbolPinNumber = aSymbolPinNumber;
146 }
147}
148
149
150std::vector<SIM_MODEL::PARAM::INFO> SIM_MODEL_RAW_SPICE::makeParamInfos()
151{
152 std::vector<PARAM::INFO> paramInfos;
153
154 for( SPICE_PARAM spiceParam : SPICE_PARAM_ITERATOR() )
155 {
156 PARAM::INFO paramInfo;
157
158 switch( spiceParam )
159 {
160 case SPICE_PARAM::TYPE:
161 paramInfo.name = "type";
162 paramInfo.type = SIM_VALUE::TYPE_STRING;
163 paramInfo.unit = "";
164 paramInfo.category = SIM_MODEL::PARAM::CATEGORY::PRINCIPAL;
165 paramInfo.defaultValue = "";
166 paramInfo.description = "Spice element type";
167 paramInfo.isSpiceInstanceParam = true;
168
169 paramInfos.push_back( paramInfo );
170 break;
171
172 case SPICE_PARAM::MODEL:
173 paramInfo.name = "model";
174 paramInfo.type = SIM_VALUE::TYPE_STRING;
175 paramInfo.unit = "";
176 paramInfo.category = SIM_MODEL::PARAM::CATEGORY::PRINCIPAL;
177 paramInfo.defaultValue = "";
178 paramInfo.description = "Model name or value";
179 paramInfo.isSpiceInstanceParam = true;
180
181 paramInfos.push_back( paramInfo );
182 break;
183
184 case SPICE_PARAM::LIB:
185 paramInfo.name = "lib";
186 paramInfo.type = SIM_VALUE::TYPE_STRING;
187 paramInfo.unit = "";
188 paramInfo.category = SIM_MODEL::PARAM::CATEGORY::PRINCIPAL;
189 paramInfo.defaultValue = "";
190 paramInfo.description = "Library path to include";
191 paramInfo.isSpiceInstanceParam = true;
192
193 paramInfos.push_back( paramInfo );
194 break;
195
196 case SPICE_PARAM::_ENUM_END:
197 break;
198 }
199 }
200
201 return paramInfos;
202}
void AssignSymbolPinNumberToModelPin(const std::string &aModelPinName, const wxString &aSymbolPinNumber) override
static std::vector< PARAM::INFO > makeParamInfos()
std::vector< SIM_MODEL_PIN > m_modelPins
Definition sim_model.h:533
@ TYPE_STRING
Definition sim_value.h:67
static std::string ToSpice(const std::string &aString)
std::string ModelLine(const SPICE_ITEM &aItem) const override
std::string ItemPins(const SPICE_ITEM &aItem) const override
std::string ItemModelName(const SPICE_ITEM &aItem) const override
std::string ItemParams() const override
std::string Preview(const SPICE_ITEM &aItem) const override
std::string ItemName(const SPICE_ITEM &aItem) const override
virtual std::vector< std::reference_wrapper< const SIM_MODEL_PIN > > GetPins() const
const SIM_MODEL & m_model
static bool empty(const wxTextEntryBase *aCtrl)
STL namespace.
SIM_MODEL::TYPE TYPE
Definition sim_model.cpp:54
std::string refName
std::vector< std::string > pinNetNames
std::vector< std::string > pinNumbers
KIBIS_PIN * pin
wxString result
Test unit parsing edge cases and error handling.