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
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{
187 // Note: currently this creates a resolved model (all Kicad variables references are resolved
188 // before building the model).
189 //
190 // That's not what we want if this is ever called from the Simulation Model Editor (or other
191 // editors, but it is what we want if called to generate a netlist or other exported items.
192
193
194 std::vector<SCH_FIELD> fields;
195
196 for( const SCH_FIELD& field : aSymbol.GetFields() )
197 {
198 if( field.GetId() == FIELD_T::REFERENCE )
199 {
200 fields.emplace_back( &aSymbol, FIELD_T::USER, field.GetName() );
201 fields.back().SetText( aSymbol.GetRef( aSheetPath ) );
202 }
203 else if( field.GetId() == FIELD_T::VALUE || field.GetName().StartsWith( wxS( "Sim." ) ) )
204 {
205 fields.emplace_back( &aSymbol, FIELD_T::USER, field.GetName() );
206 fields.back().SetText( field.GetShownText( aSheetPath, false, aDepth ) );
207 }
208 }
209
210 auto getOrCreateField =
211 [&aSymbol, &fields]( const wxString& name ) -> SCH_FIELD*
212 {
213 for( SCH_FIELD& field : fields )
214 {
215 if( field.GetName().IsSameAs( name ) )
216 return &field;
217 }
218
219 fields.emplace_back( &aSymbol, FIELD_T::USER, name );
220 return &fields.back();
221 };
222
223 wxString deviceType;
224 wxString modelType;
225 wxString modelParams;
226 wxString pinMap;
227 bool storeInValue = false;
228
229 // Infer RLC and VI models if they aren't specified
230 if( SIM_MODEL::InferSimModel( aSymbol, &fields, aResolve, aDepth, SIM_VALUE_GRAMMAR::NOTATION::SI,
231 &deviceType, &modelType, &modelParams, &pinMap ) )
232 {
233 getOrCreateField( SIM_DEVICE_FIELD )->SetText( deviceType );
234
235 if( !modelType.IsEmpty() )
236 getOrCreateField( SIM_DEVICE_SUBTYPE_FIELD )->SetText( modelType );
237
238 getOrCreateField( SIM_PARAMS_FIELD )->SetText( modelParams );
239 getOrCreateField( SIM_PINS_FIELD )->SetText( pinMap );
240
241 storeInValue = true;
242 }
243
244 std::vector<SCH_PIN*> sourcePins = aSymbol.GetAllLibPins();
245
246 std::sort( sourcePins.begin(), sourcePins.end(),
247 []( const SCH_PIN* lhs, const SCH_PIN* rhs )
248 {
249 return StrNumCmp( lhs->GetNumber(), rhs->GetNumber(), true ) < 0;
250 } );
251
252 SIM_LIBRARY::MODEL model = CreateModel( fields, aResolve, aDepth, sourcePins, aReporter );
253
254 model.model.SetIsStoredInValue( storeInValue );
255
256 return model;
257}
258
259
260SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( const std::vector<SCH_FIELD>& aFields,
261 bool aResolve, int aDepth,
262 const std::vector<SCH_PIN*>& aPins,
263 REPORTER& aReporter )
264{
265 std::string libraryPath = GetFieldValue( &aFields, SIM_LIBRARY::LIBRARY_FIELD, aResolve, aDepth );
266 std::string baseModelName = GetFieldValue( &aFields, SIM_LIBRARY::NAME_FIELD, aResolve, aDepth );
267
268 if( libraryPath != "" )
269 {
270 return CreateModel( libraryPath, baseModelName, aFields, aResolve, aDepth, aPins, aReporter );
271 }
272 else
273 {
274 m_models.push_back( SIM_MODEL::Create( aFields, aResolve, aDepth, aPins, aReporter ) );
275 return { baseModelName, *m_models.back() };
276 }
277}
278
279
280SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( const wxString& aLibraryPath,
281 const std::string& aBaseModelName,
282 const std::vector<SCH_FIELD>& aFields,
283 bool aResolve, int aDepth,
284 const std::vector<SCH_PIN*>& aPins,
285 REPORTER& aReporter )
286{
287 wxString msg;
288 SIM_LIBRARY* library = nullptr;
289 SIM_MODEL* baseModel = nullptr;
290 std::string modelName;
291 wxString path = ResolveLibraryPath( aLibraryPath, aReporter );
292
293 auto it = m_libraries.find( path );
294
295 if( it == m_libraries.end() )
296 {
297 it = m_libraries.emplace( path, SIM_LIBRARY::Create( path, m_forceFullParse, aReporter,
298 [&]( const wxString& libPath, const wxString& relativeLib ) -> wxString
299 {
300 return ResolveEmbeddedLibraryPath( libPath, relativeLib, aReporter );
301 } ) ).first;
302 }
303
304 library = &*it->second;
305
306 if( aBaseModelName == "" )
307 {
308 msg.Printf( _( "Error loading simulation model: no '%s' field" ),
310
311 aReporter.Report( msg, RPT_SEVERITY_ERROR );
312
313 modelName = _( "unknown" ).ToStdString();
314 }
315 else if( library )
316 {
317 baseModel = library->FindModel( aBaseModelName );
318 modelName = aBaseModelName;
319
320 if( !baseModel )
321 {
322 msg.Printf( _( "Error loading simulation model: could not find base model '%s' "
323 "in library '%s'" ),
324 aBaseModelName,
325 path );
326
327 aReporter.Report( msg, RPT_SEVERITY_ERROR );
328 }
329 }
330
331 m_models.push_back( SIM_MODEL::Create( baseModel, aPins, aFields, aResolve, aDepth, aReporter ) );
332
333 return { modelName, *m_models.back() };
334}
335
336
337void SIM_LIB_MGR::SetModel( int aIndex, std::unique_ptr<SIM_MODEL> aModel )
338{
339 m_models.at( aIndex ) = std::move( aModel );
340}
341
342
343std::map<wxString, std::reference_wrapper<const SIM_LIBRARY>> SIM_LIB_MGR::GetLibraries() const
344{
345 std::map<wxString, std::reference_wrapper<const SIM_LIBRARY>> libraries;
346
347 for( auto& [path, library] : m_libraries )
348 libraries.try_emplace( path, *library );
349
350 return libraries;
351}
352
353
354std::vector<std::reference_wrapper<SIM_MODEL>> SIM_LIB_MGR::GetModels() const
355{
356 std::vector<std::reference_wrapper<SIM_MODEL>> models;
357
358 for( const std::unique_ptr<SIM_MODEL>& model : m_models )
359 models.emplace_back( *model );
360
361 return models;
362}
const char * name
Definition: DXF_plotter.cpp:62
Provide an extensible class to resolve 3D model paths.
wxString ResolvePath(const wxString &aFileName, const wxString &aWorkingPath, std::vector< const EMBEDDED_FILES * > aEmbeddedFilesStack)
Determine the full path of the given file name.
bool SetProject(const PROJECT *aProject, bool *flgChanged=nullptr)
Set the current KiCad project directory as the first entry in the model path list.
Container for project specific data.
Definition: project.h:65
virtual const wxString AbsolutePath(const wxString &aFileName) const
Fix up aFileName if it is relative to the project's directory to be an absolute path and filename.
Definition: project.cpp:372
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:140
void SetText(const wxString &aText) override
Definition: sch_field.cpp:1092
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:75
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
Definition: sch_symbol.cpp:780
std::vector< SCH_PIN * > GetAllLibPins() const
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
Definition: sch_symbol.cpp:550
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.
Definition: sim_library.cpp:34
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)
Definition: sim_lib_mgr.cpp:58
const PROJECT * m_project
Definition: sim_lib_mgr.h:88
std::vector< EMBEDDED_FILES * > m_embeddedFilesStack
Definition: sim_lib_mgr.h:87
bool m_forceFullParse
Definition: sim_lib_mgr.h:89
SIM_MODEL & CreateModel(SIM_MODEL::TYPE aType, const std::vector< SCH_PIN * > &aPins, REPORTER &aReporter)
SIM_LIB_MGR(const PROJECT *aPrj)
Definition: sim_lib_mgr.cpp:44
std::vector< std::unique_ptr< SIM_MODEL > > m_models
Definition: sim_lib_mgr.h:91
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 Clear()
Definition: sim_lib_mgr.cpp:51
void SetLibrary(const wxString &aLibraryPath, REPORTER &aReporter)
std::map< wxString, std::unique_ptr< SIM_LIBRARY > > m_libraries
Definition: sim_lib_mgr.h:90
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)
Definition: sim_model.cpp:1020
static std::unique_ptr< SIM_MODEL > Create(TYPE aType, const std::vector< SCH_PIN * > &aPins, REPORTER &aReporter)
Definition: sim_model.cpp:482
void SetIsStoredInValue(bool aIsStoredInValue)
Definition: sim_model.h:500
The common library.
#define _(s)
static FILENAME_RESOLVER * resolver
Definition: export_idf.cpp:53
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:397
#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
SIM_MODEL & model
Definition: sim_library.h:41