KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sim_model_switch.cpp
Go to the documentation of this file.
1/* This program source code file is part of KiCad, a free EDA CAD application.
2 *
3 * Copyright (C) 2022 Mikolaj Wielgus
4 * Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * https://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26#include <fmt/core.h>
27
28
29std::string SPICE_GENERATOR_SWITCH::ItemLine( const SPICE_ITEM& aItem ) const
30{
31 std::string result;
32
33 switch( m_model.GetType() )
34 {
35 case SIM_MODEL::TYPE::SW_V:
36 {
37 result = SPICE_GENERATOR::ItemLine( aItem );
38 break;
39 }
40
41 case SIM_MODEL::TYPE::SW_I:
42 {
43 std::string vsourceName = fmt::format( "V__{}", aItem.refName );
44
45 wxCHECK_MSG( aItem.pinNetNames.size() >= 2, "", wxS( "Missing two pin net names for SW_I" ) );
46
47 // Current switches measure input current through a voltage source.
48 result.append( fmt::format( "{0} {1} 0\n", aItem.pinNetNames[0], aItem.pinNetNames[1] ) );
49
50 SPICE_ITEM item = aItem;
51 item.modelName = fmt::format( "{0} {1}", vsourceName, aItem.modelName );
52 result.append( SPICE_GENERATOR::ItemLine( item ) );
53 break;
54 }
55
56 default:
57 wxFAIL_MSG( wxS( "Unhandled SIM_MODEL type in SIM_MODEL_SWITCH" ) );
58 break;
59 }
60
61 return result;
62}
63
64
66{
67 std::string result;
68
69 for( int ii = 0; ii < m_model.GetParamCount(); ++ii )
70 {
71 const SIM_MODEL::PARAM& param = m_model.GetParam( ii );
72
73 if( !param.info.isSpiceInstanceParam )
74 continue;
75
76 // The only instance param is "ic", which is positional.
77 std::string value = param.value;
78
79 if( value != "none" )
80 result.append( " " + value );
81 }
82
83 return result;
84}
85
86
87std::vector<std::reference_wrapper<const SIM_MODEL_PIN>> SPICE_GENERATOR_SWITCH::GetPins() const
88{
89 switch( m_model.GetType() )
90 {
91 case SIM_MODEL::TYPE::SW_V:
92 return { m_model.GetPin( 2 ), m_model.GetPin( 3 ), m_model.GetPin( 0 ), m_model.GetPin( 1 ) };
93
94 case SIM_MODEL::TYPE::SW_I:
95 return { m_model.GetPin( 2 ), m_model.GetPin( 3 ) };
96
97 default:
98 wxFAIL_MSG( "Unhandled SIM_MODEL type in SIM_MODEL_SWITCH" );
99 return {};
100 }
101}
102
103
105 SIM_MODEL( aType,
106 std::make_unique<SPICE_GENERATOR_SWITCH>( *this ) )
107{
108 static std::vector<PARAM::INFO> vsw = makeSwVParamInfos();
109 static std::vector<PARAM::INFO> isw = makeSwIParamInfos();
110
111 switch( aType )
112 {
113 case TYPE::SW_V:
114 for( const PARAM::INFO& paramInfo : vsw )
115 AddParam( paramInfo );
116 break;
117
118 case TYPE::SW_I:
119 for( const PARAM::INFO& paramInfo : isw )
120 AddParam( paramInfo );
121 break;
122
123 default:
124 wxFAIL_MSG( wxS( "Unhandled SIM_MODEL type in SIM_MODEL_SWITCH" ) );
125 break;
126 }
127}
128
129
130const std::vector<SIM_MODEL::PARAM::INFO> SIM_MODEL_SWITCH::makeSwVParamInfos()
131{
132 std::vector<PARAM::INFO> paramInfos;
133 PARAM::INFO paramInfo;
134
135 paramInfo.name = "thr";
136 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
137 paramInfo.unit = "V";
139 paramInfo.defaultValue = "0";
140 paramInfo.description = "Threshold voltage";
141 paramInfo.isSpiceInstanceParam = false;
142 paramInfo.spiceModelName = "vt";
143 paramInfo.enumValues = {};
144 paramInfos.push_back( paramInfo );
145
146 paramInfo.name = "his";
147 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
148 paramInfo.unit = "V";
150 paramInfo.defaultValue = "0";
151 paramInfo.description = "Hysteresis voltage";
152 paramInfo.isSpiceInstanceParam = false;
153 paramInfo.spiceModelName = "vh";
154 paramInfo.enumValues = {};
155 paramInfos.push_back( paramInfo );
156
157 paramInfo.name = "ron";
158 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
159 paramInfo.unit = "Ω";
161 paramInfo.defaultValue = "1";
162 paramInfo.description = "Resistance when closed";
163 paramInfo.isSpiceInstanceParam = false;
164 paramInfo.spiceModelName = "";
165 paramInfo.enumValues = {};
166 paramInfos.push_back( paramInfo );
167
168 paramInfo.name = "roff";
169 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
170 paramInfo.unit = "Ω";
172 paramInfo.defaultValue = "1e+12";
173 paramInfo.description = "Resistance when open";
174 paramInfo.isSpiceInstanceParam = false;
175 paramInfo.spiceModelName = "";
176 paramInfo.enumValues = {};
177 paramInfos.push_back( paramInfo );
178
179 paramInfo.name = "ic";
180 paramInfo.type = SIM_VALUE::TYPE_STRING;
181 paramInfo.unit = "";
183 paramInfo.defaultValue = "none";
184 paramInfo.description = "Initial state";
185 paramInfo.isSpiceInstanceParam = true;
186 paramInfo.spiceModelName = "";
187 paramInfo.enumValues = { "none", "off", "on" };
188 paramInfos.push_back( paramInfo );
189
190 return paramInfos;
191}
192
193
194const std::vector<SIM_MODEL::PARAM::INFO> SIM_MODEL_SWITCH::makeSwIParamInfos()
195{
196 std::vector<PARAM::INFO> paramInfos;
197 PARAM::INFO paramInfo;
198
199 paramInfo.name = "thr";
200 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
201 paramInfo.unit = "A";
203 paramInfo.defaultValue = "0";
204 paramInfo.description = "Threshold current";
205 paramInfo.isSpiceInstanceParam = false;
206 paramInfo.spiceModelName = "it";
207 paramInfo.enumValues = {};
208 paramInfos.push_back( paramInfo );
209
210 paramInfo.name = "his";
211 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
212 paramInfo.unit = "A";
214 paramInfo.defaultValue = "0";
215 paramInfo.description = "Hysteresis current";
216 paramInfo.isSpiceInstanceParam = false;
217 paramInfo.spiceModelName = "ih";
218 paramInfo.enumValues = {};
219 paramInfos.push_back( paramInfo );
220
221 paramInfo.name = "ron";
222 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
223 paramInfo.unit = "Ω";
225 paramInfo.defaultValue = "1";
226 paramInfo.description = "Resistance when closed";
227 paramInfo.isSpiceInstanceParam = false;
228 paramInfo.spiceModelName = "";
229 paramInfo.enumValues = {};
230 paramInfos.push_back( paramInfo );
231
232 paramInfo.name = "roff";
233 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
234 paramInfo.unit = "Ω";
236 paramInfo.defaultValue = "1e+12";
237 paramInfo.description = "Resistance when open";
238 paramInfo.isSpiceInstanceParam = false;
239 paramInfo.spiceModelName = "";
240 paramInfo.enumValues = {};
241 paramInfos.push_back( paramInfo );
242
243 paramInfo.name = "ic";
244 paramInfo.type = SIM_VALUE::TYPE_STRING;
245 paramInfo.unit = "";
247 paramInfo.defaultValue = "1";
248 paramInfo.description = "Initial state";
249 paramInfo.isSpiceInstanceParam = true;
250 paramInfo.spiceModelName = "";
251 paramInfo.enumValues = { "none", "off", "on" };
252 paramInfos.push_back( paramInfo );
253
254 return paramInfos;
255}
SIM_MODEL_SWITCH(TYPE aType)
static const std::vector< PARAM::INFO > makeSwVParamInfos()
static const std::vector< PARAM::INFO > makeSwIParamInfos()
void AddParam(const PARAM::INFO &aInfo)
Definition: sim_model.cpp:729
virtual const PARAM & GetParam(unsigned aParamIndex) const
Definition: sim_model.cpp:789
int GetParamCount() const
Definition: sim_model.h:481
const SIM_MODEL_PIN & GetPin(unsigned aIndex) const
Definition: sim_model.h:472
TYPE GetType() const
Definition: sim_model.h:464
@ TYPE_FLOAT
Definition: sim_value.h:69
@ TYPE_STRING
Definition: sim_value.h:71
std::string ItemLine(const SPICE_ITEM &aItem) const override
std::string ItemParams() const override
std::vector< std::reference_wrapper< const SIM_MODEL_PIN > > GetPins() const override
virtual std::string ItemLine(const SPICE_ITEM &aItem) const
const SIM_MODEL & m_model
STL namespace.
SIM_MODEL::TYPE TYPE
Definition: sim_model.cpp:57
std::string spiceModelName
Definition: sim_model.h:386
std::vector< std::string > enumValues
Definition: sim_model.h:388
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
const INFO & info
Definition: sim_model.h:401
std::string refName
std::string modelName
std::vector< std::string > pinNetNames