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 return ItemParams();
122}
123
124
125SIM_MODEL_RAW_SPICE::SIM_MODEL_RAW_SPICE( const std::string& aSpiceSource ) :
126 SIM_MODEL( TYPE::RAWSPICE, std::make_unique<SPICE_GENERATOR_RAW_SPICE>( *this ) ),
127 m_spiceCode( aSpiceSource )
128{
129 static std::vector<PARAM::INFO> paramInfos = makeParamInfos();
130
131 for( const PARAM::INFO& paramInfo : paramInfos )
132 AddParam( paramInfo );
133
134 SetParamValue( "model", aSpiceSource );
135}
136
137
138void SIM_MODEL_RAW_SPICE::AssignSymbolPinNumberToModelPin( const std::string& aModelPinName,
139 const wxString& aSymbolPinNumber )
140{
141 // SPICE doesn't name model inputs so we have to assume they're indexes here.
142 int pinIndex = (int) strtol( aModelPinName.c_str(), nullptr, 10 );
143
144 if( pinIndex > 0 )
145 {
146 while( (int)m_modelPins.size() < pinIndex )
147 m_modelPins.push_back( { fmt::format( "{}", m_modelPins.size() + 1 ), wxEmptyString } );
148
149 m_modelPins[ --pinIndex /* convert to 0-based */ ].symbolPinNumber = aSymbolPinNumber;
150 }
151}
152
153
154std::vector<SIM_MODEL::PARAM::INFO> SIM_MODEL_RAW_SPICE::makeParamInfos()
155{
156 std::vector<PARAM::INFO> paramInfos;
157
158 for( SPICE_PARAM spiceParam : SPICE_PARAM_ITERATOR() )
159 {
160 PARAM::INFO paramInfo;
161
162 switch( spiceParam )
163 {
164 case SPICE_PARAM::TYPE:
165 paramInfo.name = "type";
166 paramInfo.type = SIM_VALUE::TYPE_STRING;
167 paramInfo.unit = "";
169 paramInfo.defaultValue = "";
170 paramInfo.description = "Spice element type";
171 paramInfo.isSpiceInstanceParam = true;
172
173 paramInfos.push_back( paramInfo );
174 break;
175
176 case SPICE_PARAM::MODEL:
177 paramInfo.name = "model";
178 paramInfo.type = SIM_VALUE::TYPE_STRING;
179 paramInfo.unit = "";
181 paramInfo.defaultValue = "";
182 paramInfo.description = "Model name or value";
183 paramInfo.isSpiceInstanceParam = true;
184
185 paramInfos.push_back( paramInfo );
186 break;
187
188 case SPICE_PARAM::LIB:
189 paramInfo.name = "lib";
190 paramInfo.type = SIM_VALUE::TYPE_STRING;
191 paramInfo.unit = "";
193 paramInfo.defaultValue = "";
194 paramInfo.description = "Library path to include";
195 paramInfo.isSpiceInstanceParam = true;
196
197 paramInfos.push_back( paramInfo );
198 break;
199
200 case SPICE_PARAM::_ENUM_END:
201 break;
202 }
203 }
204
205 return paramInfos;
206}
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
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::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