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
105 {
106 if( param.info.name == "model" )
107 result.append( " " + SIM_VALUE::ToSpice( param.value ) );
108 }
109
110 return result;
111}
112
113
114std::string SPICE_GENERATOR_RAW_SPICE::Preview( const SPICE_ITEM& aItem ) const
115{
116 SPICE_ITEM item = aItem;
117 item.refName = "";
118
119 for( int i = 0; i < m_model.GetPinCount(); ++i )
120 {
121 item.pinNumbers.push_back( fmt::format( "{}", i + 1 ) );
122 item.pinNetNames.push_back( fmt::format( "{}", i + 1 ) );
123 }
124
125 return ItemLine( item );
126}
127
128
129SIM_MODEL_RAW_SPICE::SIM_MODEL_RAW_SPICE( const std::string& aSpiceSource ) :
130 SIM_MODEL( TYPE::RAWSPICE, std::make_unique<SPICE_GENERATOR_RAW_SPICE>( *this ) ),
131 m_spiceCode( aSpiceSource )
132{
133 static std::vector<PARAM::INFO> paramInfos = makeParamInfos();
134
135 for( const PARAM::INFO& paramInfo : paramInfos )
136 AddParam( paramInfo );
137
138 SetParamValue( "model", aSpiceSource );
139}
140
141
142void SIM_MODEL_RAW_SPICE::SetPinSymbolPinNumber( const std::string& aPinName,
143 const std::string& aSymbolPinNumber )
144{
145 for( PIN& pin : m_pins )
146 {
147 if( pin.name == aPinName )
148 {
149 pin.symbolPinNumber = aSymbolPinNumber;
150 return;
151 }
152 }
153
154 m_pins.push_back( { aPinName, aSymbolPinNumber } );
155}
156
157
158std::vector<SIM_MODEL::PARAM::INFO> SIM_MODEL_RAW_SPICE::makeParamInfos()
159{
160 std::vector<PARAM::INFO> paramInfos;
161
162 for( SPICE_PARAM spiceParam : SPICE_PARAM_ITERATOR() )
163 {
164 PARAM::INFO paramInfo;
165
166 switch( spiceParam )
167 {
168 case SPICE_PARAM::TYPE:
169 paramInfo.name = "type";
170 paramInfo.type = SIM_VALUE::TYPE_STRING;
171 paramInfo.unit = "";
173 paramInfo.defaultValue = "";
174 paramInfo.description = "Spice element type";
175 paramInfo.isSpiceInstanceParam = true;
176
177 paramInfos.push_back( paramInfo );
178 break;
179
180 case SPICE_PARAM::MODEL:
181 paramInfo.name = "model";
182 paramInfo.type = SIM_VALUE::TYPE_STRING;
183 paramInfo.unit = "";
185 paramInfo.defaultValue = "";
186 paramInfo.description = "Model name or value";
187 paramInfo.isSpiceInstanceParam = true;
188
189 paramInfos.push_back( paramInfo );
190 break;
191
192 case SPICE_PARAM::LIB:
193 paramInfo.name = "lib";
194 paramInfo.type = SIM_VALUE::TYPE_STRING;
195 paramInfo.unit = "";
197 paramInfo.defaultValue = "";
198 paramInfo.description = "Library path to include";
199 paramInfo.isSpiceInstanceParam = true;
200
201 paramInfos.push_back( paramInfo );
202 break;
203
204 case SPICE_PARAM::_ENUM_END:
205 break;
206 }
207 }
208
209 return paramInfos;
210}
void SetPinSymbolPinNumber(const std::string &aPinName, const std::string &aSymbolPinNumber) override
static std::vector< PARAM::INFO > makeParamInfos()
std::vector< PIN > m_pins
Definition: sim_model.h:542
int GetPinCount() const
Definition: sim_model.h:472
virtual const PARAM & GetParam(unsigned aParamIndex) const
Definition: sim_model.cpp:779
static std::string ToSpice(const std::string &aString)
Definition: sim_value.h:84
@ TYPE_STRING
Definition: sim_value.h:71
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::PARAM > > GetInstanceParams() const
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:53
SIM_VALUE::TYPE type
Definition: sim_model.h:380
std::string defaultValue
Definition: sim_model.h:383
std::string description
Definition: sim_model.h:384
std::string value
Definition: sim_model.h:401
std::string refName
std::vector< std::string > pinNetNames
std::vector< std::string > pinNumbers