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, you may find one here:
19 * https://www.gnu.org/licenses/gpl-3.0.html
20 * or you may search the http://www.gnu.org website for the version 3 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <filename_resolver.h>
26#include <pgm_base.h>
27#include <string>
28#include <string_utils.h>
29#include <common.h>
30#include <functional>
31#include <sch_symbol.h>
32#include <schematic.h>
33
34// Include simulator headers after wxWidgets headers to avoid conflicts with Windows headers
35// (especially on msys2 + wxWidgets 3.0.x)
36#include <sim/sim_lib_mgr.h>
37#include <sim/sim_library.h>
38#include <sim/sim_model.h>
39#include <sim/sim_model_ideal.h>
40
41using namespace std::placeholders;
42
43
45 m_project( aPrj ),
46 m_forceFullParse( false )
47{
48}
49
50
52{
53 m_libraries.clear();
54 m_models.clear();
55}
56
57
58wxString SIM_LIB_MGR::ResolveLibraryPath( const wxString& aLibraryPath, REPORTER& aReporter )
59{
61
62 resolver.SetProject( m_project );
63
64 std::vector<const EMBEDDED_FILES*> embeddedFilesStack;
65
66 for( const EMBEDDED_FILES* embeddedFiles : m_embeddedFilesStack )
67 embeddedFilesStack.push_back( embeddedFiles );
68
69 wxString expandedPath = resolver.ResolvePath( aLibraryPath, wxEmptyString, std::move( embeddedFilesStack ) );
70
71 wxFileName fn( expandedPath );
72
73 if( fn.IsAbsolute() )
74 return fn.GetFullPath();
75
76 wxFileName projectFn( m_project ? m_project->AbsolutePath( expandedPath ) : expandedPath );
77
78 if( projectFn.Exists() )
79 return projectFn.GetFullPath();
80
81 wxFileName spiceLibFn( expandedPath );
82 wxString spiceLibDir;
83
84 wxGetEnv( wxT( "SPICE_LIB_DIR" ), &spiceLibDir );
85
86 if( !spiceLibDir.IsEmpty() && spiceLibFn.MakeAbsolute( spiceLibDir ) && spiceLibFn.Exists() )
87 return spiceLibFn.GetFullPath();
88
89 if( spiceLibDir.IsEmpty() || spiceLibFn.GetFullPath() == projectFn.GetFullPath() )
90 {
91 aReporter.Report( wxString::Format( _( "Simulation model library not found at '%s'" ),
92 projectFn.GetFullPath() ) );
93 }
94 else
95 {
96 aReporter.Report( wxString::Format( _( "Simulation model library not found at '%s' or '%s'" ),
97 projectFn.GetFullPath(),
98 spiceLibFn.GetFullPath() ) );
99 }
100
101 return aLibraryPath;
102}
103
104
105wxString SIM_LIB_MGR::ResolveEmbeddedLibraryPath( const wxString& aLibPath,
106 const wxString& aRelativeLib,
107 REPORTER& aReporter )
108{
109 wxFileName testPath( aLibPath );
110 wxString fullPath( aLibPath );
111
112 if( !testPath.IsAbsolute() && !aRelativeLib.empty() )
113 {
114 wxString relLib( aRelativeLib );
115
116 relLib = ResolveLibraryPath( relLib, aReporter );
117
118 wxFileName fn( relLib );
119
120 testPath.MakeAbsolute( fn.GetPath( true ) );
121 fullPath = testPath.GetFullPath();
122 }
123
124 wxFileName fn( fullPath );
125
126 if( !fn.Exists() )
127 fullPath = aLibPath;
128
129 fullPath = ResolveLibraryPath( fullPath, aReporter );
130
131 return fullPath;
132}
133
134
135void SIM_LIB_MGR::SetLibrary( const wxString& aLibraryPath, REPORTER& aReporter )
136{
137 wxString path = ResolveLibraryPath( aLibraryPath, aReporter );
138
140 return;
141
142 if( !wxFileName::Exists( path ) )
143 {
144 aReporter.Report( wxString::Format( _( "Simulation model library not found at '%s'" ),
145 path ) );
146 return;
147 }
148
149 std::unique_ptr<SIM_LIBRARY> library = SIM_LIBRARY::Create( path, m_forceFullParse, aReporter,
150 [&]( const wxString& libPath, const wxString& relativeLib ) -> wxString
151 {
152 return ResolveEmbeddedLibraryPath( libPath, relativeLib, aReporter );
153 } );
154
155 Clear();
156 m_libraries[path] = std::move( library );
157}
158
159
160SIM_MODEL& SIM_LIB_MGR::CreateModel( SIM_MODEL::TYPE aType, const std::vector<SCH_PIN*>& aPins,
161 REPORTER& aReporter )
162{
163 m_models.push_back( SIM_MODEL::Create( aType, aPins, aReporter ) );
164 return *m_models.back();
165}
166
167
169 const std::vector<SCH_PIN*>& aPins, REPORTER& aReporter )
170{
171 m_models.push_back( SIM_MODEL::Create( aBaseModel, aPins, aReporter ) );
172 return *m_models.back();
173}
174
175
176SIM_MODEL& SIM_LIB_MGR::CreateModel( const SIM_MODEL* aBaseModel, const std::vector<SCH_PIN*>& aPins,
177 const std::vector<SCH_FIELD>& aFields, bool aResolve, int aDepth,
178 REPORTER& aReporter )
179{
180 m_models.push_back( SIM_MODEL::Create( aBaseModel, aPins, aFields, aResolve, aDepth, aReporter ) );
181 return *m_models.back();
182}
183
185 bool aResolve, int aDepth, REPORTER& aReporter,
186 const wxString& aMergedSimPins )
187{
188 // Note: currently this creates a resolved model (all Kicad variables references are resolved
189 // before building the model).
190 //
191 // That's not what we want if this is ever called from the Simulation Model Editor (or other
192 // editors, but it is what we want if called to generate a netlist or other exported items.
193
194
195 std::vector<SCH_FIELD> fields;
196
197 for( const SCH_FIELD& field : aSymbol.GetFields() )
198 {
199 if( field.GetId() == FIELD_T::REFERENCE )
200 {
201 fields.emplace_back( &aSymbol, FIELD_T::USER, field.GetName() );
202 fields.back().SetText( aSymbol.GetRef( aSheetPath ) );
203 }
204 else if( field.GetId() == FIELD_T::VALUE || field.GetName().StartsWith( wxS( "Sim." ) ) )
205 {
206 fields.emplace_back( &aSymbol, FIELD_T::USER, field.GetName() );
207
208 // For multi-unit symbols, use merged Sim.Pins from all units if provided
209 if( !aMergedSimPins.IsEmpty() && field.GetName() == SIM_PINS_FIELD )
210 fields.back().SetText( aMergedSimPins );
211 else
212 fields.back().SetText( field.GetShownText( aSheetPath, false, aDepth ) );
213 }
214 }
215
216 auto getOrCreateField =
217 [&aSymbol, &fields]( const wxString& name ) -> SCH_FIELD*
218 {
219 for( SCH_FIELD& field : fields )
220 {
221 if( field.GetName().IsSameAs( name ) )
222 return &field;
223 }
224
225 fields.emplace_back( &aSymbol, FIELD_T::USER, name );
226 return &fields.back();
227 };
228
229 wxString deviceType;
230 wxString modelType;
231 wxString modelParams;
232 wxString pinMap;
233 bool storeInValue = false;
234
235 // Infer RLC and VI models if they aren't specified
236 if( SIM_MODEL::InferSimModel( aSymbol, &fields, aResolve, aDepth, SIM_VALUE_GRAMMAR::NOTATION::SI,
237 &deviceType, &modelType, &modelParams, &pinMap ) )
238 {
239 getOrCreateField( SIM_DEVICE_FIELD )->SetText( deviceType );
240
241 if( !modelType.IsEmpty() )
242 getOrCreateField( SIM_DEVICE_SUBTYPE_FIELD )->SetText( modelType );
243
244 getOrCreateField( SIM_PARAMS_FIELD )->SetText( modelParams );
245 getOrCreateField( SIM_PINS_FIELD )->SetText( pinMap );
246
247 storeInValue = true;
248 }
249
250 std::vector<SCH_PIN*> sourcePins = aSymbol.GetAllLibPins();
251
252 std::sort( sourcePins.begin(), sourcePins.end(),
253 []( const SCH_PIN* lhs, const SCH_PIN* rhs )
254 {
255 return StrNumCmp( lhs->GetNumber(), rhs->GetNumber(), true ) < 0;
256 } );
257
258 SIM_LIBRARY::MODEL model = CreateModel( fields, aResolve, aDepth, sourcePins, aReporter );
259
260 model.model.SetIsStoredInValue( storeInValue );
261
262 return model;
263}
264
265
266SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( const std::vector<SCH_FIELD>& aFields,
267 bool aResolve, int aDepth,
268 const std::vector<SCH_PIN*>& aPins,
269 REPORTER& aReporter )
270{
271 std::string libraryPath = GetFieldValue( &aFields, SIM_LIBRARY::LIBRARY_FIELD, aResolve, aDepth );
272 std::string baseModelName = GetFieldValue( &aFields, SIM_LIBRARY::NAME_FIELD, aResolve, aDepth );
273
274 if( libraryPath != "" )
275 {
276 return CreateModel( libraryPath, baseModelName, aFields, aResolve, aDepth, aPins, aReporter );
277 }
278 else
279 {
280 m_models.push_back( SIM_MODEL::Create( aFields, aResolve, aDepth, aPins, aReporter ) );
281 return { baseModelName, *m_models.back() };
282 }
283}
284
285
286SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( const wxString& aLibraryPath,
287 const std::string& aBaseModelName,
288 const std::vector<SCH_FIELD>& aFields,
289 bool aResolve, int aDepth,
290 const std::vector<SCH_PIN*>& aPins,
291 REPORTER& aReporter )
292{
293 wxString msg;
294 SIM_LIBRARY* library = nullptr;
295 SIM_MODEL* baseModel = nullptr;
296 std::string modelName;
297 wxString path = ResolveLibraryPath( aLibraryPath, aReporter );
298
299 auto it = m_libraries.find( path );
300
301 if( it == m_libraries.end() )
302 {
303 it = m_libraries.emplace( path, SIM_LIBRARY::Create( path, m_forceFullParse, aReporter,
304 [&]( const wxString& libPath, const wxString& relativeLib ) -> wxString
305 {
306 return ResolveEmbeddedLibraryPath( libPath, relativeLib, aReporter );
307 } ) ).first;
308 }
309
310 library = &*it->second;
311
312 if( aBaseModelName == "" )
313 {
314 msg.Printf( _( "Error loading simulation model: no '%s' field" ),
316
317 aReporter.Report( msg, RPT_SEVERITY_ERROR );
318
319 modelName = _( "unknown" ).ToStdString();
320 }
321 else if( library )
322 {
323 baseModel = library->FindModel( aBaseModelName );
324 modelName = aBaseModelName;
325
326 if( !baseModel )
327 {
328 msg.Printf( _( "Error loading simulation model: could not find base model '%s' "
329 "in library '%s'" ),
330 aBaseModelName,
331 path );
332
333 aReporter.Report( msg, RPT_SEVERITY_ERROR );
334 }
335 }
336
337 m_models.push_back( SIM_MODEL::Create( baseModel, aPins, aFields, aResolve, aDepth, aReporter ) );
338
339 return { modelName, *m_models.back() };
340}
341
342
343void SIM_LIB_MGR::SetModel( int aIndex, std::unique_ptr<SIM_MODEL> aModel )
344{
345 m_models.at( aIndex ) = std::move( aModel );
346}
347
348
349std::map<wxString, std::reference_wrapper<const SIM_LIBRARY>> SIM_LIB_MGR::GetLibraries() const
350{
351 std::map<wxString, std::reference_wrapper<const SIM_LIBRARY>> libraries;
352
353 for( auto& [path, library] : m_libraries )
354 libraries.try_emplace( path, *library );
355
356 return libraries;
357}
358
359
360std::vector<std::reference_wrapper<SIM_MODEL>> SIM_LIB_MGR::GetModels() const
361{
362 std::vector<std::reference_wrapper<SIM_MODEL>> models;
363
364 for( const std::unique_ptr<SIM_MODEL>& model : m_models )
365 models.emplace_back( *model );
366
367 return models;
368}
const char * name
Provide an extensible class to resolve 3D model paths.
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 HasMessageOfSeverity(int aSeverityMask) const
Returns true if the reporter has one or more messages matching the specified severity mask.
Definition reporter.h:143
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
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:35
static constexpr auto NAME_FIELD
Definition sim_library.h:36
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:90
std::vector< EMBEDDED_FILES * > m_embeddedFilesStack
Definition sim_lib_mgr.h:89
bool m_forceFullParse
Definition sim_lib_mgr.h:91
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:93
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:92
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)
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:408
#define SIM_PINS_FIELD
Definition sim_model.h:54
#define SIM_DEVICE_FIELD
Definition sim_model.h:52
#define SIM_PARAMS_FIELD
Definition sim_model.h:55
#define SIM_DEVICE_SUBTYPE_FIELD
Definition sim_model.h:53
@ 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