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/format.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:
33 break;
34
35 case SIM_MODEL::TYPE::SW_I:
36 {
37 std::string vsourceName = fmt::format( "V__{}", aItem.refName );
38
39 wxCHECK_MSG( aItem.pinNetNames.size() >= 2, "", wxS( "Missing two pin net names for SW_I" ) );
40
41 // Current switches measure input current through a voltage source.
42 result.append( fmt::format( "{0} {1} 0\n", aItem.pinNetNames[0], aItem.pinNetNames[1] ) );
43
44 SPICE_ITEM item = aItem;
45 item.modelName = fmt::format( "{0} {1}", vsourceName, aItem.modelName );
46 result.append( SPICE_GENERATOR::ItemLine( item ) );
47 break;
48 }
49
50 default:
51 wxFAIL_MSG( wxS( "Unhandled SIM_MODEL type in SIM_MODEL_SWITCH" ) );
52 break;
53 }
54
55 return result;
56}
57
58
60{
61 std::string result;
62
63 for( int ii = 0; ii < m_model.GetParamCount(); ++ii )
64 {
65 const SIM_MODEL::PARAM& param = m_model.GetParam( ii );
66
67 if( !param.info.isSpiceInstanceParam )
68 continue;
69
70 // The only instance param is "ic", which is positional.
71 std::string value = param.value;
72
73 if( value != "none" )
74 result.append( " " + value );
75 }
76
77 return result;
78}
79
80
81std::vector<std::reference_wrapper<const SIM_MODEL_PIN>> SPICE_GENERATOR_SWITCH::GetPins() const
82{
83 switch( m_model.GetType() )
84 {
85 case SIM_MODEL::TYPE::SW_V:
86 return { m_model.GetPin( 2 ), m_model.GetPin( 3 ), m_model.GetPin( 0 ), m_model.GetPin( 1 ) };
87
88 case SIM_MODEL::TYPE::SW_I:
89 return { m_model.GetPin( 2 ), m_model.GetPin( 3 ) };
90
91 default:
92 wxFAIL_MSG( "Unhandled SIM_MODEL type in SIM_MODEL_SWITCH" );
93 return {};
94 }
95}
96
97
99 SIM_MODEL( aType, std::make_unique<SPICE_GENERATOR_SWITCH>( *this ) )
100{
101 static std::vector<PARAM::INFO> vsw = makeSwVParamInfos();
102 static std::vector<PARAM::INFO> isw = makeSwIParamInfos();
103
104 switch( aType )
105 {
106 case TYPE::SW_V:
107 for( const PARAM::INFO& paramInfo : vsw )
108 AddParam( paramInfo );
109
110 break;
111
112 case TYPE::SW_I:
113 for( const PARAM::INFO& paramInfo : isw )
114 AddParam( paramInfo );
115
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";
133 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
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";
144 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
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 = "Ω";
155 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
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 = "Ω";
166 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
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 = "";
177 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
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";
197 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
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";
208 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
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 = "Ω";
219 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
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 = "Ω";
230 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
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 = "";
241 paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
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)
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:398
const INFO & info
Definition sim_model.h:399
std::string refName
std::string modelName
std::vector< std::string > pinNetNames
wxString result
Test unit parsing edge cases and error handling.