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( const SIM_MODEL::PARAM& param : GetInstanceParams() )
70 {
71 // The only instance param is "ic", which is positional.
72 std::string value = param.value;
73
74 if( value != "none" )
75 result.append( " " + value );
76 }
77
78 return result;
79}
80
81
82std::vector<std::reference_wrapper<const SIM_MODEL::PIN>> SPICE_GENERATOR_SWITCH::GetPins() const
83{
84 switch( m_model.GetType() )
85 {
86 case SIM_MODEL::TYPE::SW_V:
87 return { m_model.GetPin( 2 ), m_model.GetPin( 3 ), m_model.GetPin( 0 ), m_model.GetPin( 1 ) };
88
89 case SIM_MODEL::TYPE::SW_I:
90 return { m_model.GetPin( 2 ), m_model.GetPin( 3 ) };
91
92 default:
93 wxFAIL_MSG( "Unhandled SIM_MODEL type in SIM_MODEL_SWITCH" );
94 return {};
95 }
96}
97
98
100 SIM_MODEL( aType,
101 std::make_unique<SPICE_GENERATOR_SWITCH>( *this ) )
102{
103 static std::vector<PARAM::INFO> vsw = makeSwVParamInfos();
104 static std::vector<PARAM::INFO> isw = makeSwIParamInfos();
105
106 switch( aType )
107 {
108 case TYPE::SW_V:
109 for( const PARAM::INFO& paramInfo : vsw )
110 AddParam( paramInfo );
111 break;
112
113 case TYPE::SW_I:
114 for( const PARAM::INFO& paramInfo : isw )
115 AddParam( paramInfo );
116 break;
117
118 default:
119 wxFAIL_MSG( wxS( "Unhandled SIM_MODEL type in SIM_MODEL_SWITCH" ) );
120 break;
121 }
122}
123
124
125const std::vector<SIM_MODEL::PARAM::INFO> SIM_MODEL_SWITCH::makeSwVParamInfos()
126{
127 std::vector<PARAM::INFO> paramInfos;
128 PARAM::INFO paramInfo;
129
130 paramInfo.name = "thr";
131 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
132 paramInfo.unit = "V";
134 paramInfo.defaultValue = "0";
135 paramInfo.description = "Threshold voltage";
136 paramInfo.isSpiceInstanceParam = false;
137 paramInfo.spiceModelName = "vt";
138 paramInfo.enumValues = {};
139 paramInfos.push_back( paramInfo );
140
141 paramInfo.name = "his";
142 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
143 paramInfo.unit = "V";
145 paramInfo.defaultValue = "0";
146 paramInfo.description = "Hysteresis voltage";
147 paramInfo.isSpiceInstanceParam = false;
148 paramInfo.spiceModelName = "vh";
149 paramInfo.enumValues = {};
150 paramInfos.push_back( paramInfo );
151
152 paramInfo.name = "ron";
153 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
154 paramInfo.unit = "Ω";
156 paramInfo.defaultValue = "1";
157 paramInfo.description = "Resistance when closed";
158 paramInfo.isSpiceInstanceParam = false;
159 paramInfo.spiceModelName = "";
160 paramInfo.enumValues = {};
161 paramInfos.push_back( paramInfo );
162
163 paramInfo.name = "roff";
164 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
165 paramInfo.unit = "Ω";
167 paramInfo.defaultValue = "1e+12";
168 paramInfo.description = "Resistance when open";
169 paramInfo.isSpiceInstanceParam = false;
170 paramInfo.spiceModelName = "";
171 paramInfo.enumValues = {};
172 paramInfos.push_back( paramInfo );
173
174 paramInfo.name = "ic";
175 paramInfo.type = SIM_VALUE::TYPE_STRING;
176 paramInfo.unit = "";
178 paramInfo.defaultValue = "none";
179 paramInfo.description = "Initial state";
180 paramInfo.isSpiceInstanceParam = true;
181 paramInfo.spiceModelName = "";
182 paramInfo.enumValues = { "none", "off", "on" };
183 paramInfos.push_back( paramInfo );
184
185 return paramInfos;
186}
187
188
189const std::vector<SIM_MODEL::PARAM::INFO> SIM_MODEL_SWITCH::makeSwIParamInfos()
190{
191 std::vector<PARAM::INFO> paramInfos;
192 PARAM::INFO paramInfo;
193
194 paramInfo.name = "thr";
195 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
196 paramInfo.unit = "A";
198 paramInfo.defaultValue = "0";
199 paramInfo.description = "Threshold current";
200 paramInfo.isSpiceInstanceParam = false;
201 paramInfo.spiceModelName = "it";
202 paramInfo.enumValues = {};
203 paramInfos.push_back( paramInfo );
204
205 paramInfo.name = "his";
206 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
207 paramInfo.unit = "A";
209 paramInfo.defaultValue = "0";
210 paramInfo.description = "Hysteresis current";
211 paramInfo.isSpiceInstanceParam = false;
212 paramInfo.spiceModelName = "ih";
213 paramInfo.enumValues = {};
214 paramInfos.push_back( paramInfo );
215
216 paramInfo.name = "ron";
217 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
218 paramInfo.unit = "Ω";
220 paramInfo.defaultValue = "1";
221 paramInfo.description = "Resistance when closed";
222 paramInfo.isSpiceInstanceParam = false;
223 paramInfo.spiceModelName = "";
224 paramInfo.enumValues = {};
225 paramInfos.push_back( paramInfo );
226
227 paramInfo.name = "roff";
228 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
229 paramInfo.unit = "Ω";
231 paramInfo.defaultValue = "1e+12";
232 paramInfo.description = "Resistance when open";
233 paramInfo.isSpiceInstanceParam = false;
234 paramInfo.spiceModelName = "";
235 paramInfo.enumValues = {};
236 paramInfos.push_back( paramInfo );
237
238 paramInfo.name = "ic";
239 paramInfo.type = SIM_VALUE::TYPE_STRING;
240 paramInfo.unit = "";
242 paramInfo.defaultValue = "1";
243 paramInfo.description = "Initial state";
244 paramInfo.isSpiceInstanceParam = true;
245 paramInfo.spiceModelName = "";
246 paramInfo.enumValues = { "none", "off", "on" };
247 paramInfos.push_back( paramInfo );
248
249 return paramInfos;
250}
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:720
const PIN & GetPin(unsigned aIndex) const
Definition: sim_model.h:473
TYPE GetType() const
Definition: sim_model.h:465
@ 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::vector< std::reference_wrapper< const SIM_MODEL::PARAM > > GetInstanceParams() const
virtual std::string ItemLine(const SPICE_ITEM &aItem) const
const SIM_MODEL & m_model
STL namespace.
SIM_MODEL::TYPE TYPE
Definition: sim_model.cpp:53
std::string spiceModelName
Definition: sim_model.h:387
std::vector< std::string > enumValues
Definition: sim_model.h:389
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 refName
std::string modelName
std::vector< std::string > pinNetNames