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