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