KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sim_lib_mgr.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 The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <filename_resolver.h>
22#include <pgm_base.h>
23#include <string>
24#include <string_utils.h>
25#include <common.h>
26#include <functional>
27#include <sch_symbol.h>
28#include <schematic.h>
29
30// Include simulator headers after wxWidgets headers to avoid conflicts with Windows headers
31// (especially on msys2 + wxWidgets 3.0.x)
32#include <sim/sim_lib_mgr.h>
33#include <sim/sim_library.h>
34#include <sim/sim_model.h>
35#include <sim/sim_model_ideal.h>
36
37using namespace std::placeholders;
38
39
41 m_project( aPrj ),
42 m_forceFullParse( false )
43{
44}
45
46
48{
49 m_libraries.clear();
50 m_models.clear();
51}
52
53
54wxString SIM_LIB_MGR::ResolveLibraryPath( const wxString& aLibraryPath, REPORTER& aReporter )
55{
57
58 resolver.SetProject( m_project );
59
60 std::vector<const EMBEDDED_FILES*> embeddedFilesStack;
61
62 for( const EMBEDDED_FILES* embeddedFiles : m_embeddedFilesStack )
63 embeddedFilesStack.push_back( embeddedFiles );
64
65 wxString resolvedPath = resolver.ResolvePath( aLibraryPath, wxEmptyString,
66 std::move( embeddedFilesStack ) );
67
68 // If FILENAME_RESOLVER found the file, use that result
69 if( !resolvedPath.IsEmpty() )
70 {
71 wxFileName fn( resolvedPath );
72
73 if( fn.IsAbsolute() )
74 return fn.GetFullPath();
75 }
76
77 // Fall back to the original behavior for paths not found by FILENAME_RESOLVER.
78 // First expand any environment variables in the path.
79 wxString expandedPath = ExpandEnvVarSubstitutions( aLibraryPath, m_project );
80
81 // Convert to UNIX format
82 expandedPath.Replace( '\\', '/' );
83
84 wxFileName fn( expandedPath );
85
86 if( fn.IsAbsolute() && fn.Exists() )
87 return fn.GetFullPath();
88
89 wxFileName projectFn( m_project ? m_project->AbsolutePath( expandedPath ) : expandedPath );
90
91 if( projectFn.Exists() )
92 return projectFn.GetFullPath();
93
94 // Check relative to SPICE_LIB_DIR environment variable
95 wxFileName spiceLibFn( expandedPath );
96 wxString spiceLibDir;
97
98 wxGetEnv( wxT( "SPICE_LIB_DIR" ), &spiceLibDir );
99
100 if( !spiceLibDir.IsEmpty() && spiceLibFn.MakeAbsolute( spiceLibDir ) && spiceLibFn.Exists() )
101 return spiceLibFn.GetFullPath();
102
103 if( spiceLibDir.IsEmpty() || spiceLibFn.GetFullPath() == projectFn.GetFullPath() )
104 {
105 aReporter.Report( wxString::Format( _( "Simulation model library not found at '%s'" ),
106 projectFn.GetFullPath() ) );
107 }
108 else
109 {
110 aReporter.Report( wxString::Format( _( "Simulation model library not found at '%s' or '%s'" ),
111 projectFn.GetFullPath(),
112 spiceLibFn.GetFullPath() ) );
113 }
114
115 return aLibraryPath;
116}
117
118
119wxString SIM_LIB_MGR::ResolveEmbeddedLibraryPath( const wxString& aLibPath,
120 const wxString& aRelativeLib,
121 REPORTER& aReporter )
122{
123 wxFileName testPath( aLibPath );
124 wxString fullPath( aLibPath );
125
126 if( !testPath.IsAbsolute() && !aRelativeLib.empty() )
127 {
128 wxString relLib( aRelativeLib );
129
130 relLib = ResolveLibraryPath( relLib, aReporter );
131
132 wxFileName fn( relLib );
133
134 testPath.MakeAbsolute( fn.GetPath( true ) );
135 fullPath = testPath.GetFullPath();
136 }
137
138 wxFileName fn( fullPath );
139
140 if( !fn.Exists() )
141 fullPath = aLibPath;
142
143 fullPath = ResolveLibraryPath( fullPath, aReporter );
144
145 return fullPath;
146}
147
148
149void SIM_LIB_MGR::SetLibrary( const wxString& aLibraryPath, REPORTER& aReporter )
150{
151 wxString path = ResolveLibraryPath( aLibraryPath, aReporter );
152
154 return;
155
156 if( !wxFileName::Exists( path ) )
157 {
158 aReporter.Report( wxString::Format( _( "Simulation model library not found at '%s'" ),
159 path ) );
160 return;
161 }
162
163 std::unique_ptr<SIM_LIBRARY> library = SIM_LIBRARY::Create( path, m_forceFullParse, aReporter,
164 [&]( const wxString& libPath, const wxString& relativeLib ) -> wxString
165 {
166 return ResolveEmbeddedLibraryPath( libPath, relativeLib, aReporter );
167 } );
168
169 Clear();
170 m_libraries[path] = std::move( library );
171}
172
173
174SIM_MODEL& SIM_LIB_MGR::CreateModel( SIM_MODEL::TYPE aType, const std::vector<SCH_PIN*>& aPins,
175 REPORTER& aReporter )
176{
177 m_models.push_back( SIM_MODEL::Create( aType, aPins, aReporter ) );
178 return *m_models.back();
179}
180
181
183 const std::vector<SCH_PIN*>& aPins, REPORTER& aReporter )
184{
185 m_models.push_back( SIM_MODEL::Create( aBaseModel, aPins, aReporter ) );
186 return *m_models.back();
187}
188
189
190SIM_MODEL& SIM_LIB_MGR::CreateModel( const SIM_MODEL* aBaseModel, const std::vector<SCH_PIN*>& aPins,
191 const std::vector<SCH_FIELD>& aFields, bool aResolve, int aDepth,
192 REPORTER& aReporter )
193{
194 m_models.push_back( SIM_MODEL::Create( aBaseModel, aPins, aFields, aResolve, aDepth, aReporter ) );
195 return *m_models.back();
196}
197
199 bool aResolve, int aDepth, const wxString& aVariantName,
200 REPORTER& aReporter, const wxString& aMergedSimPins )
201{
202 // Note: currently this creates a resolved model (all Kicad variables references are resolved
203 // before building the model).
204 //
205 // That's not what we want if this is ever called from the Simulation Model Editor (or other
206 // editors, but it is what we want if called to generate a netlist or other exported items.
207
208
209 std::vector<SCH_FIELD> fields;
210
211 for( const SCH_FIELD& field : aSymbol.GetFields() )
212 {
213 if( field.GetId() == FIELD_T::REFERENCE )
214 {
215 fields.emplace_back( &aSymbol, FIELD_T::USER, field.GetName() );
216 fields.back().SetText( aSymbol.GetRef( aSheetPath ) );
217 }
218 else if( field.GetId() == FIELD_T::VALUE || field.GetName().StartsWith( wxS( "Sim." ) ) )
219 {
220 fields.emplace_back( &aSymbol, FIELD_T::USER, field.GetName() );
221
222 // For multi-unit symbols, use merged Sim.Pins from all units if provided
223 if( !aMergedSimPins.IsEmpty() && field.GetName() == SIM_PINS_FIELD )
224 fields.back().SetText( aMergedSimPins );
225 else
226 fields.back().SetText( field.GetShownText( aSheetPath, false, aDepth, aVariantName ) );
227 }
228 }
229
230 auto getOrCreateField =
231 [&aSymbol, &fields]( const wxString& name ) -> SCH_FIELD*
232 {
233 for( SCH_FIELD& field : fields )
234 {
235 if( field.GetName().IsSameAs( name ) )
236 return &field;
237 }
238
239 fields.emplace_back( &aSymbol, FIELD_T::USER, name );
240 return &fields.back();
241 };
242
243 wxString deviceType;
244 wxString modelType;
245 wxString modelParams;
246 wxString pinMap;
247 bool storeInValue = false;
248
249 // Infer RLC and VI models if they aren't specified
250 if( SIM_MODEL::InferSimModel( aSymbol, &fields, aResolve, aDepth, SIM_VALUE_GRAMMAR::NOTATION::SI,
251 &deviceType, &modelType, &modelParams, &pinMap ) )
252 {
253 getOrCreateField( SIM_DEVICE_FIELD )->SetText( deviceType );
254
255 if( !modelType.IsEmpty() )
256 getOrCreateField( SIM_DEVICE_SUBTYPE_FIELD )->SetText( modelType );
257
258 getOrCreateField( SIM_PARAMS_FIELD )->SetText( modelParams );
259 getOrCreateField( SIM_PINS_FIELD )->SetText( pinMap );
260
261 storeInValue = true;
262 }
263
264 std::vector<SCH_PIN*> sourcePins = aSymbol.GetAllLibPins();
265
266 std::sort( sourcePins.begin(), sourcePins.end(),
267 []( const SCH_PIN* lhs, const SCH_PIN* rhs )
268 {
269 return StrNumCmp( lhs->GetNumber(), rhs->GetNumber(), true ) < 0;
270 } );
271
272 SIM_LIBRARY::MODEL model = CreateModel( fields, aResolve, aDepth, sourcePins, aReporter );
273
274 model.model.SetIsStoredInValue( storeInValue );
275
276 return model;
277}
278
279
280SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( const std::vector<SCH_FIELD>& aFields,
281 bool aResolve, int aDepth,
282 const std::vector<SCH_PIN*>& aPins,
283 REPORTER& aReporter )
284{
285 std::string libraryPath = GetFieldValue( &aFields, SIM_LIBRARY::LIBRARY_FIELD, aResolve, aDepth );
286 std::string baseModelName = GetFieldValue( &aFields, SIM_LIBRARY::NAME_FIELD, aResolve, aDepth );
287
288 if( libraryPath != "" )
289 {
290 return CreateModel( libraryPath, baseModelName, aFields, aResolve, aDepth, aPins, aReporter );
291 }
292 else
293 {
294 m_models.push_back( SIM_MODEL::Create( aFields, aResolve, aDepth, aPins, aReporter ) );
295 return { baseModelName, *m_models.back() };
296 }
297}
298
299
300SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( const wxString& aLibraryPath,
301 const std::string& aBaseModelName,
302 const std::vector<SCH_FIELD>& aFields,
303 bool aResolve, int aDepth,
304 const std::vector<SCH_PIN*>& aPins,
305 REPORTER& aReporter )
306{
307 wxString msg;
308 SIM_LIBRARY* library = nullptr;
309 SIM_MODEL* baseModel = nullptr;
310 std::string modelName;
311 wxString path = ResolveLibraryPath( aLibraryPath, aReporter );
312
313 auto it = m_libraries.find( path );
314
315 if( it == m_libraries.end() )
316 {
317 it = m_libraries.emplace( path, SIM_LIBRARY::Create( path, m_forceFullParse, aReporter,
318 [&]( const wxString& libPath, const wxString& relativeLib ) -> wxString
319 {
320 return ResolveEmbeddedLibraryPath( libPath, relativeLib, aReporter );
321 } ) ).first;
322 }
323
324 library = &*it->second;
325
326 if( aBaseModelName == "" )
327 {
328 msg.Printf( _( "Error loading simulation model: no '%s' field" ),
330
331 aReporter.Report( msg, RPT_SEVERITY_ERROR );
332
333 modelName = _( "unknown" ).ToStdString();
334 }
335 else if( library )
336 {
337 baseModel = library->FindModel( aBaseModelName );
338 modelName = aBaseModelName;
339
340 if( !baseModel )
341 {
342 msg.Printf( _( "Error loading simulation model: could not find base model '%s' "
343 "in library '%s'" ),
344 aBaseModelName,
345 path );
346
347 aReporter.Report( msg, RPT_SEVERITY_ERROR );
348 }
349 }
350
351 m_models.push_back( SIM_MODEL::Create( baseModel, aPins, aFields, aResolve, aDepth, aReporter ) );
352
353 return { modelName, *m_models.back() };
354}
355
356
357void SIM_LIB_MGR::SetModel( int aIndex, std::unique_ptr<SIM_MODEL> aModel )
358{
359 m_models.at( aIndex ) = std::move( aModel );
360}
361
362
363std::map<wxString, std::reference_wrapper<const SIM_LIBRARY>> SIM_LIB_MGR::GetLibraries() const
364{
365 std::map<wxString, std::reference_wrapper<const SIM_LIBRARY>> libraries;
366
367 for( auto& [path, library] : m_libraries )
368 libraries.try_emplace( path, *library );
369
370 return libraries;
371}
372
373
374std::vector<std::reference_wrapper<SIM_MODEL>> SIM_LIB_MGR::GetModels() const
375{
376 std::vector<std::reference_wrapper<SIM_MODEL>> models;
377
378 for( const std::unique_ptr<SIM_MODEL>& model : m_models )
379 models.emplace_back( *model );
380
381 return models;
382}
const char * name
Provide an extensible class to resolve 3D model paths.
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)
Report a string with a given severity.
Definition reporter.h:100
virtual bool HasMessageOfSeverity(int aSeverityMask) const
Returns true if the reporter has one or more messages matching the specified severity mask.
Definition reporter.h:141
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:69
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
std::vector< SCH_PIN * > GetAllLibPins() const
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
static std::unique_ptr< SIM_LIBRARY > Create(const wxString &aFilePath, bool aForceFullParse, REPORTER &aReporter, const std::function< wxString(const wxString &, const wxString &)> &aResolver)
Read library from a source file (e.g.
static constexpr auto LIBRARY_FIELD
Definition sim_library.h:31
static constexpr auto NAME_FIELD
Definition sim_library.h:32
void SetModel(int aIndex, std::unique_ptr< SIM_MODEL > aModel)
wxString ResolveLibraryPath(const wxString &aLibraryPath, REPORTER &aReporter)
const PROJECT * m_project
Definition sim_lib_mgr.h:86
std::vector< EMBEDDED_FILES * > m_embeddedFilesStack
Definition sim_lib_mgr.h:85
bool m_forceFullParse
Definition sim_lib_mgr.h:87
SIM_MODEL & CreateModel(SIM_MODEL::TYPE aType, const std::vector< SCH_PIN * > &aPins, REPORTER &aReporter)
SIM_LIB_MGR(const PROJECT *aPrj)
std::vector< std::unique_ptr< SIM_MODEL > > m_models
Definition sim_lib_mgr.h:89
std::map< wxString, std::reference_wrapper< const SIM_LIBRARY > > GetLibraries() const
wxString ResolveEmbeddedLibraryPath(const wxString &aLibPath, const wxString &aRelativeLib, REPORTER &aReporter)
std::vector< std::reference_wrapper< SIM_MODEL > > GetModels() const
void SetLibrary(const wxString &aLibraryPath, REPORTER &aReporter)
std::map< wxString, std::unique_ptr< SIM_LIBRARY > > m_libraries
Definition sim_lib_mgr.h:88
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)
static std::unique_ptr< SIM_MODEL > Create(TYPE aType, const std::vector< SCH_PIN * > &aPins, REPORTER &aReporter)
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition common.cpp:704
The common library.
#define _(s)
static FILENAME_RESOLVER * resolver
see class PGM_BASE
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_UNDEFINED
wxString GetFieldValue(const std::vector< SCH_FIELD > *aFields, FIELD_T aFieldType)
Definition sch_field.h:421
#define SIM_PINS_FIELD
Definition sim_model.h:50
#define SIM_DEVICE_FIELD
Definition sim_model.h:48
#define SIM_PARAMS_FIELD
Definition sim_model.h:52
#define SIM_DEVICE_SUBTYPE_FIELD
Definition sim_model.h:49
@ 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".
std::string path
KIBIS_MODEL * model