KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sim_model.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 CERN
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 3
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * https://www.gnu.org/licenses/gpl-3.0.html
21 * or you may search the http://www.gnu.org website for the version 3 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <wx/tokenzr.h>
27#include <ki_exception.h>
28#include <lib_symbol.h>
29#include <sch_symbol.h>
30#include <string_utils.h>
31#include <wx/regex.h>
32
33#include <iterator>
34
35#include <sim/sim_model.h>
37#include <sim/sim_model_ideal.h>
40#include <sim/sim_model_r_pot.h>
41#include <sim/sim_model_ibis.h>
46#include <sim/sim_model_tline.h>
48#include <sim/sim_lib_mgr.h>
50
51#include <boost/algorithm/string.hpp>
52#include <fmt/core.h>
53#include <pegtl/contrib/parse_tree.hpp>
54
55
57
58using TYPE = SIM_MODEL::TYPE;
59
60
61SIM_MODEL::DEVICE_INFO SIM_MODEL::DeviceInfo( DEVICE_T aDeviceType )
62{
63 switch( aDeviceType )
64 {
65 // | fieldValue | description | showInMenu |
66 // -------------------------------------------------------
67 //
68 case DEVICE_T::NONE: return { "", "", true };
69 case DEVICE_T::R: return { "R", "Resistor", true };
70 case DEVICE_T::C: return { "C", "Capacitor", true };
71 case DEVICE_T::L: return { "L", "Inductor", true };
72 case DEVICE_T::K: return { "K", "Mutual Inductance Statement", true };
73 case DEVICE_T::TLINE: return { "TLINE", "Transmission Line", true };
74 case DEVICE_T::SW: return { "SW", "Switch", true };
75
76 case DEVICE_T::D: return { "D", "Diode", true };
77 case DEVICE_T::NPN: return { "NPN", "NPN BJT", true };
78 case DEVICE_T::PNP: return { "PNP", "PNP BJT", true };
79
80 case DEVICE_T::NJFET: return { "NJFET", "N-channel JFET", true };
81 case DEVICE_T::PJFET: return { "PJFET", "P-channel JFET", true };
82
83 case DEVICE_T::NMOS: return { "NMOS", "N-channel MOSFET", true };
84 case DEVICE_T::PMOS: return { "PMOS", "P-channel MOSFET", true };
85 case DEVICE_T::NMES: return { "NMES", "N-channel MESFET", true };
86 case DEVICE_T::PMES: return { "PMES", "P-channel MESFET", true };
87
88 case DEVICE_T::V: return { "V", "Voltage Source", true };
89 case DEVICE_T::I: return { "I", "Current Source", true };
90 case DEVICE_T::E: return { "E", "Voltage Source", false };
91 case DEVICE_T::F: return { "F", "Current Source", false };
92 case DEVICE_T::G: return { "G", "Current Source", false };
93 case DEVICE_T::H: return { "H", "Voltage Source", false };
94
95 case DEVICE_T::KIBIS: return { "IBIS", "IBIS Model", false };
96
97 case DEVICE_T::SUBCKT: return { "SUBCKT", "Subcircuit", false };
98 case DEVICE_T::XSPICE: return { "XSPICE", "XSPICE Code Model", true };
99 case DEVICE_T::SPICE: return { "SPICE", "Raw SPICE Element", true };
100
101 default: wxFAIL; return {};
102 }
103}
104
105
106SIM_MODEL::INFO SIM_MODEL::TypeInfo( TYPE aType )
107{
108 switch( aType )
109 {
110 // | deviceType | fieldValue | description |
111 // ---------------------------------------------------------------------
112 //
113 case TYPE::NONE: return { DEVICE_T::NONE, "", "" };
114
115 case TYPE::R: return { DEVICE_T::R, "", "Ideal" };
116 case TYPE::R_POT: return { DEVICE_T::R, "POT", "Potentiometer" };
117 case TYPE::R_BEHAVIORAL: return { DEVICE_T::R, "=", "Behavioral" };
118
119 case TYPE::C: return { DEVICE_T::C, "", "Ideal" };
120 case TYPE::C_BEHAVIORAL: return { DEVICE_T::C, "=", "Behavioral" };
121
122 case TYPE::L: return { DEVICE_T::L, "", "Ideal" };
123 case TYPE::L_BEHAVIORAL: return { DEVICE_T::L, "=", "Behavioral" };
124
125 case TYPE::K: return { DEVICE_T::K, "", "Mutual Inductance Statement" };
126
127 case TYPE::TLINE_Z0: return { DEVICE_T::TLINE, "", "Characteristic impedance" };
128 case TYPE::TLINE_RLGC: return { DEVICE_T::TLINE, "RLGC", "RLGC" };
129
130 case TYPE::SW_V: return { DEVICE_T::SW, "V", "Voltage-controlled" };
131 case TYPE::SW_I: return { DEVICE_T::SW, "I", "Current-controlled" };
132
133 case TYPE::D: return { DEVICE_T::D, "", "" };
134
135 case TYPE::NPN_VBIC: return { DEVICE_T::NPN, "VBIC", "VBIC" };
136 case TYPE::PNP_VBIC: return { DEVICE_T::PNP, "VBIC", "VBIC" };
137 case TYPE::NPN_GUMMELPOON: return { DEVICE_T::NPN, "GUMMELPOON", "Gummel-Poon" };
138 case TYPE::PNP_GUMMELPOON: return { DEVICE_T::PNP, "GUMMELPOON", "Gummel-Poon" };
139 //case TYPE::BJT_MEXTRAM: return {};
140 case TYPE::NPN_HICUM2: return { DEVICE_T::NPN, "HICUML2", "HICUM level 2" };
141 case TYPE::PNP_HICUM2: return { DEVICE_T::PNP, "HICUML2", "HICUM level 2" };
142 //case TYPE::BJT_HICUM_L0: return {};
143
144 case TYPE::NJFET_SHICHMANHODGES: return { DEVICE_T::NJFET, "SHICHMANHODGES", "Shichman-Hodges" };
145 case TYPE::PJFET_SHICHMANHODGES: return { DEVICE_T::PJFET, "SHICHMANHODGES", "Shichman-Hodges" };
146 case TYPE::NJFET_PARKERSKELLERN: return { DEVICE_T::NJFET, "PARKERSKELLERN", "Parker-Skellern" };
147 case TYPE::PJFET_PARKERSKELLERN: return { DEVICE_T::PJFET, "PARKERSKELLERN", "Parker-Skellern" };
148
149 case TYPE::NMES_STATZ: return { DEVICE_T::NMES, "STATZ", "Statz" };
150 case TYPE::PMES_STATZ: return { DEVICE_T::PMES, "STATZ", "Statz" };
151 case TYPE::NMES_YTTERDAL: return { DEVICE_T::NMES, "YTTERDAL", "Ytterdal" };
152 case TYPE::PMES_YTTERDAL: return { DEVICE_T::PMES, "YTTERDAL", "Ytterdal" };
153 case TYPE::NMES_HFET1: return { DEVICE_T::NMES, "HFET1", "HFET1" };
154 case TYPE::PMES_HFET1: return { DEVICE_T::PMES, "HFET1", "HFET1" };
155 case TYPE::NMES_HFET2: return { DEVICE_T::NMES, "HFET2", "HFET2" };
156 case TYPE::PMES_HFET2: return { DEVICE_T::PMES, "HFET2", "HFET2" };
157
158 case TYPE::NMOS_VDMOS: return { DEVICE_T::NMOS, "VDMOS", "VDMOS" };
159 case TYPE::PMOS_VDMOS: return { DEVICE_T::PMOS, "VDMOS", "VDMOS" };
160 case TYPE::NMOS_MOS1: return { DEVICE_T::NMOS, "MOS1", "Classical quadratic (MOS1)" };
161 case TYPE::PMOS_MOS1: return { DEVICE_T::PMOS, "MOS1", "Classical quadratic (MOS1)" };
162 case TYPE::NMOS_MOS2: return { DEVICE_T::NMOS, "MOS2", "Grove-Frohman (MOS2)" };
163 case TYPE::PMOS_MOS2: return { DEVICE_T::PMOS, "MOS2", "Grove-Frohman (MOS2)" };
164 case TYPE::NMOS_MOS3: return { DEVICE_T::NMOS, "MOS3", "MOS3" };
165 case TYPE::PMOS_MOS3: return { DEVICE_T::PMOS, "MOS3", "MOS3" };
166 case TYPE::NMOS_BSIM1: return { DEVICE_T::NMOS, "BSIM1", "BSIM1" };
167 case TYPE::PMOS_BSIM1: return { DEVICE_T::PMOS, "BSIM1", "BSIM1" };
168 case TYPE::NMOS_BSIM2: return { DEVICE_T::NMOS, "BSIM2", "BSIM2" };
169 case TYPE::PMOS_BSIM2: return { DEVICE_T::PMOS, "BSIM2", "BSIM2" };
170 case TYPE::NMOS_MOS6: return { DEVICE_T::NMOS, "MOS6", "MOS6" };
171 case TYPE::PMOS_MOS6: return { DEVICE_T::PMOS, "MOS6", "MOS6" };
172 case TYPE::NMOS_BSIM3: return { DEVICE_T::NMOS, "BSIM3", "BSIM3" };
173 case TYPE::PMOS_BSIM3: return { DEVICE_T::PMOS, "BSIM3", "BSIM3" };
174 case TYPE::NMOS_MOS9: return { DEVICE_T::NMOS, "MOS9", "MOS9" };
175 case TYPE::PMOS_MOS9: return { DEVICE_T::PMOS, "MOS9", "MOS9" };
176 case TYPE::NMOS_B4SOI: return { DEVICE_T::NMOS, "B4SOI", "BSIM4 SOI (B4SOI)" };
177 case TYPE::PMOS_B4SOI: return { DEVICE_T::PMOS, "B4SOI", "BSIM4 SOI (B4SOI)" };
178 case TYPE::NMOS_BSIM4: return { DEVICE_T::NMOS, "BSIM4", "BSIM4" };
179 case TYPE::PMOS_BSIM4: return { DEVICE_T::PMOS, "BSIM4", "BSIM4" };
180 //case TYPE::NMOS_EKV2_6: return {};
181 //case TYPE::PMOS_EKV2_6: return {};
182 //case TYPE::NMOS_PSP: return {};
183 //case TYPE::PMOS_PSP: return {};
184 case TYPE::NMOS_B3SOIFD: return { DEVICE_T::NMOS, "B3SOIFD", "B3SOIFD (BSIM3 FD-SOI)" };
185 case TYPE::PMOS_B3SOIFD: return { DEVICE_T::PMOS, "B3SOIFD", "B3SOIFD (BSIM3 FD-SOI)" };
186 case TYPE::NMOS_B3SOIDD: return { DEVICE_T::NMOS, "B3SOIDD", "B3SOIDD (BSIM3 SOI)" };
187 case TYPE::PMOS_B3SOIDD: return { DEVICE_T::PMOS, "B3SOIDD", "B3SOIDD (BSIM3 SOI)" };
188 case TYPE::NMOS_B3SOIPD: return { DEVICE_T::NMOS, "B3SOIPD", "B3SOIPD (BSIM3 PD-SOI)" };
189 case TYPE::PMOS_B3SOIPD: return { DEVICE_T::PMOS, "B3SOIPD", "B3SOIPD (BSIM3 PD-SOI)" };
190 //case TYPE::NMOS_STAG: return {};
191 //case TYPE::PMOS_STAG: return {};
192 case TYPE::NMOS_HISIM2: return { DEVICE_T::NMOS, "HISIM2", "HiSIM2" };
193 case TYPE::PMOS_HISIM2: return { DEVICE_T::PMOS, "HISIM2", "HiSIM2" };
194 case TYPE::NMOS_HISIMHV1: return { DEVICE_T::NMOS, "HISIMHV1", "HiSIM_HV1" };
195 case TYPE::PMOS_HISIMHV1: return { DEVICE_T::PMOS, "HISIMHV1", "HiSIM_HV1" };
196 case TYPE::NMOS_HISIMHV2: return { DEVICE_T::NMOS, "HISIMHV2", "HiSIM_HV2" };
197 case TYPE::PMOS_HISIMHV2: return { DEVICE_T::PMOS, "HISIMHV2", "HiSIM_HV2" };
198
199 case TYPE::V: return { DEVICE_T::V, "DC", "DC", };
200 case TYPE::V_SIN: return { DEVICE_T::V, "SIN", "Sine" };
201 case TYPE::V_PULSE: return { DEVICE_T::V, "PULSE", "Pulse" };
202 case TYPE::V_EXP: return { DEVICE_T::V, "EXP", "Exponential" };
203 case TYPE::V_AM: return { DEVICE_T::V, "AM", "Amplitude modulated" };
204 case TYPE::V_SFFM: return { DEVICE_T::V, "SFFM", "Single-frequency FM" };
205 case TYPE::V_VCL: return { DEVICE_T::E, "", "Voltage-controlled" };
206 case TYPE::V_CCL: return { DEVICE_T::H, "", "Current-controlled" };
207 case TYPE::V_PWL: return { DEVICE_T::V, "PWL", "Piecewise linear" };
208 case TYPE::V_WHITENOISE: return { DEVICE_T::V, "WHITENOISE", "White noise" };
209 case TYPE::V_PINKNOISE: return { DEVICE_T::V, "PINKNOISE", "Pink noise (1/f)" };
210 case TYPE::V_BURSTNOISE: return { DEVICE_T::V, "BURSTNOISE", "Burst noise" };
211 case TYPE::V_RANDUNIFORM: return { DEVICE_T::V, "RANDUNIFORM", "Random uniform" };
212 case TYPE::V_RANDGAUSSIAN: return { DEVICE_T::V, "RANDGAUSSIAN", "Random Gaussian" };
213 case TYPE::V_RANDEXP: return { DEVICE_T::V, "RANDEXP", "Random exponential" };
214 case TYPE::V_RANDPOISSON: return { DEVICE_T::V, "RANDPOISSON", "Random Poisson" };
215 case TYPE::V_BEHAVIORAL: return { DEVICE_T::V, "=", "Behavioral" };
216
217 case TYPE::I: return { DEVICE_T::I, "DC", "DC", };
218 case TYPE::I_SIN: return { DEVICE_T::I, "SIN", "Sine" };
219 case TYPE::I_PULSE: return { DEVICE_T::I, "PULSE", "Pulse" };
220 case TYPE::I_EXP: return { DEVICE_T::I, "EXP", "Exponential" };
221 case TYPE::I_AM: return { DEVICE_T::I, "AM", "Amplitude modulated" };
222 case TYPE::I_SFFM: return { DEVICE_T::I, "SFFM", "Single-frequency FM" };
223 case TYPE::I_VCL: return { DEVICE_T::G, "", "Voltage-controlled" };
224 case TYPE::I_CCL: return { DEVICE_T::F, "", "Current-controlled" };
225 case TYPE::I_PWL: return { DEVICE_T::I, "PWL", "Piecewise linear" };
226 case TYPE::I_WHITENOISE: return { DEVICE_T::I, "WHITENOISE", "White noise" };
227 case TYPE::I_PINKNOISE: return { DEVICE_T::I, "PINKNOISE", "Pink noise (1/f)" };
228 case TYPE::I_BURSTNOISE: return { DEVICE_T::I, "BURSTNOISE", "Burst noise" };
229 case TYPE::I_RANDUNIFORM: return { DEVICE_T::I, "RANDUNIFORM", "Random uniform" };
230 case TYPE::I_RANDGAUSSIAN: return { DEVICE_T::I, "RANDGAUSSIAN", "Random Gaussian" };
231 case TYPE::I_RANDEXP: return { DEVICE_T::I, "RANDEXP", "Random exponential" };
232 case TYPE::I_RANDPOISSON: return { DEVICE_T::I, "RANDPOISSON", "Random Poisson" };
233 case TYPE::I_BEHAVIORAL: return { DEVICE_T::I, "=", "Behavioral" };
234
235 case TYPE::SUBCKT: return { DEVICE_T::SUBCKT, "", "Subcircuit" };
236 case TYPE::XSPICE: return { DEVICE_T::XSPICE, "", "" };
237
238 case TYPE::KIBIS_DEVICE: return { DEVICE_T::KIBIS, "DEVICE", "Device" };
239 case TYPE::KIBIS_DRIVER_DC: return { DEVICE_T::KIBIS, "DCDRIVER", "DC driver" };
240 case TYPE::KIBIS_DRIVER_RECT: return { DEVICE_T::KIBIS, "RECTDRIVER", "Rectangular wave driver" };
241 case TYPE::KIBIS_DRIVER_PRBS: return { DEVICE_T::KIBIS, "PRBSDRIVER", "PRBS driver" };
242
243 case TYPE::RAWSPICE: return { DEVICE_T::SPICE, "", "" };
244
245 default: wxFAIL; return {};
246 }
247}
248
249
251{
252 switch( aType )
253 {
254 // | itemType | modelType | fnName | level |isDefaultLvl|hasExpr|version|
255 // -------------------------------------------------------------------------
256 case TYPE::R: return { "R", "" };
257 case TYPE::R_POT: return { "A", "" };
258 case TYPE::R_BEHAVIORAL: return { "R", "", "", "0", false, true };
259
260 case TYPE::C: return { "C", "" };
261 case TYPE::C_BEHAVIORAL: return { "C", "", "", "0", false, true };
262
263 case TYPE::L: return { "L", "" };
264 case TYPE::L_BEHAVIORAL: return { "L", "", "", "0", false, true };
265
266 case TYPE::K: return { "K", "" };
267
268 //case TYPE::TLINE_Z0: return { "T" };
269 case TYPE::TLINE_Z0: return { "O", "LTRA" };
270 case TYPE::TLINE_RLGC: return { "O", "LTRA" };
271
272 case TYPE::SW_V: return { "S", "SW" };
273 case TYPE::SW_I: return { "W", "CSW" };
274
275 case TYPE::D: return { "D", "D" };
276
277 case TYPE::NPN_VBIC: return { "Q", "NPN", "", "4" };
278 case TYPE::PNP_VBIC: return { "Q", "PNP", "", "4" };
279 case TYPE::NPN_GUMMELPOON: return { "Q", "NPN", "", "1", true };
280 case TYPE::PNP_GUMMELPOON: return { "Q", "PNP", "", "1", true };
281 case TYPE::NPN_HICUM2: return { "Q", "NPN", "", "8" };
282 case TYPE::PNP_HICUM2: return { "Q", "PNP", "", "8" };
283
284 case TYPE::NJFET_SHICHMANHODGES: return { "J", "NJF", "", "1", true };
285 case TYPE::PJFET_SHICHMANHODGES: return { "J", "PJF", "", "1", true };
286 case TYPE::NJFET_PARKERSKELLERN: return { "J", "NJF", "", "2" };
287 case TYPE::PJFET_PARKERSKELLERN: return { "J", "PJF", "", "2" };
288
289 case TYPE::NMES_STATZ: return { "Z", "NMF", "", "1", true };
290 case TYPE::PMES_STATZ: return { "Z", "PMF", "", "1", true };
291 case TYPE::NMES_YTTERDAL: return { "Z", "NMF", "", "2" };
292 case TYPE::PMES_YTTERDAL: return { "Z", "PMF", "", "2" };
293 case TYPE::NMES_HFET1: return { "Z", "NMF", "", "5" };
294 case TYPE::PMES_HFET1: return { "Z", "PMF", "", "5" };
295 case TYPE::NMES_HFET2: return { "Z", "NMF", "", "6" };
296 case TYPE::PMES_HFET2: return { "Z", "PMF", "", "6" };
297
298 case TYPE::NMOS_VDMOS: return { "M", "VDMOS NCHAN" };
299 case TYPE::PMOS_VDMOS: return { "M", "VDMOS PCHAN" };
300 case TYPE::NMOS_MOS1: return { "M", "NMOS", "", "1", true };
301 case TYPE::PMOS_MOS1: return { "M", "PMOS", "", "1", true };
302 case TYPE::NMOS_MOS2: return { "M", "NMOS", "", "2" };
303 case TYPE::PMOS_MOS2: return { "M", "PMOS", "", "2" };
304 case TYPE::NMOS_MOS3: return { "M", "NMOS", "", "3" };
305 case TYPE::PMOS_MOS3: return { "M", "PMOS", "", "3" };
306 case TYPE::NMOS_BSIM1: return { "M", "NMOS", "", "4" };
307 case TYPE::PMOS_BSIM1: return { "M", "PMOS", "", "4" };
308 case TYPE::NMOS_BSIM2: return { "M", "NMOS", "", "5" };
309 case TYPE::PMOS_BSIM2: return { "M", "PMOS", "", "5" };
310 case TYPE::NMOS_MOS6: return { "M", "NMOS", "", "6" };
311 case TYPE::PMOS_MOS6: return { "M", "PMOS", "", "6" };
312 case TYPE::NMOS_BSIM3: return { "M", "NMOS", "", "8" };
313 case TYPE::PMOS_BSIM3: return { "M", "PMOS", "", "8" };
314 case TYPE::NMOS_MOS9: return { "M", "NMOS", "", "9" };
315 case TYPE::PMOS_MOS9: return { "M", "PMOS", "", "9" };
316 case TYPE::NMOS_B4SOI: return { "M", "NMOS", "", "10" };
317 case TYPE::PMOS_B4SOI: return { "M", "PMOS", "", "10" };
318 case TYPE::NMOS_BSIM4: return { "M", "NMOS", "", "14" };
319 case TYPE::PMOS_BSIM4: return { "M", "PMOS", "", "14" };
320 //case TYPE::NMOS_EKV2_6: return {};
321 //case TYPE::PMOS_EKV2_6: return {};
322 //case TYPE::NMOS_PSP: return {};
323 //case TYPE::PMOS_PSP: return {};
324 case TYPE::NMOS_B3SOIFD: return { "M", "NMOS", "", "55" };
325 case TYPE::PMOS_B3SOIFD: return { "M", "PMOS", "", "55" };
326 case TYPE::NMOS_B3SOIDD: return { "M", "NMOS", "", "56" };
327 case TYPE::PMOS_B3SOIDD: return { "M", "PMOS", "", "56" };
328 case TYPE::NMOS_B3SOIPD: return { "M", "NMOS", "", "57" };
329 case TYPE::PMOS_B3SOIPD: return { "M", "PMOS", "", "57" };
330 //case TYPE::NMOS_STAG: return {};
331 //case TYPE::PMOS_STAG: return {};
332 case TYPE::NMOS_HISIM2: return { "M", "NMOS", "", "68" };
333 case TYPE::PMOS_HISIM2: return { "M", "PMOS", "", "68" };
334 case TYPE::NMOS_HISIMHV1: return { "M", "NMOS", "", "73", false, false, "1.2.4" };
335 case TYPE::PMOS_HISIMHV1: return { "M", "PMOS", "", "73", false, false, "1.2.4" };
336 case TYPE::NMOS_HISIMHV2: return { "M", "NMOS", "", "73", false, false, "2.2.0" };
337 case TYPE::PMOS_HISIMHV2: return { "M", "PMOS", "", "73", false, false, "2.2.0" };
338
339 case TYPE::V: return { "V", "", "DC" };
340 case TYPE::V_SIN: return { "V", "", "SIN" };
341 case TYPE::V_PULSE: return { "V", "", "PULSE" };
342 case TYPE::V_EXP: return { "V", "", "EXP" };
343 case TYPE::V_AM: return { "V", "", "AM" };
344 case TYPE::V_SFFM: return { "V", "", "SFFM" };
345 case TYPE::V_VCL: return { "E", "", "" };
346 case TYPE::V_CCL: return { "H", "", "" };
347 case TYPE::V_PWL: return { "V", "", "PWL" };
348 case TYPE::V_WHITENOISE: return { "V", "", "TRNOISE" };
349 case TYPE::V_PINKNOISE: return { "V", "", "TRNOISE" };
350 case TYPE::V_BURSTNOISE: return { "V", "", "TRNOISE" };
351 case TYPE::V_RANDUNIFORM: return { "V", "", "TRRANDOM" };
352 case TYPE::V_RANDGAUSSIAN: return { "V", "", "TRRANDOM" };
353 case TYPE::V_RANDEXP: return { "V", "", "TRRANDOM" };
354 case TYPE::V_RANDPOISSON: return { "V", "", "TRRANDOM" };
355 case TYPE::V_BEHAVIORAL: return { "B" };
356
357 case TYPE::I: return { "I", "", "DC" };
358 case TYPE::I_PULSE: return { "I", "", "PULSE" };
359 case TYPE::I_SIN: return { "I", "", "SIN" };
360 case TYPE::I_EXP: return { "I", "", "EXP" };
361 case TYPE::I_AM: return { "I", "", "AM" };
362 case TYPE::I_SFFM: return { "I", "", "SFFM" };
363 case TYPE::I_VCL: return { "G", "", "" };
364 case TYPE::I_CCL: return { "F", "", "" };
365 case TYPE::I_PWL: return { "I", "", "PWL" };
366 case TYPE::I_WHITENOISE: return { "I", "", "TRNOISE" };
367 case TYPE::I_PINKNOISE: return { "I", "", "TRNOISE" };
368 case TYPE::I_BURSTNOISE: return { "I", "", "TRNOISE" };
369 case TYPE::I_RANDUNIFORM: return { "I", "", "TRRANDOM" };
370 case TYPE::I_RANDGAUSSIAN: return { "I", "", "TRRANDOM" };
371 case TYPE::I_RANDEXP: return { "I", "", "TRRANDOM" };
372 case TYPE::I_RANDPOISSON: return { "I", "", "TRRANDOM" };
373 case TYPE::I_BEHAVIORAL: return { "B" };
374
375 case TYPE::SUBCKT: return { "X" };
376 case TYPE::XSPICE: return { "A" };
377
378 case TYPE::KIBIS_DEVICE: return { "X" };
379 case TYPE::KIBIS_DRIVER_DC: return { "X" };
380 case TYPE::KIBIS_DRIVER_RECT: return { "X" };
381 case TYPE::KIBIS_DRIVER_PRBS: return { "X" };
382
383 case TYPE::NONE:
384 case TYPE::RAWSPICE: return {};
385
386 default: wxFAIL; return {};
387 }
388}
389
390
391TYPE SIM_MODEL::ReadTypeFromFields( const std::vector<SCH_FIELD>& aFields, bool aResolve, int aDepth,
392 REPORTER& aReporter )
393{
394 std::string deviceTypeFieldValue = GetFieldValue( &aFields, SIM_DEVICE_FIELD, aResolve, aDepth );
395 std::string typeFieldValue = GetFieldValue( &aFields, SIM_DEVICE_SUBTYPE_FIELD, aResolve, aDepth );
396
397 if( !deviceTypeFieldValue.empty() )
398 {
399 for( TYPE type : TYPE_ITERATOR() )
400 {
401 if( typeFieldValue == TypeInfo( type ).fieldValue )
402 {
403 if( deviceTypeFieldValue == DeviceInfo( TypeInfo( type ).deviceType ).fieldValue )
404 return type;
405 }
406 }
407 }
408
409 if( typeFieldValue.empty() )
410 return TYPE::NONE;
411
412 wxString reference = GetFieldValue( &aFields, FIELD_T::REFERENCE );
413
414 if( !reference.IsEmpty() )
415 {
416 aReporter.Report( wxString::Format( _( "No simulation model definition found for "
417 "symbol '%s'." ),
418 reference ),
420 }
421 else
422 {
423 aReporter.Report( _( "No simulation model definition found." ),
425 }
426
427 return TYPE::NONE;
428}
429
430
431void SIM_MODEL::ReadDataFields( const std::vector<SCH_FIELD>* aFields, bool aResolve, int aDepth,
432 const std::vector<SCH_PIN*>& aPins )
433{
434 bool diffMode = GetFieldValue( aFields, SIM_LIBRARY_IBIS::DIFF_FIELD, aResolve, aDepth ) == "1";
435 SwitchSingleEndedDiff( diffMode );
436
437 m_serializer->ParseEnable( GetFieldValue( aFields, SIM_LEGACY_ENABLE_FIELD_V7, aResolve, aDepth ) );
438
439 createPins( aPins );
440 m_serializer->ParsePins( GetFieldValue( aFields, SIM_PINS_FIELD, aResolve, aDepth ) );
441
442 std::string paramsField = GetFieldValue( aFields, SIM_PARAMS_FIELD, aResolve, aDepth );
443
444 if( !m_serializer->ParseParams( paramsField ) )
445 m_serializer->ParseValue( GetFieldValue( aFields, SIM_VALUE_FIELD, aResolve, aDepth ) );
446}
447
448
449void SIM_MODEL::WriteFields( std::vector<SCH_FIELD>& aFields, const SCH_SHEET_PATH* aSheetPath,
450 const wxString& aVariantName ) const
451{
452 // Remove duplicate fields: they are at the end of list
453 for( size_t ii = aFields.size() - 1; ii > 0; ii-- )
454 {
455 wxString currFieldName = aFields[ii].GetName();
456
457 auto end_candidate_list = aFields.begin() + ii - 1;
458
459 auto fieldIt = std::find_if( aFields.begin(), end_candidate_list,
460 [&]( const SCH_FIELD& f )
461 {
462 return f.GetName() == currFieldName;
463 } );
464
465 // If duplicate field found: remove current checked item
466 if( fieldIt != end_candidate_list )
467 aFields.erase( aFields.begin() + ii );
468 }
469
470 SetFieldValue( aFields, SIM_DEVICE_FIELD, m_serializer->GenerateDevice(), false,
471 aSheetPath, aVariantName );
472 SetFieldValue( aFields, SIM_DEVICE_SUBTYPE_FIELD, m_serializer->GenerateDeviceSubtype(), false,
473 aSheetPath, aVariantName );
474
475 SetFieldValue( aFields, SIM_LEGACY_ENABLE_FIELD_V7, m_serializer->GenerateEnable(), false,
476 aSheetPath, aVariantName );
477 SetFieldValue( aFields, SIM_PINS_FIELD, m_serializer->GeneratePins(), false, aSheetPath, aVariantName );
478
479 SetFieldValue( aFields, SIM_PARAMS_FIELD, m_serializer->GenerateParams(), false, aSheetPath, aVariantName );
480
481 if( IsStoredInValue() )
482 SetFieldValue( aFields, SIM_VALUE_FIELD, m_serializer->GenerateValue(), false, aSheetPath, aVariantName );
483}
484
485
486std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( TYPE aType, const std::vector<SCH_PIN*>& aPins,
487 REPORTER& aReporter )
488{
489 std::unique_ptr<SIM_MODEL> model = Create( aType );
490
491 try
492 {
493 // Passing nullptr to ReadDataFields will make it act as if all fields were empty.
494 model->ReadDataFields( static_cast<const std::vector<SCH_FIELD>*>( nullptr ),
495 false, 0, aPins );
496 }
497 catch( IO_ERROR& )
498 {
499 wxFAIL_MSG( "Shouldn't throw reading empty fields!" );
500 }
501
502 return model;
503}
504
505
506std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( const SIM_MODEL* aBaseModel,
507 const std::vector<SCH_PIN*>& aPins,
508 REPORTER& aReporter )
509{
510 std::unique_ptr<SIM_MODEL> model;
511
512 if( aBaseModel )
513 {
514 TYPE type = aBaseModel->GetType();
515
516 if( dynamic_cast<const SIM_MODEL_SPICE_FALLBACK*>( aBaseModel ) )
517 model = std::make_unique<SIM_MODEL_SPICE_FALLBACK>( type );
518 else if( dynamic_cast< const SIM_MODEL_RAW_SPICE*>( aBaseModel ) )
519 model = std::make_unique<SIM_MODEL_RAW_SPICE>();
520 else
521 model = Create( type );
522
523 model->SetBaseModel( *aBaseModel );
524 }
525 else // No base model means the model wasn't found in the library, so create a fallback
526 {
527 model = std::make_unique<SIM_MODEL_SPICE_FALLBACK>( TYPE::NONE );
528 }
529
530 try
531 {
532 model->ReadDataFields( static_cast<const std::vector<SCH_FIELD>*>( nullptr ),
533 false, 0, aPins );
534 }
535 catch( IO_ERROR& )
536 {
537 wxFAIL_MSG( "Shouldn't throw reading empty fields!" );
538 }
539
540 return model;
541}
542
543
544std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( const SIM_MODEL* aBaseModel,
545 const std::vector<SCH_PIN*>& aPins,
546 const std::vector<SCH_FIELD>& aFields,
547 bool aResolve, int aDepth, REPORTER& aReporter )
548{
549 std::unique_ptr<SIM_MODEL> model;
550
551 if( aBaseModel )
552 {
553 NULL_REPORTER devnull;
554 TYPE type = aBaseModel->GetType();
555 TYPE type_override = ReadTypeFromFields( aFields, aResolve, aDepth, devnull );
556
557 // Check for an override in the case of IBIS models.
558 // The other models require type to be set from the base model.
559 if( dynamic_cast<const SIM_MODEL_IBIS*>( aBaseModel ) &&
560 type_override != TYPE::NONE )
561 {
562 type = type_override;
563 }
564
565 if( dynamic_cast<const SIM_MODEL_SPICE_FALLBACK*>( aBaseModel ) )
566 model = std::make_unique<SIM_MODEL_SPICE_FALLBACK>( type );
567 else if( dynamic_cast< const SIM_MODEL_RAW_SPICE*>( aBaseModel ) )
568 model = std::make_unique<SIM_MODEL_RAW_SPICE>();
569 else
570 model = Create( type );
571
572 model->SetBaseModel( *aBaseModel );
573 }
574 else // No base model means the model wasn't found in the library, so create a fallback
575 {
576 TYPE type = ReadTypeFromFields( aFields, aResolve, aDepth, aReporter );
577 model = std::make_unique<SIM_MODEL_SPICE_FALLBACK>( type );
578 }
579
580 try
581 {
582 model->ReadDataFields( &aFields, aResolve, aDepth, aPins );
583 }
584 catch( IO_ERROR& err )
585 {
586 aReporter.Report( wxString::Format( _( "Error reading simulation model from symbol '%s':\n%s" ),
588 err.Problem() ),
590 }
591
592 return model;
593}
594
595
596std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( const std::vector<SCH_FIELD>& aFields,
597 bool aResolve, int aDepth,
598 const std::vector<SCH_PIN*>& aPins, REPORTER& aReporter )
599{
600 TYPE type = ReadTypeFromFields( aFields, aResolve, aDepth, aReporter );
601 std::unique_ptr<SIM_MODEL> model = SIM_MODEL::Create( type );
602
603 try
604 {
605 model->ReadDataFields( &aFields, aResolve, aDepth, aPins );
606 }
607 catch( const IO_ERROR& parse_err )
608 {
609 if( !aResolve )
610 {
611 aReporter.Report( parse_err.What(), RPT_SEVERITY_ERROR );
612 return model;
613 }
614
615 // Just because we can't parse it doesn't mean that a SPICE interpreter can't. Fall
616 // back to a raw spice code model.
617
618 std::string modelData = GetFieldValue( &aFields, SIM_PARAMS_FIELD, aResolve, aDepth );
619
620 if( modelData.empty() )
621 modelData = GetFieldValue( &aFields, SIM_VALUE_FIELD, aResolve, aDepth );
622
623 model = std::make_unique<SIM_MODEL_RAW_SPICE>( modelData );
624
625 try
626 {
627 model->createPins( aPins );
628 model->m_serializer->ParsePins( GetFieldValue( &aFields, SIM_PINS_FIELD, aResolve, aDepth ) );
629 }
630 catch( const IO_ERROR& err )
631 {
632 // We own the pin syntax, so if we can't parse it then there's an error.
633 aReporter.Report( wxString::Format( _( "Error reading simulation model from symbol '%s':\n%s" ),
635 err.Problem() ),
637 }
638 }
639
640 return model;
641}
642
643
644SIM_MODEL::~SIM_MODEL() = default;
645
646
648{
649 m_modelPins.push_back( aPin );
650}
651
652
654{
655 m_modelPins.clear();
656}
657
658
659int SIM_MODEL::FindModelPinIndex( const std::string& aSymbolPinNumber )
660{
661 for( int modelPinIndex = 0; modelPinIndex < GetPinCount(); ++modelPinIndex )
662 {
663 if( GetPin( modelPinIndex ).symbolPinNumber == aSymbolPinNumber )
664 return modelPinIndex;
665 }
666
668}
669
670
672{
673 m_params.emplace_back( aInfo );
674
675 // Enums are initialized with their default values.
676 if( aInfo.enumValues.size() >= 1 )
677 m_params.back().value = aInfo.defaultValue;
678}
679
680
681void SIM_MODEL::SetBaseModel( const SIM_MODEL& aBaseModel )
682{
683 wxASSERT_MSG( GetType() == aBaseModel.GetType(),
684 wxS( "Simulation model type must be the same as its base class!" ) );
685
686 m_baseModel = &aBaseModel;
687}
688
689
690std::vector<std::reference_wrapper<const SIM_MODEL_PIN>> SIM_MODEL::GetPins() const
691{
692 std::vector<std::reference_wrapper<const SIM_MODEL_PIN>> pins;
693
694 for( int modelPinIndex = 0; modelPinIndex < GetPinCount(); ++modelPinIndex )
695 pins.emplace_back( GetPin( modelPinIndex ) );
696
697 return pins;
698}
699
701 const wxString& aSymbolPinNumber )
702{
703 if( aModelPinIndex >= 0 && aModelPinIndex < (int) m_modelPins.size() )
704 m_modelPins.at( aModelPinIndex ).symbolPinNumber = aSymbolPinNumber;
705}
706
707
708void SIM_MODEL::AssignSymbolPinNumberToModelPin( const std::string& aModelPinName,
709 const wxString& aSymbolPinNumber )
710{
712 {
713 if( pin.modelPinName == aModelPinName )
714 {
715 pin.symbolPinNumber = aSymbolPinNumber;
716 return;
717 }
718 }
719
720 // If aPinName wasn't in fact a name, see if it's a raw (1-based) index. This is required
721 // for legacy files which didn't use pin names.
722 int pinIndex = (int) strtol( aModelPinName.c_str(), nullptr, 10 );
723
724 if( pinIndex < 1 || pinIndex > (int) m_modelPins.size() )
725 THROW_IO_ERROR( wxString::Format( _( "Unknown simulation model pin '%s'" ), aModelPinName ) );
726
727 m_modelPins[ --pinIndex /* convert to 0-based */ ].symbolPinNumber = aSymbolPinNumber;
728}
729
730
731const SIM_MODEL::PARAM& SIM_MODEL::GetParam( unsigned aParamIndex ) const
732{
733 if( m_baseModel && m_params.at( aParamIndex ).value == "" )
734 return m_baseModel->GetParam( aParamIndex );
735 else
736 return m_params.at( aParamIndex );
737}
738
739
740bool SIM_MODEL::PARAM::INFO::Matches( const std::string& aParamName ) const
741{
742 return boost::iequals( name, aParamName );
743}
744
745
746int SIM_MODEL::doFindParam( const std::string& aParamName ) const
747{
748 for( int ii = 0; ii < (int) GetParamCount(); ++ii )
749 {
750 if( GetParam( ii ).Matches( aParamName ) )
751 return ii;
752 }
753
754 return -1;
755}
756
757
758const SIM_MODEL::PARAM* SIM_MODEL::FindParam( const std::string& aParamName ) const
759{
760 int idx = doFindParam( aParamName );
761
762 return idx >= 0 ? &GetParam( idx ) : nullptr;
763}
764
765
766const SIM_MODEL::PARAM& SIM_MODEL::GetParamOverride( unsigned aParamIndex ) const
767{
768 return m_params.at( aParamIndex );
769}
770
771
772const SIM_MODEL::PARAM& SIM_MODEL::GetBaseParam( unsigned aParamIndex ) const
773{
774 if( m_baseModel )
775 return m_baseModel->GetParam( aParamIndex );
776 else
777 return m_params.at( aParamIndex );
778}
779
780
781void SIM_MODEL::doSetParamValue( int aParamIndex, const std::string& aValue )
782{
783 m_params.at( aParamIndex ).value = aValue;
784}
785
786
787void SIM_MODEL::SetParamValue( int aParamIndex, const std::string& aValue,
788 SIM_VALUE::NOTATION aNotation )
789{
790 // Notation conversion is very slow. Avoid if possible.
791
792 auto plainNumber =
793 []( const std::string& aString )
794 {
795 for( char c : aString )
796 {
797 if( c != '.' && ( c < '0' || c > '9' ) )
798 return false;
799 }
800
801 return true;
802 };
803
804
805 if( aValue.find( ',' ) != std::string::npos )
806 {
807 doSetParamValue( aParamIndex, SIM_VALUE::ConvertNotation( aValue, aNotation,
808 SIM_VALUE::NOTATION::SI ) );
809 }
810 else if( aNotation != SIM_VALUE::NOTATION::SI && !plainNumber( aValue ) )
811 {
812 doSetParamValue( aParamIndex, SIM_VALUE::ConvertNotation( aValue, aNotation,
813 SIM_VALUE::NOTATION::SI ) );
814 }
815 else
816 {
817 doSetParamValue( aParamIndex, aValue );
818 }
819}
820
821
822void SIM_MODEL::SetParamValue( const std::string& aParamName, const std::string& aValue,
823 SIM_VALUE::NOTATION aNotation )
824{
825 int idx = doFindParam( aParamName );
826
827 if( idx < 0 )
828 THROW_IO_ERROR( wxString::Format( "Unknown simulation model parameter '%s'", aParamName ) );
829
830 SetParamValue( idx, aValue, aNotation );
831}
832
833
834std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( TYPE aType )
835{
836 switch( aType )
837 {
838 case TYPE::R:
839 case TYPE::C:
840 case TYPE::L:
841 return std::make_unique<SIM_MODEL_IDEAL>( aType );
842
843 case TYPE::R_POT:
844 return std::make_unique<SIM_MODEL_R_POT>();
845
846 case TYPE::K:
847 return std::make_unique<SIM_MODEL_L_MUTUAL>();
848
849 case TYPE::R_BEHAVIORAL:
850 case TYPE::C_BEHAVIORAL:
851 case TYPE::L_BEHAVIORAL:
852 case TYPE::V_BEHAVIORAL:
853 case TYPE::I_BEHAVIORAL:
854 return std::make_unique<SIM_MODEL_BEHAVIORAL>( aType );
855
856 case TYPE::TLINE_Z0:
857 case TYPE::TLINE_RLGC:
858 return std::make_unique<SIM_MODEL_TLINE>( aType );
859
860 case TYPE::SW_V:
861 case TYPE::SW_I:
862 return std::make_unique<SIM_MODEL_SWITCH>( aType );
863
864 case TYPE::V:
865 case TYPE::I:
866 case TYPE::V_SIN:
867 case TYPE::I_SIN:
868 case TYPE::V_PULSE:
869 case TYPE::I_PULSE:
870 case TYPE::V_EXP:
871 case TYPE::I_EXP:
872 case TYPE::V_AM:
873 case TYPE::I_AM:
874 case TYPE::V_SFFM:
875 case TYPE::I_SFFM:
876 case TYPE::V_VCL:
877 case TYPE::V_CCL:
878 case TYPE::V_PWL:
879 case TYPE::I_VCL:
880 case TYPE::I_CCL:
881 case TYPE::I_PWL:
882 case TYPE::V_WHITENOISE:
883 case TYPE::I_WHITENOISE:
884 case TYPE::V_PINKNOISE:
885 case TYPE::I_PINKNOISE:
886 case TYPE::V_BURSTNOISE:
887 case TYPE::I_BURSTNOISE:
888 case TYPE::V_RANDUNIFORM:
889 case TYPE::I_RANDUNIFORM:
890 case TYPE::V_RANDGAUSSIAN:
891 case TYPE::I_RANDGAUSSIAN:
892 case TYPE::V_RANDEXP:
893 case TYPE::I_RANDEXP:
894 case TYPE::V_RANDPOISSON:
895 case TYPE::I_RANDPOISSON:
896 return std::make_unique<SIM_MODEL_SOURCE>( aType );
897
898 case TYPE::SUBCKT:
899 return std::make_unique<SIM_MODEL_SUBCKT>();
900
901 case TYPE::XSPICE:
902 return std::make_unique<SIM_MODEL_XSPICE>( aType );
903
904 case TYPE::KIBIS_DEVICE:
905 case TYPE::KIBIS_DRIVER_DC:
906 case TYPE::KIBIS_DRIVER_RECT:
907 case TYPE::KIBIS_DRIVER_PRBS:
908 return std::make_unique<SIM_MODEL_IBIS>( aType );
909
910 case TYPE::RAWSPICE:
911 return std::make_unique<SIM_MODEL_RAW_SPICE>();
912
913 default:
914 return std::make_unique<SIM_MODEL_NGSPICE>( aType );
915 }
916}
917
918
920 SIM_MODEL( aType, std::make_unique<SPICE_GENERATOR>( *this ),
921 std::make_unique<SIM_MODEL_SERIALIZER>( *this ) )
922{
923}
924
925
926SIM_MODEL::SIM_MODEL( TYPE aType, std::unique_ptr<SPICE_GENERATOR> aSpiceGenerator ) :
927 SIM_MODEL( aType, std::move( aSpiceGenerator ),
928 std::make_unique<SIM_MODEL_SERIALIZER>( *this ) )
929{
930}
931
932
933SIM_MODEL::SIM_MODEL( TYPE aType, std::unique_ptr<SPICE_GENERATOR> aSpiceGenerator,
934 std::unique_ptr<SIM_MODEL_SERIALIZER> aSerializer ) :
935 m_baseModel( nullptr ),
936 m_serializer( std::move( aSerializer ) ),
937 m_spiceGenerator( std::move( aSpiceGenerator ) ),
938 m_type( aType ),
939 m_isEnabled( true ),
940 m_isStoredInValue( false )
941{
942}
943
944
945void SIM_MODEL::createPins( const std::vector<SCH_PIN*>& aSymbolPins )
946{
947 // Default pin sequence: model pins are the same as symbol pins.
948 // Excess model pins are set as Not Connected.
949 // Note that intentionally nothing is added if `GetPinNames()` returns an empty vector.
950
951 // SIM_MODEL pins must be ordered by symbol pin numbers -- this is assumed by the code that
952 // accesses them.
953
954 std::vector<std::string> pinNames = GetPinNames();
955
956 for( unsigned modelPinIndex = 0; modelPinIndex < pinNames.size(); ++modelPinIndex )
957 {
958 wxString pinName = pinNames[ modelPinIndex ];
959 bool optional = false;
960
961 if( pinName.StartsWith( '<' ) && pinName.EndsWith( '>' ) )
962 {
963 pinName = pinName.Mid( 1, pinName.Length() - 2 );
964 optional = true;
965 }
966
967 if( modelPinIndex < aSymbolPins.size() )
968 {
969 AddPin( { pinNames.at( modelPinIndex ),
970 aSymbolPins[ modelPinIndex ]->GetNumber().ToStdString() } );
971 }
972 else if( !optional )
973 {
974 AddPin( { pinNames.at( modelPinIndex ), "" } );
975 }
976 }
977}
978
979
981{
982 // SUBCKTs are a single level; there's never a baseModel.
983 if( m_type == TYPE::SUBCKT )
984 return false;
985
986 // Model must be written if there's no base model or the base model is an internal model
987 if( !m_baseModel || aItem.baseModelName == "" )
988 return true;
989
990 for( int ii = 0; ii < GetParamCount(); ++ii )
991 {
992 const PARAM& param = m_params[ii];
993
994 // Instance parameters are written in item lines
995 if( param.info.isSpiceInstanceParam )
996 continue;
997
998 // Empty parameters are interpreted as default-value
999 if ( param.value == "" )
1000 continue;
1001
1002 if( const SIM_MODEL* baseModel = dynamic_cast<const SIM_MODEL*>( m_baseModel ) )
1003 {
1004 const std::string& baseValue = baseModel->m_params[ii].value;
1005
1006 if( param.value == baseValue )
1007 continue;
1008
1009 // One more check for equivalence, mostly for early 7.0 files which wrote all
1010 // parameters to the Sim.Params field in normalized format
1011 if( param.value == SIM_VALUE::Normalize( SIM_VALUE::ToDouble( baseValue ) ) )
1012 continue;
1013
1014 // Overrides must be written
1015 return true;
1016 }
1017 }
1018
1019 return false;
1020}
1021
1022
1023template <class T>
1024bool SIM_MODEL::InferSimModel( T& aSymbol, std::vector<SCH_FIELD>* aFields, bool aResolve, int aDepth,
1025 SIM_VALUE_GRAMMAR::NOTATION aNotation, wxString* aDeviceType,
1026 wxString* aModelType, wxString* aModelParams, wxString* aPinMap )
1027{
1028 // SPICE notation is case-insensitive and locale-insensitve. This means it uses "Meg" for
1029 // mega (as both 'M' and 'm' must mean milli), and "." (always) for a decimal separator.
1030 //
1031 // KiCad's GUI uses the SI-standard 'M' for mega and 'm' for milli, and a locale-dependent
1032 // decimal separator.
1033 //
1034 // KiCad's Sim.* fields are in-between, using SI notation but a fixed decimal separator.
1035 //
1036 // So where does that leave inferred value fields? Behavioural models must be passed in
1037 // straight, because we don't (at present) know how to parse them.
1038 //
1039 // However, behavioural models _look_ like SPICE code, so it's not a stretch to expect them
1040 // to _be_ SPICE code. A passive capacitor model on the other hand, just looks like a
1041 // capacitance. Some users might expect 3,3u to work, while others might expect 3,300uF to
1042 // work.
1043 //
1044 // Checking the locale isn't reliable because it assumes the current computer's locale is
1045 // the same as the locale the schematic was authored in -- something that isn't true, for
1046 // instance, when sharing designs over DIYAudio.com.
1047 //
1048 // However, even the E192 series of preferred values uses only 3 significant digits, so a ','
1049 // or '.' followed by 3 digits _could_ reasonably-reliably be interpreted as a thousands
1050 // separator.
1051 //
1052 // Or we could just say inferred values are locale-independent, with "." used as a decimal
1053 // separator and "," used as a thousands separator. 3,300uF works, but 3,3 does not.
1054
1055 auto convertNotation =
1056 [&]( const wxString& units ) -> wxString
1057 {
1060 if( units == wxS( "µ" ) || units == wxS( "μ" ) )
1061 return wxS( "u" );
1062
1063 if( aNotation == SIM_VALUE_GRAMMAR::NOTATION::SPICE )
1064 {
1065 if( units == wxT( "M" ) )
1066 return wxT( "Meg" );
1067 }
1068 else if( aNotation == SIM_VALUE_GRAMMAR::NOTATION::SI )
1069 {
1070 if( units.Capitalize() == wxT( "Meg" ) )
1071 return wxT( "M" );
1072 }
1073
1074 return units;
1075 };
1076
1077 auto convertSeparators =
1078 []( wxString* mantissa )
1079 {
1080 mantissa->Replace( wxS( " " ), wxEmptyString );
1081
1082 wxChar ambiguousSeparator = '?';
1083 wxChar thousandsSeparator = '?';
1084 bool thousandsSeparatorFound = false;
1085 wxChar decimalSeparator = '?';
1086 bool decimalSeparatorFound = false;
1087 int digits = 0;
1088
1089 for( int ii = (int) mantissa->length() - 1; ii >= 0; --ii )
1090 {
1091 wxChar c = mantissa->GetChar( ii );
1092
1093 if( c >= '0' && c <= '9' )
1094 {
1095 digits += 1;
1096 }
1097 else if( c == '.' || c == ',' )
1098 {
1099 if( decimalSeparator != '?' || thousandsSeparator != '?' )
1100 {
1101 // We've previously found a non-ambiguous separator...
1102
1103 if( c == decimalSeparator )
1104 {
1105 if( thousandsSeparatorFound )
1106 return false; // decimal before thousands
1107 else if( decimalSeparatorFound )
1108 return false; // more than one decimal
1109 else
1110 decimalSeparatorFound = true;
1111 }
1112 else if( c == thousandsSeparator )
1113 {
1114 if( digits != 3 )
1115 return false; // thousands not followed by 3 digits
1116 else
1117 thousandsSeparatorFound = true;
1118 }
1119 }
1120 else if( ambiguousSeparator != '?' )
1121 {
1122 // We've previously found a separator, but we don't know for sure
1123 // which...
1124
1125 if( c == ambiguousSeparator )
1126 {
1127 // They both must be thousands separators
1128 thousandsSeparator = ambiguousSeparator;
1129 thousandsSeparatorFound = true;
1130 decimalSeparator = c == '.' ? ',' : '.';
1131 }
1132 else
1133 {
1134 // The first must have been a decimal, and this must be a
1135 // thousands.
1136 decimalSeparator = ambiguousSeparator;
1137 decimalSeparatorFound = true;
1138 thousandsSeparator = c;
1139 thousandsSeparatorFound = true;
1140 }
1141 }
1142 else
1143 {
1144 // This is the first separator...
1145
1146 // If it's preceeded by a '0' (only), or if it's followed by some
1147 // number of digits not equal to 3, then it -must- be a decimal
1148 // separator.
1149 //
1150 // In all other cases we don't really know what it is yet.
1151
1152 if( ( ii == 1 && mantissa->GetChar( 0 ) == '0' ) || digits != 3 )
1153 {
1154 decimalSeparator = c;
1155 decimalSeparatorFound = true;
1156 thousandsSeparator = c == '.' ? ',' : '.';
1157 }
1158 else
1159 {
1160 ambiguousSeparator = c;
1161 }
1162 }
1163
1164 digits = 0;
1165 }
1166 else
1167 {
1168 digits = 0;
1169 }
1170 }
1171
1172 // If we found nothing difinitive then we have to assume SPICE-native syntax
1173 if( decimalSeparator == '?' && thousandsSeparator == '?' )
1174 {
1175 decimalSeparator = '.';
1176 thousandsSeparator = ',';
1177 }
1178
1179 mantissa->Replace( thousandsSeparator, wxEmptyString );
1180 mantissa->Replace( decimalSeparator, '.' );
1181
1182 return true;
1183 };
1184
1185 wxString prefix = aSymbol.GetPrefix();
1186 wxString library = GetFieldValue( aFields, SIM_LIBRARY_FIELD, aResolve, aDepth );
1187 wxString modelName = GetFieldValue( aFields, SIM_NAME_FIELD, aResolve, aDepth );
1188 wxString value = GetFieldValue( aFields, SIM_VALUE_FIELD, aResolve, aDepth );
1189 std::vector<SCH_PIN*> pins;
1190
1191 if constexpr (std::is_same_v<T, SCH_SYMBOL>)
1192 pins = static_cast<SCH_SYMBOL*>( &aSymbol )->GetPins( nullptr );
1193 else if constexpr (std::is_same_v<T, LIB_SYMBOL>)
1194 pins = static_cast<LIB_SYMBOL*>( &aSymbol )->GetGraphicalPins( 0, 0 );
1195
1196
1197 // ensure the pins are sorted by number (not guaranteed in the symbol)
1198 // because the inferred spice model pin assignment here and elsewhere depends on
1199 // us maintaing the list of pins in order
1200 std::sort( pins.begin(), pins.end(),
1201 []( const SCH_PIN* a, const SCH_PIN* b )
1202 {
1203 return a->GetNumber() < b->GetNumber();
1204 } );
1205
1206 *aDeviceType = GetFieldValue( aFields, SIM_DEVICE_FIELD, aResolve, aDepth );
1207 *aModelType = GetFieldValue( aFields, SIM_DEVICE_SUBTYPE_FIELD, aResolve, aDepth );
1208 *aModelParams = GetFieldValue( aFields, SIM_PARAMS_FIELD, aResolve, aDepth );
1209 *aPinMap = GetFieldValue( aFields, SIM_PINS_FIELD, aResolve, aDepth );
1210
1211 if( pins.size() != 2 )
1212 return false;
1213
1214 if( ( ( *aDeviceType == "R" || *aDeviceType == "L" || *aDeviceType == "C" )
1215 && aModelType->IsEmpty() )
1216 ||
1217 ( library.IsEmpty() && modelName.IsEmpty()
1218 && aDeviceType->IsEmpty()
1219 && aModelType->IsEmpty()
1220 && !value.IsEmpty()
1221 && ( prefix.StartsWith( "R" ) || prefix.StartsWith( "L" ) || prefix.StartsWith( "C" ) ) ) )
1222 {
1223 if( aModelParams->IsEmpty() )
1224 {
1225 wxRegEx idealVal( wxT( "^"
1226 "([0-9\\,\\. ]+)"
1227 "([fFpPnNuUmMkKgGtTμµ𝛍𝜇𝝁 ]|M(e|E)(g|G))?"
1228 "([fFhHΩΩ𝛀𝛺𝝮rR]|ohm)?"
1229 "([-1-9 ]*)"
1230 "([fFhHΩΩ𝛀𝛺𝝮rR]|ohm)?"
1231 "$" ) );
1232
1233 if( idealVal.Matches( value ) ) // Ideal
1234 {
1235 wxString valueMantissa( idealVal.GetMatch( value, 1 ) );
1236 wxString valueExponent( idealVal.GetMatch( value, 2 ) );
1237 wxString valueFraction( idealVal.GetMatch( value, 6 ) );
1238
1239 if( !convertSeparators( &valueMantissa ) )
1240 return false;
1241
1242 if( valueMantissa.Contains( wxT( "." ) ) || valueFraction.IsEmpty() )
1243 {
1244 aModelParams->Printf( wxT( "%s=\"%s%s\"" ),
1245 prefix.Left(1).Lower(),
1246 std::move( valueMantissa ),
1247 convertNotation( valueExponent ) );
1248 }
1249 else
1250 {
1251 aModelParams->Printf( wxT( "%s=\"%s.%s%s\"" ),
1252 prefix.Left(1).Lower(),
1253 std::move( valueMantissa ),
1254 std::move( valueFraction ),
1255 convertNotation( valueExponent ) );
1256 }
1257 }
1258 else // Behavioral
1259 {
1260 *aModelType = wxT( "=" );
1261 aModelParams->Printf( wxT( "%s=\"%s\"" ), prefix.Left(1).Lower(), std::move( value ) );
1262 }
1263 }
1264
1265 if( aDeviceType->IsEmpty() )
1266 *aDeviceType = prefix.Left( 1 );
1267
1268 if( aPinMap->IsEmpty() )
1269 aPinMap->Printf( wxT( "%s=+ %s=-" ), pins[0]->GetNumber(), pins[1]->GetNumber() );
1270
1271 return true;
1272 }
1273
1274 if( ( ( *aDeviceType == wxT( "V" ) || *aDeviceType == wxT( "I" ) )
1275 && ( aModelType->IsEmpty() || *aModelType == wxT( "DC" ) ) )
1276 ||
1277 ( aDeviceType->IsEmpty()
1278 && aModelType->IsEmpty()
1279 && !value.IsEmpty()
1280 && ( prefix.StartsWith( "V" ) || prefix.StartsWith( "I" ) ) ) )
1281 {
1282 if( !value.IsEmpty() )
1283 {
1284 wxString param = "dc";
1285
1286 if( value.StartsWith( wxT( "DC " ) ) )
1287 {
1288 value = value.Right( value.Length() - 3 );
1289 }
1290 else if( value.StartsWith( wxT( "AC " ) ) )
1291 {
1292 value = value.Right( value.Length() - 3 );
1293 param = "ac";
1294 }
1295
1296 wxRegEx sourceVal( wxT( "^"
1297 "([0-9\\,\\. ]+)"
1298 "([fFpPnNuUmMkKgGtTμµ𝛍𝜇𝝁 ]|M(e|E)(g|G))?"
1299 "([vVaA])?"
1300 "([-1-9 ]*)"
1301 "([vVaA])?"
1302 "$" ) );
1303
1304 if( sourceVal.Matches( value ) )
1305 {
1306 wxString valueMantissa( sourceVal.GetMatch( value, 1 ) );
1307 wxString valueExponent( sourceVal.GetMatch( value, 2 ) );
1308 wxString valueFraction( sourceVal.GetMatch( value, 6 ) );
1309
1310 if( !convertSeparators( &valueMantissa ) )
1311 return false;
1312
1313 if( valueMantissa.Contains( wxT( "." ) ) || valueFraction.IsEmpty() )
1314 {
1315 aModelParams->Printf( wxT( "%s=\"%s%s\" %s" ),
1316 std::move( param ),
1317 std::move( valueMantissa ),
1318 convertNotation( valueExponent ),
1319 *aModelParams );
1320 }
1321 else
1322 {
1323 aModelParams->Printf( wxT( "%s=\"%s.%s%s\" %s" ),
1324 std::move( param ),
1325 std::move( valueMantissa ),
1326 std::move( valueFraction ),
1327 convertNotation( valueExponent ),
1328 *aModelParams );
1329 }
1330 }
1331 else
1332 {
1333 aModelParams->Printf( wxT( "%s=\"%s\" %s" ),
1334 std::move( param ),
1335 std::move( value ),
1336 *aModelParams );
1337 }
1338 }
1339
1340 if( aDeviceType->IsEmpty() )
1341 *aDeviceType = prefix.Left( 1 );
1342
1343 if( aModelType->IsEmpty() )
1344 *aModelType = wxT( "DC" );
1345
1346 if( aPinMap->IsEmpty() )
1347 aPinMap->Printf( wxT( "%s=+ %s=-" ), pins[0]->GetNumber(), pins[1]->GetNumber() );
1348
1349 return true;
1350 }
1351
1352 return false;
1353}
1354
1355
1356template bool SIM_MODEL::InferSimModel<SCH_SYMBOL>( SCH_SYMBOL& aSymbol, std::vector<SCH_FIELD>* aFields,
1357 bool aResolve, int aDepth,
1359 wxString* aDeviceType, wxString* aModelType,
1360 wxString* aModelParams, wxString* aPinMap );
1361template bool SIM_MODEL::InferSimModel<LIB_SYMBOL>( LIB_SYMBOL& aSymbol, std::vector<SCH_FIELD>* aFields,
1362 bool aResolve, int aDepth,
1364 wxString* aDeviceType, wxString* aModelType,
1365 wxString* aModelParams, wxString* aPinMap );
1366
1367
1368template <typename T>
1369void SIM_MODEL::MigrateSimModel( T& aSymbol, const PROJECT* aProject )
1370{
1371 class FIELD_INFO
1372 {
1373 public:
1374 FIELD_INFO()
1375 {
1376 m_Visible = false;
1377 m_Attributes.m_Size = VECTOR2I( DEFAULT_SIZE_TEXT * schIUScale.IU_PER_MILS,
1378 DEFAULT_SIZE_TEXT * schIUScale.IU_PER_MILS );
1379 };
1380
1381 FIELD_INFO( const wxString& aText, SCH_FIELD* aField ) :
1382 m_Text( aText ),
1383 m_Visible( aField->IsVisible() ),
1384 m_Attributes( aField->GetAttributes() ),
1385 m_Pos( aField->GetPosition() )
1386 {}
1387
1388 bool IsEmpty() const { return m_Text.IsEmpty(); }
1389
1390 SCH_FIELD CreateField( T* aSymbol, const wxString& aFieldName )
1391 {
1392 SCH_FIELD field( aSymbol, FIELD_T::USER, aFieldName );
1393
1394 field.SetText( m_Text );
1395 field.SetVisible( m_Visible );
1396 field.SetAttributes( m_Attributes );
1397 field.SetPosition( m_Pos );
1398
1399 return field;
1400 }
1401
1402 public:
1403 wxString m_Text;
1404 bool m_Visible;
1405 TEXT_ATTRIBUTES m_Attributes;
1406 VECTOR2I m_Pos;
1407 };
1408
1409 SCH_FIELD* existing_deviceField = aSymbol.GetField( SIM_DEVICE_FIELD );
1410 SCH_FIELD* existing_deviceSubtypeField = aSymbol.GetField( SIM_DEVICE_SUBTYPE_FIELD );
1411 SCH_FIELD* existing_pinsField = aSymbol.GetField( SIM_PINS_FIELD );
1412 SCH_FIELD* existing_paramsField = aSymbol.GetField( SIM_PARAMS_FIELD );
1413
1414 wxString existing_deviceSubtype;
1415
1416 if( existing_deviceSubtypeField )
1417 existing_deviceSubtype = existing_deviceSubtypeField->GetShownText( false ).Upper();
1418
1419 if( existing_deviceField
1420 || existing_deviceSubtypeField
1421 || existing_pinsField
1422 || existing_paramsField )
1423 {
1424 // Has a current (V7+) model field.
1425
1426 // Up until 7.0RC2 we used '+' and '-' for potentiometer pins, which doesn't match
1427 // SPICE. Here we remap them to 'r0' and 'r1'.
1428 if( existing_deviceSubtype == wxS( "POT" ) )
1429 {
1430 if( existing_pinsField )
1431 {
1432 wxString pinMap = existing_pinsField->GetText();
1433 pinMap.Replace( wxS( "=+" ), wxS( "=r1" ) );
1434 pinMap.Replace( wxS( "=-" ), wxS( "=r0" ) );
1435 existing_pinsField->SetText( pinMap );
1436 }
1437 }
1438
1439 // Up until 8.0RC1 random voltage/current sources were a bit of a mess.
1440 if( existing_deviceSubtype.StartsWith( wxS( "RAND" ) ) )
1441 {
1442 // Re-fetch value without resolving references. If it's an indirect value then we
1443 // can't migrate it.
1444 existing_deviceSubtype = existing_deviceSubtypeField->GetText().Upper();
1445
1446 if( existing_deviceSubtype.Replace( wxS( "NORMAL" ), wxS( "GAUSSIAN" ) ) )
1447 existing_deviceSubtypeField->SetText( existing_deviceSubtype );
1448
1449 if( existing_paramsField )
1450 {
1451 wxString params = existing_paramsField->GetText().Lower();
1452 size_t count = 0;
1453
1454 // We used to support 'min' and 'max' instead of 'range' and 'offset', but we
1455 // wrote all 4 to the netlist which would cause ngspice to barf, so no one has
1456 // working documents with min and max specified. Just delete them if they're
1457 // uninitialized.
1458 count += params.Replace( wxS( "min=0 " ), wxEmptyString );
1459 count += params.Replace( wxS( "max=0 " ), wxEmptyString );
1460
1461 // We used to use 'dt', but the correct ngspice name is 'ts'.
1462 count += params.Replace( wxS( "dt=" ), wxS( "ts=" ) );
1463
1464 if( count )
1465 existing_paramsField->SetText( params );
1466 }
1467 }
1468
1469 // Up until 8.0.1 we treated a mutual inductance statement as a type of inductor --
1470 // which is confusing because it doesn't represent a device at all.
1471 if( existing_deviceSubtype == wxS( "MUTUAL" ) )
1472 {
1473 if( existing_deviceSubtypeField ) // Can't be null, but Coverity doesn't know that
1474 aSymbol.RemoveField( existing_deviceSubtypeField );
1475
1476 if( existing_deviceField )
1477 {
1478 existing_deviceField->SetText( wxS( "K" ) );
1479 }
1480 else
1481 {
1482 FIELD_INFO deviceFieldInfo;
1483 deviceFieldInfo.m_Text = wxS( "K" );
1484
1485 SCH_FIELD deviceField = deviceFieldInfo.CreateField( &aSymbol, SIM_DEVICE_FIELD );
1486 aSymbol.AddField( deviceField );
1487 }
1488 }
1489
1490 return;
1491 }
1492
1493 auto getSIValue =
1494 []( SCH_FIELD* aField )
1495 {
1496 if( !aField ) // no, not really, but it keeps Coverity happy
1497 return wxString( wxEmptyString );
1498
1499 wxRegEx regex( wxT( "([^a-z])(M)(e|E)(g|G)($|[^a-z])" ) );
1500 wxString value = aField->GetText();
1501
1502 // Keep prefix, M, and suffix, but drop e|E and g|G
1503 regex.ReplaceAll( &value, wxT( "\\1\\2\\5" ) );
1504
1505 return value;
1506 };
1507
1508 auto generateDefaultPinMapFromSymbol =
1509 []( const std::vector<SCH_PIN*>& sourcePins )
1510 {
1511 wxString pinMap;
1512
1513 // If we're creating the pinMap from the symbol it means we don't know what the
1514 // SIM_MODEL's pin names are, so just use indexes.
1515
1516 for( unsigned ii = 0; ii < sourcePins.size(); ++ii )
1517 {
1518 if( ii > 0 )
1519 pinMap.Append( wxS( " " ) );
1520
1521 pinMap.Append( wxString::Format( wxT( "%s=%u" ),
1522 sourcePins[ii]->GetNumber(),
1523 ii + 1 ) );
1524 }
1525
1526 return pinMap;
1527 };
1528
1529 wxString prefix = aSymbol.GetPrefix();
1530 SCH_FIELD* valueField = aSymbol.GetField( FIELD_T::VALUE );
1531 bool sourcePinsSorted = false;
1532 std::vector<SCH_PIN*> sourcePins;
1533
1534 if constexpr (std::is_same_v<T, SCH_SYMBOL>)
1535 sourcePins = static_cast<SCH_SYMBOL*>( &aSymbol )->GetPins( nullptr );
1536 else if constexpr (std::is_same_v<T, LIB_SYMBOL>)
1537 sourcePins = static_cast<LIB_SYMBOL*>( &aSymbol )->GetGraphicalPins( 0, 0 );
1538
1539 auto lazySortSourcePins =
1540 [&sourcePins, &sourcePinsSorted]()
1541 {
1542 if( !sourcePinsSorted )
1543 {
1544 std::sort( sourcePins.begin(), sourcePins.end(),
1545 []( const SCH_PIN* lhs, const SCH_PIN* rhs )
1546 {
1547 return StrNumCmp( lhs->GetNumber(), rhs->GetNumber(), true ) < 0;
1548 } );
1549 }
1550
1551 sourcePinsSorted = true;
1552 };
1553
1554 FIELD_INFO deviceInfo;
1555 FIELD_INFO modelInfo;
1556 FIELD_INFO deviceSubtypeInfo;
1557 FIELD_INFO libInfo;
1558 FIELD_INFO spiceParamsInfo;
1559 FIELD_INFO pinMapInfo;
1560 bool modelFromValueField = false;
1561
1562 if( aSymbol.GetField( SIM_LEGACY_PRIMITIVE_FIELD )
1563 || aSymbol.GetField( SIM_LEGACY_PINS_FIELD )
1564 || aSymbol.GetField( SIM_LEGACY_MODEL_FIELD )
1565 || aSymbol.GetField( SIM_LEGACY_ENABLE_FIELD )
1566 || aSymbol.GetField( SIM_LEGACY_LIBRARY_FIELD ) )
1567 {
1568 if( SCH_FIELD* primitiveField = aSymbol.GetField( SIM_LEGACY_PRIMITIVE_FIELD ) )
1569 {
1570 deviceInfo = FIELD_INFO( primitiveField->GetText(), primitiveField );
1571 aSymbol.RemoveField( primitiveField );
1572 }
1573
1574 if( SCH_FIELD* nodeSequenceField = aSymbol.GetField( SIM_LEGACY_PINS_FIELD ) )
1575 {
1576 const wxString delimiters( "{:,; }" );
1577 const wxString& nodeSequence = nodeSequenceField->GetText();
1578 wxString pinMap;
1579
1580 if( nodeSequence != "" )
1581 {
1582 wxStringTokenizer tkz( nodeSequence, delimiters );
1583
1584 for( long modelPinNumber = 1; tkz.HasMoreTokens(); ++modelPinNumber )
1585 {
1586 long symbolPinNumber = 1;
1587 tkz.GetNextToken().ToLong( &symbolPinNumber );
1588
1589 if( modelPinNumber != 1 )
1590 pinMap.Append( " " );
1591
1592 pinMap.Append( wxString::Format( "%ld=%ld", symbolPinNumber, modelPinNumber ) );
1593 }
1594 }
1595
1596 pinMapInfo = FIELD_INFO( pinMap, nodeSequenceField );
1597 aSymbol.RemoveField( nodeSequenceField );
1598 }
1599
1600 if( SCH_FIELD* modelField = aSymbol.GetField( SIM_LEGACY_MODEL_FIELD ) )
1601 {
1602 modelInfo = FIELD_INFO( getSIValue( modelField ), modelField );
1603 aSymbol.RemoveField( modelField );
1604 }
1605 else if( valueField )
1606 {
1607 modelInfo = FIELD_INFO( getSIValue( valueField ), valueField );
1608 modelFromValueField = true;
1609 }
1610
1611 if( SCH_FIELD* libFileField = aSymbol.GetField( SIM_LEGACY_LIBRARY_FIELD ) )
1612 {
1613 libInfo = FIELD_INFO( libFileField->GetText(), libFileField );
1614 aSymbol.RemoveField( libFileField );
1615 }
1616 }
1617 else
1618 {
1619 // Auto convert some legacy fields used in the middle of 7.0 development...
1620
1621 if( SCH_FIELD* legacyType = aSymbol.GetField( wxT( "Sim_Type" ) ) )
1622 {
1623 legacyType->SetName( SIM_DEVICE_SUBTYPE_FIELD );
1624 }
1625
1626 if( SCH_FIELD* legacyDevice = aSymbol.GetField( wxT( "Sim_Device" ) ) )
1627 {
1628 legacyDevice->SetName( SIM_DEVICE_FIELD );
1629 }
1630
1631 if( SCH_FIELD* legacyPins = aSymbol.GetField( wxT( "Sim_Pins" ) ) )
1632 {
1633 bool isPassive = prefix.StartsWith( wxT( "R" ) )
1634 || prefix.StartsWith( wxT( "L" ) )
1635 || prefix.StartsWith( wxT( "C" ) );
1636
1637 // Migrate pins from array of indexes to name-value-pairs
1638 wxString pinMap;
1639 wxArrayString pinIndexes;
1640
1641 wxStringSplit( legacyPins->GetText(), pinIndexes, ' ' );
1642
1643 lazySortSourcePins();
1644
1645 if( isPassive && pinIndexes.size() == 2 && sourcePins.size() == 2 )
1646 {
1647 if( pinIndexes[0] == wxT( "2" ) )
1648 {
1649 pinMap.Printf( wxT( "%s=- %s=+" ),
1650 sourcePins[0]->GetNumber(),
1651 sourcePins[1]->GetNumber() );
1652 }
1653 else
1654 {
1655 pinMap.Printf( wxT( "%s=+ %s=-" ),
1656 sourcePins[0]->GetNumber(),
1657 sourcePins[1]->GetNumber() );
1658 }
1659 }
1660 else
1661 {
1662 for( unsigned ii = 0; ii < pinIndexes.size() && ii < sourcePins.size(); ++ii )
1663 {
1664 if( ii > 0 )
1665 pinMap.Append( wxS( " " ) );
1666
1667 pinMap.Append( wxString::Format( wxT( "%s=%s" ),
1668 sourcePins[ii]->GetNumber(),
1669 pinIndexes[ ii ] ) );
1670 }
1671 }
1672
1673 legacyPins->SetName( SIM_PINS_FIELD );
1674 legacyPins->SetText( pinMap );
1675 }
1676
1677 if( SCH_FIELD* legacyParams = aSymbol.GetField( wxT( "Sim_Params" ) ) )
1678 {
1679 legacyParams->SetName( SIM_PARAMS_FIELD );
1680 }
1681
1682 return;
1683 }
1684
1685 wxString device = deviceInfo.m_Text.Trim( true ).Trim( false );
1686 wxString lib = libInfo.m_Text.Trim( true ).Trim( false );
1687 wxString model = modelInfo.m_Text.Trim( true ).Trim( false );
1688 wxString modelLineParams;
1689
1690 bool libraryModel = false;
1691 bool inferredModel = false;
1692 bool internalModel = false;
1693
1694 if( !lib.IsEmpty() )
1695 {
1696 WX_STRING_REPORTER reporter;
1697 SIM_LIB_MGR libMgr( aProject );
1698 std::vector<SCH_FIELD> emptyFields;
1699 std::vector<EMBEDDED_FILES*> embeddedFilesStack;
1700
1701 if constexpr (std::is_same_v<T, SCH_SYMBOL>)
1702 {
1703 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( &aSymbol );
1704 embeddedFilesStack.push_back( symbol->Schematic()->GetEmbeddedFiles() );
1705 }
1706
1707 if( EMBEDDED_FILES* symbolEmbeddedFiles = aSymbol.GetEmbeddedFiles() )
1708 {
1709 embeddedFilesStack.push_back( symbolEmbeddedFiles );
1710
1711 if constexpr (std::is_same_v<T, SCH_SYMBOL>)
1712 {
1713 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( &aSymbol );
1714 symbol->GetLibSymbolRef()->AppendParentEmbeddedFiles( embeddedFilesStack );
1715 }
1716 else if constexpr (std::is_same_v<T, LIB_SYMBOL>)
1717 {
1718 LIB_SYMBOL* symbol = static_cast<LIB_SYMBOL*>( &aSymbol );
1719 symbol->AppendParentEmbeddedFiles( embeddedFilesStack );
1720 }
1721 }
1722
1723 libMgr.SetFilesStack( std::move( embeddedFilesStack ) );
1724
1725 // Pull out any following parameters from model name
1726 model = model.BeforeFirst( ' ', &modelLineParams );
1727 modelInfo.m_Text = model;
1728
1729 lazySortSourcePins();
1730
1731 SIM_LIBRARY::MODEL simModel = libMgr.CreateModel( lib, model.ToStdString(),
1732 emptyFields, false, 0,
1733 sourcePins, reporter );
1734
1735 if( reporter.HasMessage() )
1736 libraryModel = false; // Fall back to raw spice model
1737 else
1738 libraryModel = true;
1739
1740 if( pinMapInfo.IsEmpty() )
1741 {
1742 // Try to generate a default pin map from the SIM_MODEL's pins; if that fails,
1743 // generate one from the symbol's pins
1744 pinMapInfo.m_Text = wxString( simModel.model.Serializer().GeneratePins() );
1745
1746 if( pinMapInfo.IsEmpty() )
1747 pinMapInfo.m_Text = generateDefaultPinMapFromSymbol( sourcePins );
1748 }
1749 }
1750 else if( ( device == wxS( "R" )
1751 || device == wxS( "L" )
1752 || device == wxS( "C" )
1753 || device == wxS( "V" )
1754 || device == wxS( "I" ) )
1755 && prefix.StartsWith( device )
1756 && modelFromValueField )
1757 {
1758 inferredModel = true;
1759 }
1760 else if( device == wxS( "V" ) || device == wxS( "I" ) )
1761 {
1762 // See if we have a SPICE time-dependent function such as "sin(0 1 60)" or "sin 0 1 60"
1763 // that can be handled by a built-in SIM_MODEL_SOURCE.
1764
1765 wxStringTokenizer tokenizer( model, wxT( "() " ), wxTOKEN_STRTOK );
1766
1767 if( tokenizer.HasMoreTokens() )
1768 {
1769 deviceSubtypeInfo.m_Text = tokenizer.GetNextToken();
1770 deviceSubtypeInfo.m_Text.MakeUpper();
1771
1772 for( SIM_MODEL::TYPE type : SIM_MODEL::TYPE_ITERATOR() )
1773 {
1774 if( device == SIM_MODEL::SpiceInfo( type ).itemType
1775 && deviceSubtypeInfo.m_Text == SIM_MODEL::SpiceInfo( type ).functionName )
1776 {
1777 try
1778 {
1779 std::unique_ptr<SIM_MODEL> simModel = SIM_MODEL::Create( type );
1780
1781 if( deviceSubtypeInfo.m_Text == wxT( "DC" ) && tokenizer.CountTokens() == 1 )
1782 {
1783 wxCHECK( valueField, /* void */ );
1784 valueField->SetText( tokenizer.GetNextToken() );
1785 modelFromValueField = false;
1786 }
1787 else
1788 {
1789 for( int ii = 0; tokenizer.HasMoreTokens(); ++ii )
1790 {
1791 simModel->SetParamValue( ii, tokenizer.GetNextToken().ToStdString(),
1793 }
1794
1795 deviceSubtypeInfo.m_Text = SIM_MODEL::TypeInfo( type ).fieldValue;
1796
1797 spiceParamsInfo = modelInfo;
1798 spiceParamsInfo.m_Text = wxString( simModel->Serializer().GenerateParams() );
1799 }
1800
1801 internalModel = true;
1802
1803 if( pinMapInfo.IsEmpty() )
1804 {
1805 lazySortSourcePins();
1806
1807 // Generate a default pin map from the SIM_MODEL's pins
1808 simModel->createPins( sourcePins );
1809 pinMapInfo.m_Text = wxString( simModel->Serializer().GeneratePins() );
1810 }
1811 }
1812 catch( ... )
1813 {
1814 // Fall back to raw spice model
1815 }
1816
1817 break;
1818 }
1819 }
1820 }
1821 }
1822
1823 if( libraryModel )
1824 {
1825 SCH_FIELD libField = libInfo.CreateField( &aSymbol, SIM_LIBRARY_FIELD );
1826 aSymbol.AddField( libField );
1827
1828 SCH_FIELD nameField = modelInfo.CreateField( &aSymbol, SIM_NAME_FIELD );
1829 aSymbol.AddField( nameField );
1830
1831 if( !modelLineParams.IsEmpty() )
1832 {
1833 spiceParamsInfo = modelInfo;
1834 spiceParamsInfo.m_Pos.x += nameField.GetBoundingBox().GetWidth();
1835 spiceParamsInfo.m_Text = modelLineParams;
1836
1837 BOX2I nameBBox = nameField.GetBoundingBox();
1838 int nameWidth = nameBBox.GetWidth();
1839
1840 // Add space between model name and additional parameters
1841 nameWidth += KiROUND( nameBBox.GetHeight() * 1.25 );
1842
1843 if( nameField.GetHorizJustify() == GR_TEXT_H_ALIGN_RIGHT )
1844 spiceParamsInfo.m_Pos.x -= nameWidth;
1845 else
1846 spiceParamsInfo.m_Pos.x += nameWidth;
1847
1848 SCH_FIELD paramsField = spiceParamsInfo.CreateField( &aSymbol, SIM_PARAMS_FIELD );
1849 aSymbol.AddField( paramsField );
1850 }
1851
1852 if( modelFromValueField )
1853 valueField->SetText( wxT( "${SIM.NAME}" ) );
1854 }
1855 else if( inferredModel )
1856 {
1857 // DeviceType is left in the reference designator and Model is left in the value field,
1858 // so there's nothing to do here....
1859 }
1860 else if( internalModel )
1861 {
1862 SCH_FIELD deviceField = deviceInfo.CreateField( &aSymbol, SIM_DEVICE_FIELD );
1863 aSymbol.AddField( deviceField );
1864
1865 if( !deviceSubtypeInfo.m_Text.IsEmpty() )
1866 {
1867 SCH_FIELD subtypeField = deviceSubtypeInfo.CreateField( &aSymbol, SIM_DEVICE_SUBTYPE_FIELD );
1868 aSymbol.AddField( subtypeField );
1869 }
1870
1871 if( !spiceParamsInfo.IsEmpty() )
1872 {
1873 SCH_FIELD paramsField = spiceParamsInfo.CreateField( &aSymbol, SIM_PARAMS_FIELD );
1874 aSymbol.AddField( paramsField );
1875 }
1876
1877 if( modelFromValueField )
1878 valueField->SetText( wxT( "${SIM.PARAMS}" ) );
1879 }
1880 else // Insert a raw spice model as a substitute.
1881 {
1882 if( device.IsEmpty() && lib.IsEmpty() )
1883 {
1884 spiceParamsInfo = modelInfo;
1885 }
1886 else
1887 {
1888 spiceParamsInfo.m_Text.Printf( wxT( "type=\"%s\" model=\"%s\" lib=\"%s\"" ), device,
1889 model, lib );
1890 }
1891
1892 deviceInfo.m_Text = SIM_MODEL::DeviceInfo( SIM_MODEL::DEVICE_T::SPICE ).fieldValue;
1893
1894 SCH_FIELD deviceField = deviceInfo.CreateField( &aSymbol, SIM_DEVICE_FIELD );
1895 aSymbol.AddField( deviceField );
1896
1897 SCH_FIELD paramsField = spiceParamsInfo.CreateField( &aSymbol, SIM_PARAMS_FIELD );
1898 aSymbol.AddField( paramsField );
1899
1900 if( modelFromValueField )
1901 {
1902 // Get the current Value field, after previous changes.
1903 valueField = aSymbol.GetField( FIELD_T::VALUE );
1904
1905 if( valueField )
1906 valueField->SetText( wxT( "${SIM.PARAMS}" ) );
1907 }
1908
1909 // We know nothing about the SPICE model here, so we've got no choice but to generate
1910 // the default pin map from the symbol's pins.
1911
1912 if( pinMapInfo.IsEmpty() )
1913 {
1914 lazySortSourcePins();
1915 pinMapInfo.m_Text = generateDefaultPinMapFromSymbol( sourcePins );
1916 }
1917 }
1918
1919 if( !pinMapInfo.IsEmpty() )
1920 {
1921 SCH_FIELD pinsField = pinMapInfo.CreateField( &aSymbol, SIM_PINS_FIELD );
1922 aSymbol.AddField( pinsField );
1923 }
1924}
1925
1926
1928 const PROJECT* aProject );
1930 const PROJECT* aProject );
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:114
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
constexpr size_type GetWidth() const
Definition box2.h:214
constexpr size_type GetHeight() const
Definition box2.h:215
virtual bool IsVisible() const
Definition eda_text.h:187
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
Definition eda_text.cpp:447
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition eda_text.h:200
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:400
const TEXT_ATTRIBUTES & GetAttributes() const
Definition eda_text.h:231
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
virtual const wxString Problem() const
what was the problem?
Define a library symbol object.
Definition lib_symbol.h:83
std::vector< const SCH_PIN * > GetGraphicalPins(int aUnit=0, int aBodyStyle=0) const
Graphical pins: Return schematic pin objects as drawn (unexpanded), filtered by unit/body.
void AppendParentEmbeddedFiles(std::vector< EMBEDDED_FILES * > &aStack) const
A singleton reporter that reports to nowhere.
Definition reporter.h:216
Container for project specific data.
Definition project.h:65
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
virtual bool HasMessage() const
Returns true if any messages were reported.
Definition reporter.h:134
EMBEDDED_FILES * GetEmbeddedFiles() override
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
VECTOR2I GetPosition() const override
virtual const wxString & GetText() const override
Return the string associated with the text object.
Definition sch_field.h:116
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0, const wxString &aVariantName=wxEmptyString) const
void SetPosition(const VECTOR2I &aPosition) override
void SetText(const wxString &aText) override
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition sch_item.cpp:254
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Schematic symbol object.
Definition sch_symbol.h:76
std::vector< const SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:184
static constexpr auto DIFF_FIELD
SIM_MODEL & CreateModel(SIM_MODEL::TYPE aType, const std::vector< SCH_PIN * > &aPins, REPORTER &aReporter)
void SetFilesStack(std::vector< EMBEDDED_FILES * > aFilesStack)
Definition sim_lib_mgr.h:48
Serializes/deserializes a SIM_MODEL for storage in LIB_FIELDs/SCH_FIELDs.
std::string GeneratePins() const
int FindModelPinIndex(const std::string &aSymbolPinNumber)
static void MigrateSimModel(T &aSymbol, const PROJECT *aProject)
const PARAM & GetBaseParam(unsigned aParamIndex) const
void AddParam(const PARAM::INFO &aInfo)
bool IsStoredInValue() const
Definition sim_model.h:506
virtual std::vector< std::string > GetPinNames() const
Definition sim_model.h:466
void WriteFields(std::vector< SCH_FIELD > &aFields, const SCH_SHEET_PATH *aSheetPath=nullptr, const wxString &aVariantName=wxEmptyString) const
std::unique_ptr< SPICE_GENERATOR > m_spiceGenerator
Definition sim_model.h:541
virtual bool requiresSpiceModelLine(const SPICE_ITEM &aItem) const
void ClearPins()
std::vector< SIM_MODEL_PIN > m_modelPins
Definition sim_model.h:536
static INFO TypeInfo(TYPE aType)
int GetPinCount() const
Definition sim_model.h:468
void AddPin(const SIM_MODEL_PIN &aPin)
static SPICE_INFO SpiceInfo(TYPE aType)
const SIM_MODEL_SERIALIZER & Serializer() const
Definition sim_model.h:432
bool m_isEnabled
Definition sim_model.h:544
void ReadDataFields(const std::vector< SCH_FIELD > *aFields, bool aResolve, int aDepth, const std::vector< SCH_PIN * > &aPins)
static bool InferSimModel(T &aSymbol, std::vector< SCH_FIELD > *aFields, bool aResolve, int aDepth, SIM_VALUE_GRAMMAR::NOTATION aNotation, wxString *aDeviceType, wxString *aModelType, wxString *aModelParams, wxString *aPinMap)
virtual const PARAM & GetParam(unsigned aParamIndex) const
bool m_isStoredInValue
Definition sim_model.h:545
void createPins(const std::vector< SCH_PIN * > &aSymbolPins)
virtual void SetBaseModel(const SIM_MODEL &aBaseModel)
SIM_MODEL()=delete
static TYPE ReadTypeFromFields(const std::vector< SCH_FIELD > &aFields, bool aResolve, int aDepth, REPORTER &aReporter)
int GetParamCount() const
Definition sim_model.h:478
void AssignSymbolPinNumberToModelPin(int aPinIndex, const wxString &aSymbolPinNumber)
static DEVICE_INFO DeviceInfo(DEVICE_T aDeviceType)
Definition sim_model.cpp:61
const PARAM * FindParam(const std::string &aParamName) const
virtual void doSetParamValue(int aParamIndex, const std::string &aValue)
std::vector< PARAM > m_params
Definition sim_model.h:535
const PARAM & GetParamOverride(unsigned aParamIndex) const
friend class SPICE_GENERATOR
Definition sim_model.h:80
virtual ~SIM_MODEL()
static std::unique_ptr< SIM_MODEL > Create(TYPE aType, const std::vector< SCH_PIN * > &aPins, REPORTER &aReporter)
void SetParamValue(int aParamIndex, const std::string &aValue, SIM_VALUE::NOTATION aNotation=SIM_VALUE::NOTATION::SI)
virtual void SwitchSingleEndedDiff(bool aDiff)
Definition sim_model.h:508
const SIM_MODEL_PIN & GetPin(unsigned aIndex) const
Definition sim_model.h:469
std::unique_ptr< SIM_MODEL_SERIALIZER > m_serializer
Definition sim_model.h:538
virtual int doFindParam(const std::string &aParamName) const
TYPE GetType() const
Definition sim_model.h:461
std::vector< std::reference_wrapper< const SIM_MODEL_PIN > > GetPins() const
const TYPE m_type
Definition sim_model.h:543
const SIM_MODEL * m_baseModel
Definition sim_model.h:537
static std::string Normalize(double aValue)
static std::string ConvertNotation(const std::string &aString, NOTATION aFromNotation, NOTATION aToNotation)
static double ToDouble(const std::string &aString, double aDefault=NAN)
SIM_VALUE_GRAMMAR::NOTATION NOTATION
Definition sim_value.h:61
A wrapper for reporting to a wxString object.
Definition reporter.h:191
#define _(s)
#define DEFAULT_SIZE_TEXT
This is the "default-of-the-default" hardcoded text size; individual application define their own def...
Definition eda_text.h:70
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
STL namespace.
@ RPT_SEVERITY_ERROR
void SetFieldValue(std::vector< SCH_FIELD > &aFields, const wxString &aFieldName, const std::string &aValue, bool aIsVisible=true, const SCH_SHEET_PATH *aSheetPath=nullptr, const wxString &aVariantName=wxEmptyString)
Definition sch_field.h:428
wxString GetFieldValue(const std::vector< SCH_FIELD > *aFields, FIELD_T aFieldType)
Definition sch_field.h:403
SIM_MODEL::TYPE TYPE
Definition sim_model.cpp:58
#define SIM_PINS_FIELD
Definition sim_model.h:54
#define SIM_DEVICE_FIELD
Definition sim_model.h:52
#define SIM_NAME_FIELD
Definition sim_model.h:57
#define SIM_LIBRARY_FIELD
Definition sim_model.h:56
#define SIM_LEGACY_ENABLE_FIELD
Definition sim_model.h:64
#define SIM_LEGACY_ENABLE_FIELD_V7
Definition sim_model.h:60
#define SIM_LEGACY_MODEL_FIELD
Definition sim_model.h:62
#define SIM_LEGACY_PINS_FIELD
Definition sim_model.h:63
#define SIM_LEGACY_LIBRARY_FIELD
Definition sim_model.h:65
#define SIM_PARAMS_FIELD
Definition sim_model.h:55
#define SIM_VALUE_FIELD
Definition sim_model.h:50
#define SIM_LEGACY_PRIMITIVE_FIELD
Definition sim_model.h:61
#define SIM_DEVICE_SUBTYPE_FIELD
Definition sim_model.h:53
bool convertSeparators(wxString *value)
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
SIM_MODEL & model
Definition sim_library.h:41
std::vector< std::string > enumValues
Definition sim_model.h:388
bool Matches(const std::string &aName) const
std::string defaultValue
Definition sim_model.h:382
bool Matches(const std::string &aName) const
Definition sim_model.h:395
std::string value
Definition sim_model.h:400
const INFO & info
Definition sim_model.h:401
static constexpr auto NOT_CONNECTED
Definition sim_model.h:73
std::string baseModelName
@ USER
The field ID hasn't been set yet; field is invalid.
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ VALUE
Field Value of part, i.e. "3.3K".
KIBIS_MODEL * model
KIBIS_PIN * pin
@ GR_TEXT_H_ALIGN_RIGHT
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695