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 The 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, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <fmt/core.h>
23
24
25std::string SPICE_GENERATOR_SWITCH::ItemLine( const SPICE_ITEM& aItem ) const
26{
27 std::string result;
28
29 switch( m_model.GetType() )
30 {
31 case SIM_MODEL::TYPE::SW_V:
32 {
34 break;
35 }
36
37 case SIM_MODEL::TYPE::SW_I:
38 {
39 std::string vsourceName = fmt::format( "V__{}", aItem.refName );
40
41 wxCHECK_MSG( aItem.pinNetNames.size() >= 2, "", wxS( "Missing two pin net names for SW_I" ) );
42
43 // Current switches measure input current through a voltage source.
44 result.append( fmt::format( "{0} {1} 0\n", aItem.pinNetNames[0], aItem.pinNetNames[1] ) );
45
46 SPICE_ITEM item = aItem;
47 item.modelName = fmt::format( "{0} {1}", vsourceName, aItem.modelName );
48 result.append( SPICE_GENERATOR::ItemLine( item ) );
49 break;
50 }
51
52 default:
53 wxFAIL_MSG( wxS( "Unhandled SIM_MODEL type in SIM_MODEL_SWITCH" ) );
54 break;
55 }
56
57 return result;
58}
59
60
62{
63 std::string result;
64
65 for( int ii = 0; ii < m_model.GetParamCount(); ++ii )
66 {
67 const SIM_MODEL::PARAM& param = m_model.GetParam( ii );
68
69 if( !param.info.isSpiceInstanceParam )
70 continue;
71
72 // The only instance param is "ic", which is positional.
73 std::string value = param.value;
74
75 if( value != "none" )
76 result.append( " " + value );
77 }
78
79 return result;
80}
81
82
83std::vector<std::reference_wrapper<const SIM_MODEL_PIN>> SPICE_GENERATOR_SWITCH::GetPins() const
84{
85 switch( m_model.GetType() )
86 {
87 case SIM_MODEL::TYPE::SW_V:
88 return { m_model.GetPin( 2 ), m_model.GetPin( 3 ), m_model.GetPin( 0 ), m_model.GetPin( 1 ) };
89
90 case SIM_MODEL::TYPE::SW_I:
91 return { m_model.GetPin( 2 ), m_model.GetPin( 3 ) };
92
93 default:
94 wxFAIL_MSG( "Unhandled SIM_MODEL type in SIM_MODEL_SWITCH" );
95 return {};
96 }
97}
98
99
101 SIM_MODEL( aType,
102 std::make_unique<SPICE_GENERATOR_SWITCH>( *this ) )
103{
104 static std::vector<PARAM::INFO> vsw = makeSwVParamInfos();
105 static std::vector<PARAM::INFO> isw = makeSwIParamInfos();
106
107 switch( aType )
108 {
109 case TYPE::SW_V:
110 for( const PARAM::INFO& paramInfo : vsw )
111 AddParam( paramInfo );
112 break;
113
114 case TYPE::SW_I:
115 for( const PARAM::INFO& paramInfo : isw )
116 AddParam( paramInfo );
117 break;
118
119 default:
120 wxFAIL_MSG( wxS( "Unhandled SIM_MODEL type in SIM_MODEL_SWITCH" ) );
121 break;
122 }
123}
124
125
126const std::vector<SIM_MODEL::PARAM::INFO> SIM_MODEL_SWITCH::makeSwVParamInfos()
127{
128 std::vector<PARAM::INFO> paramInfos;
129 PARAM::INFO paramInfo;
130
131 paramInfo.name = "thr";
132 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
133 paramInfo.unit = "V";
134 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
135 paramInfo.defaultValue = "0";
136 paramInfo.description = "Threshold voltage";
137 paramInfo.isSpiceInstanceParam = false;
138 paramInfo.spiceModelName = "vt";
139 paramInfo.enumValues = {};
140 paramInfos.push_back( paramInfo );
141
142 paramInfo.name = "his";
143 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
144 paramInfo.unit = "V";
145 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
146 paramInfo.defaultValue = "0";
147 paramInfo.description = "Hysteresis voltage";
148 paramInfo.isSpiceInstanceParam = false;
149 paramInfo.spiceModelName = "vh";
150 paramInfo.enumValues = {};
151 paramInfos.push_back( paramInfo );
152
153 paramInfo.name = "ron";
154 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
155 paramInfo.unit = "Ω";
156 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
157 paramInfo.defaultValue = "1";
158 paramInfo.description = "Resistance when closed";
159 paramInfo.isSpiceInstanceParam = false;
160 paramInfo.spiceModelName = "";
161 paramInfo.enumValues = {};
162 paramInfos.push_back( paramInfo );
163
164 paramInfo.name = "roff";
165 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
166 paramInfo.unit = "Ω";
167 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
168 paramInfo.defaultValue = "1e+12";
169 paramInfo.description = "Resistance when open";
170 paramInfo.isSpiceInstanceParam = false;
171 paramInfo.spiceModelName = "";
172 paramInfo.enumValues = {};
173 paramInfos.push_back( paramInfo );
174
175 paramInfo.name = "ic";
176 paramInfo.type = SIM_VALUE::TYPE_STRING;
177 paramInfo.unit = "";
178 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
179 paramInfo.defaultValue = "none";
180 paramInfo.description = "Initial state";
181 paramInfo.isSpiceInstanceParam = true;
182 paramInfo.spiceModelName = "";
183 paramInfo.enumValues = { "none", "off", "on" };
184 paramInfos.push_back( paramInfo );
185
186 return paramInfos;
187}
188
189
190const std::vector<SIM_MODEL::PARAM::INFO> SIM_MODEL_SWITCH::makeSwIParamInfos()
191{
192 std::vector<PARAM::INFO> paramInfos;
193 PARAM::INFO paramInfo;
194
195 paramInfo.name = "thr";
196 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
197 paramInfo.unit = "A";
198 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
199 paramInfo.defaultValue = "0";
200 paramInfo.description = "Threshold current";
201 paramInfo.isSpiceInstanceParam = false;
202 paramInfo.spiceModelName = "it";
203 paramInfo.enumValues = {};
204 paramInfos.push_back( paramInfo );
205
206 paramInfo.name = "his";
207 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
208 paramInfo.unit = "A";
209 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
210 paramInfo.defaultValue = "0";
211 paramInfo.description = "Hysteresis current";
212 paramInfo.isSpiceInstanceParam = false;
213 paramInfo.spiceModelName = "ih";
214 paramInfo.enumValues = {};
215 paramInfos.push_back( paramInfo );
216
217 paramInfo.name = "ron";
218 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
219 paramInfo.unit = "Ω";
220 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
221 paramInfo.defaultValue = "1";
222 paramInfo.description = "Resistance when closed";
223 paramInfo.isSpiceInstanceParam = false;
224 paramInfo.spiceModelName = "";
225 paramInfo.enumValues = {};
226 paramInfos.push_back( paramInfo );
227
228 paramInfo.name = "roff";
229 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
230 paramInfo.unit = "Ω";
231 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
232 paramInfo.defaultValue = "1e+12";
233 paramInfo.description = "Resistance when open";
234 paramInfo.isSpiceInstanceParam = false;
235 paramInfo.spiceModelName = "";
236 paramInfo.enumValues = {};
237 paramInfos.push_back( paramInfo );
238
239 paramInfo.name = "ic";
240 paramInfo.type = SIM_VALUE::TYPE_STRING;
241 paramInfo.unit = "";
242 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
243 paramInfo.defaultValue = "1";
244 paramInfo.description = "Initial state";
245 paramInfo.isSpiceInstanceParam = true;
246 paramInfo.spiceModelName = "";
247 paramInfo.enumValues = { "none", "off", "on" };
248 paramInfos.push_back( paramInfo );
249
250 return paramInfos;
251}
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)
SIM_MODEL()=delete
@ TYPE_STRING
Definition sim_value.h:67
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:54
std::string value
Definition sim_model.h:397
const INFO & info
Definition sim_model.h:398
std::string refName
std::string modelName
std::vector< std::string > pinNetNames
wxString result
Test unit parsing edge cases and error handling.