KiCad PCB EDA Suite
Loading...
Searching...
No Matches
netlist_exporter_spice.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) 1992-2013 jp.charras at wanadoo.fr
5 * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
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, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <sim/kibis/kibis.h>
27#include <common.h>
28#include <confirm.h>
29#include <pgm_base.h>
30#include <env_paths.h>
31#include <richio.h>
34#include <sch_screen.h>
35#include <sch_textbox.h>
36#include <string_utils.h>
37#include <algorithm>
38#include <ki_exception.h>
39
41#include <fmt/core.h>
42#include <paths.h>
43#include <wx/dir.h>
44#include <wx/log.h>
45#include <wx/tokenzr.h>
46#include <locale_io.h>
47#include "markup_parser.h"
48
49
50std::string NAME_GENERATOR::Generate( const std::string& aProposedName )
51{
52 std::string name = aProposedName;
53 int ii = 1;
54
55 // insert() both tests for the collision and records the accepted name, so subsequent calls
56 // actually see previously generated names.
57 while( !m_names.insert( name ).second )
58 name = fmt::format( "{}#{}", aProposedName, ii++ );
59
60 return name;
61}
62
63
65 NETLIST_EXPORTER_BASE( aSchematic ),
66 m_libMgr( &aSchematic->Project() )
67{
68 std::vector<EMBEDDED_FILES*> embeddedFilesStack;
69 embeddedFilesStack.push_back( aSchematic->GetEmbeddedFiles() );
70 m_libMgr.SetFilesStack( std::move( embeddedFilesStack ) );
71}
72
73
74bool NETLIST_EXPORTER_SPICE::WriteNetlist( const wxString& aOutFileName, unsigned aNetlistOptions,
75 REPORTER& aReporter )
76{
77 try
78 {
79 FILE_OUTPUTFORMATTER formatter( aOutFileName, wxT( "wt" ), '\'' );
80 bool result = DoWriteNetlist( wxEmptyString, aNetlistOptions, formatter, aReporter );
81 formatter.Finish();
82
83 return result;
84 }
85 catch( const IO_ERROR& ioe )
86 {
87 aReporter.Report( ioe.What(), RPT_SEVERITY_ERROR );
88 return false;
89 }
90}
91
92
93bool NETLIST_EXPORTER_SPICE::DoWriteNetlist( const wxString& aSimCommand, unsigned aSimOptions,
94 OUTPUTFORMATTER& aFormatter, REPORTER& aReporter )
95{
97
98 // Cleanup list to avoid duplicate if the netlist exporter is run more than once.
99 m_rawIncludes.clear();
100
101 bool result = ReadSchematicAndLibraries( aSimOptions, aReporter );
102
103 WriteHead( aFormatter, aSimOptions );
104
105 writeIncludes( aFormatter, aSimOptions );
106 writeModels( aFormatter );
107
108 // Skip this if there is no netlist to avoid an ngspice segfault
109 if( !m_items.empty() )
110 WriteDirectives( aSimCommand, aSimOptions, aFormatter );
111
112 writeItems( aFormatter );
113
114 WriteTail( aFormatter, aSimOptions );
115
116 return result;
117}
118
119
120void NETLIST_EXPORTER_SPICE::WriteHead( OUTPUTFORMATTER& aFormatter, unsigned aNetlistOptions )
121{
122 aFormatter.Print( 0, ".title KiCad schematic\n" );
123}
124
125
126void NETLIST_EXPORTER_SPICE::WriteTail( OUTPUTFORMATTER& aFormatter, unsigned aNetlistOptions )
127{
128 aFormatter.Print( 0, ".end\n" );
129}
130
131
133 REPORTER& aReporter )
134{
135 std::set<std::string> refNames; // Set of reference names to check for duplication.
136 int ncCounter = 1;
137 wxString variant = m_schematic->GetCurrentVariant();
138
139 ReadDirectives( aNetlistOptions );
140
141 m_nets.clear();
142 m_items.clear();
143 m_multiunitModels.clear();
144 m_modelNameGenerator.Clear();
146 m_libParts.clear();
147
148 wxFileName cacheDir;
149 cacheDir.AssignDir( PATHS::GetUserCachePath() );
150 cacheDir.AppendDir( wxT( "ibis" ) );
151
152 if( !cacheDir.DirExists() )
153 {
154 cacheDir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
155
156 if( !cacheDir.DirExists() )
157 {
158 wxLogTrace( wxT( "IBIS_CACHE:" ),
159 wxT( "%s:%s:%d\n * failed to create ibis cache directory '%s'" ),
160 __FILE__, __FUNCTION__, __LINE__, cacheDir.GetPath() );
161
162 return false;
163 }
164 }
165
166 wxDir dir;
167 wxString dirName = cacheDir.GetFullPath();
168
169 if( !dir.Open( dirName ) )
170 return false;
171
172 wxFileName thisFile;
173 wxArrayString fileList;
174 wxString fileSpec = wxT( "*.cache" );
175
176 thisFile.SetPath( dirName ); // Set the base path to the cache folder
177
178 size_t numFilesFound = wxDir::GetAllFiles( dirName, &fileList, fileSpec );
179
180 for( size_t ii = 0; ii < numFilesFound; ii++ )
181 {
182 // Completes path to specific file so we can get its "last access" date
183 thisFile.SetFullName( fileList[ii] );
184 wxRemoveFile( thisFile.GetFullPath() );
185 }
186
187 for( SCH_SHEET_PATH& sheet : BuildSheetList( aNetlistOptions ) )
188 {
189 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
190 {
191 SCH_SYMBOL* symbol = findNextSymbol( item, sheet );
192
193 if( !symbol || symbol->ResolveExcludedFromSim( &sheet, variant ) )
194 continue;
195
196 try
197 {
198 SPICE_ITEM spiceItem;
199 std::vector<PIN_INFO> pins = CreatePinList( symbol, sheet, true );
200
201 for( const SCH_FIELD& field : symbol->GetFields() )
202 {
203 spiceItem.fields.emplace_back( symbol, FIELD_T::USER, field.GetName() );
204
205 if( field.GetId() == FIELD_T::REFERENCE )
206 spiceItem.fields.back().SetText( symbol->GetRef( &sheet ) );
207 else
208 spiceItem.fields.back().SetText( field.GetShownText( &sheet, false, 0, variant ) );
209 }
210
211 readRefName( sheet, *symbol, spiceItem, refNames );
212 readModel( sheet, *symbol, spiceItem, variant, aReporter );
213 readPinNumbers( *symbol, spiceItem, pins );
214 readPinNetNames( *symbol, spiceItem, pins, ncCounter );
215 readNodePattern( spiceItem );
216 // TODO: transmission line handling?
217
218 m_items.push_back( std::move( spiceItem ) );
219 }
220 catch( IO_ERROR& e )
221 {
222 aReporter.Report( e.What(), RPT_SEVERITY_ERROR );
223 }
224 }
225 }
226
228}
229
230
232{
233 MARKUP::MARKUP_PARSER markupParser( aNetName->ToStdString() );
234 std::unique_ptr<MARKUP::NODE> root = markupParser.Parse();
235
236 std::function<void( const std::unique_ptr<MARKUP::NODE>&)> convertMarkup =
237 [&]( const std::unique_ptr<MARKUP::NODE>& aNode )
238 {
239 if( aNode )
240 {
241 if( !aNode->is_root() )
242 {
243 if( aNode->isOverbar() )
244 {
245 // ~{CLK} is a different signal than CLK
246 *aNetName += '~';
247 }
248 else if( aNode->isSubscript() || aNode->isSuperscript() )
249 {
250 // V_{OUT} is just a pretty-printed version of VOUT
251 }
252
253 if( aNode->has_content() )
254 *aNetName += aNode->string();
255 }
256
257 for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
258 convertMarkup( child );
259 }
260 };
261
262 *aNetName = wxEmptyString;
263 convertMarkup( root );
264
265 // Replace all ngspice-disallowed chars in netnames by a '_'
266 aNetName->Replace( '%', '_' );
267 aNetName->Replace( '(', '_' );
268 aNetName->Replace( ')', '_' );
269 aNetName->Replace( ',', '_' );
270 aNetName->Replace( '[', '_' );
271 aNetName->Replace( ']', '_' );
272 aNetName->Replace( '<', '_' );
273 aNetName->Replace( '>', '_' );
274 aNetName->Replace( '~', '_' );
275 aNetName->Replace( ' ', '_' );
276
277 // Make sure that SPICE zero should be zero anywhere, independent if it is local or not.
278 // Therefore any signal ending with '/0' is rewritten as '0' to be recognized by SPICE.
279 if( aNetName->EndsWith( wxS( "/0" ) ) && !aNetName->EndsWith( wxS( "//0" ) ) )
280 aNetName->assign( wxS( "0" ) );
281
282 // Make sure that local ground signals with leading slash ('/gnd') are rewritten as gloabal gnd to be recognized
283 // by SPICE as zero.
284 if( aNetName->IsSameAs( wxS( "/gnd" ), false /* caseSensitive=false */ ) )
285 aNetName->assign( aNetName->Mid( 1 ) );
286
287 // A net name on the root sheet with a label '/foo' is going to get titled "//foo". This
288 // will trip up ngspice as "//" opens a line comment.
289 if( aNetName->StartsWith( wxS( "//" ) ) )
290 aNetName->Replace( wxS( "//" ), wxS( "/root/" ), false /* replace all */ );
291}
292
293
294wxString NETLIST_EXPORTER_SPICE::GetItemName( const wxString& aRefName ) const
295{
296 if( const SPICE_ITEM* item = FindItem( aRefName ) )
297 return item->model->SpiceGenerator().ItemName( *item );
298
299 return wxEmptyString;
300}
301
302
303const SPICE_ITEM* NETLIST_EXPORTER_SPICE::FindItem( const wxString& aRefName ) const
304{
305 const std::string refName = aRefName.ToStdString();
306 const std::list<SPICE_ITEM>& spiceItems = GetItems();
307
308 auto it = std::find_if( spiceItems.begin(), spiceItems.end(),
309 [&refName]( const SPICE_ITEM& item )
310 {
311 return item.refName == refName;
312 } );
313
314 if( it != spiceItems.end() )
315 return &*it;
316
317 return nullptr;
318}
319
320
321void NETLIST_EXPORTER_SPICE::ReadDirectives( unsigned aNetlistOptions )
322{
323 wxString text;
324
325 m_directives.clear();
326
327 for( const SCH_SHEET_PATH& sheet : BuildSheetList( aNetlistOptions ) )
328 {
329 for( SCH_ITEM* item : sheet.LastScreen()->Items() )
330 {
331 if( item->ResolveExcludedFromSim() )
332 continue;
333
334 if( item->Type() == SCH_TEXT_T )
335 text = static_cast<SCH_TEXT*>( item )->GetShownText( &sheet, false );
336 else if( item->Type() == SCH_TEXTBOX_T )
337 text = static_cast<SCH_TEXTBOX*>( item )->GetShownText( nullptr, &sheet, false );
338 else
339 continue;
340
341 // Send anything that contains directives to SPICE
342 wxStringTokenizer tokenizer( text, "\r\n", wxTOKEN_STRTOK );
343 bool foundDirective = false;
344
345 auto isDirective =
346 []( const wxString& line, const wxString& dir )
347 {
348 return line == dir || line.StartsWith( dir + wxS( " " ) );
349 };
350
351 while( tokenizer.HasMoreTokens() )
352 {
353 wxString line = tokenizer.GetNextToken().Upper();
354
355 if( line.StartsWith( wxT( "." ) ) )
356 {
357 if( isDirective( line, wxS( ".AC" ) )
358 || isDirective( line, wxS( ".CONTROL" ) )
359 || isDirective( line, wxS( ".CSPARAM" ) )
360 || isDirective( line, wxS( ".DISTO" ) )
361 || isDirective( line, wxS( ".DC" ) )
362 || isDirective( line, wxS( ".ELSE" ) )
363 || isDirective( line, wxS( ".ELSEIF" ) )
364 || isDirective( line, wxS( ".END" ) )
365 || isDirective( line, wxS( ".ENDC" ) )
366 || isDirective( line, wxS( ".ENDIF" ) )
367 || isDirective( line, wxS( ".ENDS" ) )
368 || isDirective( line, wxS( ".FOUR" ) )
369 || isDirective( line, wxS( ".FUNC" ) )
370 || isDirective( line, wxS( ".GLOBAL" ) )
371 || isDirective( line, wxS( ".IC" ) )
372 || isDirective( line, wxS( ".IF" ) )
373 || isDirective( line, wxS( ".INCLUDE" ) )
374 || isDirective( line, wxS( ".LIB" ) )
375 || isDirective( line, wxS( ".MEAS" ) )
376 || isDirective( line, wxS( ".MODEL" ) )
377 || isDirective( line, wxS( ".NODESET" ) )
378 || isDirective( line, wxS( ".NOISE" ) )
379 || isDirective( line, wxS( ".OP" ) )
380 || isDirective( line, wxS( ".OPTIONS" ) )
381 || isDirective( line, wxS( ".PARAM" ) )
382 || isDirective( line, wxS( ".PLOT" ) )
383 || isDirective( line, wxS( ".PRINT" ) )
384 || isDirective( line, wxS( ".PROBE" ) )
385 || isDirective( line, wxS( ".PZ" ) )
386 || isDirective( line, wxS( ".SAVE" ) )
387 || isDirective( line, wxS( ".SENS" ) )
388 || isDirective( line, wxS( ".SP" ) )
389 || isDirective( line, wxS( ".SUBCKT" ) )
390 || isDirective( line, wxS( ".TEMP" ) )
391 || isDirective( line, wxS( ".TF" ) )
392 || isDirective( line, wxS( ".TITLE" ) )
393 || isDirective( line, wxS( ".TRAN" ) )
394 || isDirective( line, wxS( ".WIDTH" ) ) )
395 {
396 foundDirective = true;
397 break;
398 }
399 }
400 else if( line.StartsWith( wxT( "K" ) ) )
401 {
402 // Check for mutual inductor declaration
403 wxStringTokenizer line_t( line, " \t", wxTOKEN_STRTOK );
404
405 // Coupling ID
406 if( !line_t.HasMoreTokens() || !line_t.GetNextToken().StartsWith( wxT( "K" ) ) )
407 continue;
408
409 // Inductor 1 ID
410 if( !line_t.HasMoreTokens() || !line_t.GetNextToken().StartsWith( wxT( "L" ) ) )
411 continue;
412
413 // Inductor 2 ID
414 if( !line_t.HasMoreTokens() || !line_t.GetNextToken().StartsWith( wxT( "L" ) ) )
415 continue;
416
417 // That's probably distinctive enough not to bother trying to parse the
418 // coupling value. If there's anything else, assume it's the value.
419 if( line_t.HasMoreTokens() )
420 {
421 foundDirective = true;
422 break;
423 }
424 }
425 }
426
427 if( foundDirective )
428 m_directives.emplace_back( text );
429 }
430 }
431}
432
433
435 const SCH_SHEET_PATH& aSheet,
436 const wxString& aVariantName )
437{
438 // Only process multi-unit symbols
439 if( !aSymbol.GetLibSymbolRef() || aSymbol.GetLibSymbolRef()->GetUnitCount() <= 1 )
440 return wxEmptyString;
441
442 wxString ref = aSymbol.GetRef( &aSheet );
443 std::vector<std::pair<wxString, wxString>> pinList;
444 std::set<wxString> pinNumbers;
445
446 // Helper to parse and collect pin mappings from a Sim.Pins field value
447 auto parsePins = [&]( const wxString& aPins )
448 {
449 wxStringTokenizer tokenizer( aPins, wxS( " \t\r\n" ), wxTOKEN_STRTOK );
450
451 while( tokenizer.HasMoreTokens() )
452 {
453 wxString token = tokenizer.GetNextToken();
454 int pos = token.Find( wxS( '=' ) );
455
456 if( pos == wxNOT_FOUND )
457 continue;
458
459 wxString pinNumber = token.Left( pos );
460 wxString modelPin = token.Mid( pos + 1 );
461
462 // Only add if we haven't seen this pin number before
463 if( pinNumbers.insert( pinNumber ).second )
464 pinList.emplace_back( pinNumber, modelPin );
465 }
466 };
467
468 // First, parse pins from the current symbol
469 if( SCH_FIELD* pinsField = aSymbol.GetField( SIM_PINS_FIELD ) )
470 parsePins( pinsField->GetShownText( &aSheet, false, 0, aVariantName ) );
471
472 // Then, find all other units with the same reference and collect their Sim.Pins
473 for( const SCH_SHEET_PATH& sheet : m_schematic->Hierarchy() )
474 {
475 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
476 {
477 SCH_SYMBOL* other = static_cast<SCH_SYMBOL*>( item );
478
479 if( other == &aSymbol )
480 continue;
481
482 if( other->GetRef( &sheet ) != ref )
483 continue;
484
485 if( SCH_FIELD* pinsField = other->GetField( SIM_PINS_FIELD ) )
486 parsePins( pinsField->GetShownText( &sheet, false, 0, aVariantName ) );
487 }
488 }
489
490 // If no pins were collected or only from current symbol, return empty
491 // (let the normal processing handle it)
492 if( pinList.empty() )
493 return wxEmptyString;
494
495 // Build the merged Sim.Pins string
496 wxString merged;
497
498 for( const auto& [pinNumber, modelPin] : pinList )
499 {
500 if( !merged.IsEmpty() )
501 merged += wxS( " " );
502
503 merged += pinNumber + wxS( "=" ) + modelPin;
504 }
505
506 return merged;
507}
508
509
510std::vector<UNIT_PIN_MAP> NETLIST_EXPORTER_SPICE::collectUnitPinMaps( SCH_SYMBOL& aSymbol,
511 const SCH_SHEET_PATH& aSheet,
512 const wxString& aVariantName )
513{
514 std::vector<UNIT_PIN_MAP> unitMaps;
515
516 if( !aSymbol.GetLibSymbolRef() || aSymbol.GetLibSymbolRef()->GetUnitCount() <= 1 )
517 return unitMaps;
518
519 wxString ref = aSymbol.GetRef( &aSheet );
520 std::set<int> seenUnits;
521
522 auto parseUnit =
523 [&]( SCH_SYMBOL& aUnit, const SCH_SHEET_PATH& aUnitSheet )
524 {
525 SCH_FIELD* pinsField = aUnit.GetField( SIM_PINS_FIELD );
526
527 if( !pinsField )
528 return;
529
530 wxString pins = pinsField->GetShownText( &aUnitSheet, false, 0, aVariantName );
531
532 // The same logical unit can be reached more than once through a reused hierarchical
533 // sheet; gather it only once so it does not synthesize duplicate instances.
534 if( !seenUnits.insert( aUnit.GetUnit() ).second )
535 return;
536
537 UNIT_PIN_MAP map;
538 map.unit = aUnit.GetUnit();
539 map.pins = ParseSimPinsTokens( pins, ref );
540
541 if( !map.pins.empty() )
542 unitMaps.push_back( std::move( map ) );
543 };
544
545 // The primary unit is processed first; the remaining units are matched case-insensitively
546 // across the whole hierarchy, mirroring findAllUnitsOfSymbol().
547 parseUnit( aSymbol, aSheet );
548
549 for( const SCH_SHEET_PATH& sheet : m_schematic->Hierarchy() )
550 {
551 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
552 {
553 SCH_SYMBOL* other = static_cast<SCH_SYMBOL*>( item );
554
555 if( other == &aSymbol )
556 continue;
557
558 if( other->GetRef( &sheet ).CmpNoCase( ref ) != 0 )
559 continue;
560
561 parseUnit( *other, sheet );
562 }
563 }
564
565 // Order instances by unit number so the generated wrapper is deterministic regardless of
566 // where each unit happens to be placed on the schematic.
567 std::sort( unitMaps.begin(), unitMaps.end(),
568 []( const UNIT_PIN_MAP& lhs, const UNIT_PIN_MAP& rhs )
569 {
570 return lhs.unit < rhs.unit;
571 } );
572
573 return unitMaps;
574}
575
576
578 const SCH_SHEET_PATH& aSheet,
579 const wxString& aVariantName ) const
580{
581 auto read =
582 [&]( SCH_SYMBOL& aUnit, const SCH_SHEET_PATH& aUnitSheet ) -> wxString
583 {
584 if( SCH_FIELD* field = aUnit.GetField( SIM_DECOMPOSITION_FIELD ) )
585 return field->GetShownText( &aUnitSheet, false, 0, aVariantName );
586
587 return wxEmptyString;
588 };
589
590 // Decomposition is a component-level flag. Prefer the primary unit, but fall back to any
591 // sibling so it is found regardless of which unit carries it.
592 wxString value = read( aSymbol, aSheet );
593
594 if( value.IsEmpty() && aSymbol.GetLibSymbolRef() && aSymbol.GetLibSymbolRef()->GetUnitCount() > 1 )
595 {
596 wxString ref = aSymbol.GetRef( &aSheet );
597
598 for( const SCH_SHEET_PATH& sheet : m_schematic->Hierarchy() )
599 {
600 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
601 {
602 SCH_SYMBOL* other = static_cast<SCH_SYMBOL*>( item );
603
604 if( other == &aSymbol || other->GetRef( &sheet ).CmpNoCase( ref ) != 0 )
605 continue;
606
607 value = read( *other, sheet );
608
609 if( !value.IsEmpty() )
610 return SIM_DECOMPOSITION::Parse( value );
611 }
612 }
613 }
614
615 return SIM_DECOMPOSITION::Parse( value );
616}
617
618
620 SPICE_ITEM& aItem, std::set<std::string>& aRefNames )
621{
622 aItem.refName = aSymbol.GetRef( &aSheet );
623
624 [[maybe_unused]] bool inserted = aRefNames.insert( aItem.refName ).second;
625 wxASSERT_MSG( inserted, wxT( "Duplicate refdes encountered; what happened to ReadyToNetlist()?" ) );
626}
627
628
630 const wxString& aVariantName, REPORTER& aReporter )
631{
632 SIM_DECOMPOSITION decomposition = getDecomposition( aSymbol, aSheet, aVariantName );
633
634 bool multiUnit = aSymbol.GetLibSymbolRef() && aSymbol.GetLibSymbolRef()->GetUnitCount() > 1;
635 bool repeat = decomposition.mode == SIM_DECOMPOSITION::MODE::REPEAT_PER_UNIT && multiUnit;
636
637 // The whole-device default merges the per-unit Sim.Pins into one instance. Repeat mode keeps
638 // the units distinct and synthesizes a wrapper, so it must not merge.
639 wxString mergedSimPins = repeat ? wxString()
640 : collectMergedSimPins( aSymbol, aSheet, aVariantName );
641
642 const SIM_LIBRARY::MODEL& libModel = m_libMgr.CreateModel( &aSheet, aSymbol, true, 0, aVariantName,
643 aReporter, mergedSimPins );
644
645 aItem.baseModelName = libModel.name;
646 aItem.model = &libModel.model;
647
648 if( repeat )
649 {
650 // The wrapper instantiates the base as a subcircuit (inner X lines), so it only supports a
651 // named subcircuit base model. Built-in/IBIS/unresolved models would yield invalid inner
652 // instances, so reject them with a clear error rather than emit a broken netlist.
653 if( libModel.model.GetType() != SIM_MODEL::TYPE::SUBCKT || libModel.name.empty() )
654 {
655 THROW_IO_ERROR( wxString::Format(
656 _( "Symbol '%s' uses repeat-per-unit decomposition, which requires a named "
657 "subcircuit model." ),
658 aSymbol.GetRef( &aSheet ) ) );
659 }
660
661 std::vector<UNIT_PIN_MAP> unitMaps = collectUnitPinMaps( aSymbol, aSheet, aVariantName );
662
663 // The wrapper copies what it needs from libModel.model at construction; m_multiunitModels
664 // keeps it alive for as long as m_items references it via aItem.model. Build it even for a
665 // single functional unit so that shared pins carried by other units are still wired (the
666 // constructor throws if no instances result). It owns its content-derived name so
667 // identical components share one definition; do not run it through the per-item uniquifier.
668 auto wrapper = std::make_unique<SIM_MODEL_MULTIUNIT>( libModel.model, libModel.name, unitMaps,
669 decomposition.sharedModelPins );
670
671 aItem.model = wrapper.get();
672 aItem.baseModelName = wrapper->GetSignature();
673 aItem.modelName = wrapper->GetSignature().ToStdString();
674
675 m_multiunitModels.push_back( std::move( wrapper ) );
676 return;
677 }
678
679 std::string modelName = aItem.model->SpiceGenerator().ModelName( aItem );
680
681 // Only uniquify names that KiCad itself defines with a .model line. A subcircuit (or other
682 // externally defined) name has to match the definition pulled in from its library verbatim, and
683 // several symbols sharing one subcircuit must resolve to that same name, so it is left untouched.
684 if( aItem.model->requiresSpiceModelLine( aItem ) )
685 aItem.modelName = m_modelNameGenerator.Generate( modelName );
686 else
687 aItem.modelName = modelName;
688
689 // FIXME: Don't have special cases for raw Spice models and KIBIS.
690 if( auto rawSpiceModel = dynamic_cast<const SIM_MODEL_RAW_SPICE*>( aItem.model ) )
691 {
692 int libParamIndex = static_cast<int>( SIM_MODEL_RAW_SPICE::SPICE_PARAM::LIB );
693 wxString path = rawSpiceModel->GetParam( libParamIndex ).value;
694
695 if( !path.IsEmpty() )
696 m_rawIncludes.insert( path );
697 }
698 else if( auto ibisModel = dynamic_cast<const SIM_MODEL_IBIS*>( aItem.model ) )
699 {
700 wxFileName cacheFn;
701 cacheFn.AssignDir( PATHS::GetUserCachePath() );
702 cacheFn.AppendDir( wxT( "ibis" ) );
703 cacheFn.SetFullName( aSymbol.GetRef( &aSheet ) + wxT( ".cache" ) );
704
705 wxFile cacheFile( cacheFn.GetFullPath(), wxFile::write );
706
707 if( !cacheFile.IsOpened() )
708 {
709 wxLogError( _( "Could not open file '%s' to write IBIS model" ),
710 cacheFn.GetFullPath() );
711 }
712
713 auto spiceGenerator = static_cast<const SPICE_GENERATOR_IBIS&>( ibisModel->SpiceGenerator() );
714
715 wxString cacheFilepath = cacheFn.GetPath( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR );
716 std::string modelData = spiceGenerator.IbisDevice( aItem, m_schematic,
717 cacheFilepath, aReporter );
718
719 cacheFile.Write( wxString( modelData ) );
720 m_rawIncludes.insert( cacheFn.GetFullPath() );
721 }
722}
723
724
726 const std::vector<PIN_INFO>& aPins )
727{
728 for( const PIN_INFO& pin : aPins )
729 aItem.pinNumbers.emplace_back( pin.num.ToStdString() );
730}
731
732
734 const std::vector<PIN_INFO>& aPins, int& aNcCounter )
735{
736 for( const PIN_INFO& pinInfo : aPins )
737 {
738 wxString netName = GenerateItemPinNetName( pinInfo.netName, aNcCounter );
739
740 aItem.pinNetNames.push_back( netName.ToStdString() );
741 m_nets.insert( netName );
742 }
743}
744
745
747 std::vector<std::string>& aModifiers )
748{
749 std::string input = GetFieldValue( &aItem.fields, SIM_NODES_FORMAT_FIELD, true, 0 );
750
751 if( input == "" )
752 return;
753
754 tao::pegtl::string_input<> in( input, "Sim.NodesFormat field" );
755 std::unique_ptr<tao::pegtl::parse_tree::node> root;
756 std::string singleNodeModifier;
757
758 try
759 {
760 root = tao::pegtl::parse_tree::parse<SIM_XSPICE_PARSER_GRAMMAR::nodeSequenceGrammar,
762 tao::pegtl::nothing,
764 for( const auto& node : root->children )
765 {
766 if( node->is_type<SIM_XSPICE_PARSER_GRAMMAR::squareBracketC>() )
767 {
768 //we want ']' to close previous ?
769 aModifiers.back().append( node->string() );
770 }
771 else
772 { //rest goes to the new singleNodeModifier
773 singleNodeModifier.append( node->string() );
774 }
775
776 if( node->is_type<SIM_XSPICE_PARSER_GRAMMAR::nodeName>() )
777 {
778 aModifiers.push_back( singleNodeModifier );
779 singleNodeModifier.erase( singleNodeModifier.begin(), singleNodeModifier.end() );
780 }
781 }
782 }
783 catch( const tao::pegtl::parse_error& e )
784 {
785 THROW_IO_ERROR( wxString::Format( _( "Error in parsing model '%s', error: '%s'" ),
786 aItem.refName, e.what() ) );
787 }
788}
790{
791 std::vector<std::string> xspicePattern;
792 NETLIST_EXPORTER_SPICE::getNodePattern( aItem, xspicePattern );
793
794 if( xspicePattern.empty() )
795 return;
796
797 if( xspicePattern.size() != aItem.pinNetNames.size() )
798 {
799 THROW_IO_ERROR( wxString::Format( _( "Error in parsing model '%s', wrong number of nodes "
800 "'?' in Sim.NodesFormat compared to connections" ),
801 aItem.refName ) );
802 return;
803 }
804
805 auto itNetNames = aItem.pinNetNames.begin();
806
807 for( std::string& pattern : xspicePattern )
808 {
809 // ngspice does not care about aditional spaces, and we make sure that "%d?" is separated
810 const std::string netName = " " + *itNetNames + " ";
811 pattern.replace( pattern.find( "?" ), 1, netName );
812 *itNetNames = pattern;
813 ++itNetNames;
814 }
815}
816
817void NETLIST_EXPORTER_SPICE::writeInclude( OUTPUTFORMATTER& aFormatter, unsigned aNetlistOptions,
818 const wxString& aPath )
819{
820 // First, expand env vars, if any.
821 wxString expandedPath = ExpandEnvVarSubstitutions( aPath, &m_schematic->Project() );
822
823 // Path may have been authored by someone on a Windows box; convert it to UNIX format
824 expandedPath.Replace( '\\', '/' );
825
826 wxString fullPath;
827
828 if( aNetlistOptions & OPTION_ADJUST_INCLUDE_PATHS )
829 {
830 // Look for the library in known search locations.
831 fullPath = ResolveFile( expandedPath, &Pgm().GetLocalEnvVariables(), &m_schematic->Project() );
832
833 if( fullPath.IsEmpty() )
834 {
835 wxLogError( _( "Could not find library file '%s'" ), expandedPath );
836 fullPath = expandedPath;
837 }
838 else if( wxFileName::GetPathSeparator() == '\\' )
839 {
840 // Convert it to UNIX format (again) if ResolveFile() returned a Windows style path
841 fullPath.Replace( '\\', '/' );
842 }
843 }
844 else
845 {
846 fullPath = expandedPath;
847 }
848
849 aFormatter.Print( 0, ".include \"%s\"\n", TO_UTF8( fullPath ) );
850}
851
852
853void NETLIST_EXPORTER_SPICE::writeIncludes( OUTPUTFORMATTER& aFormatter, unsigned aNetlistOptions )
854{
855 for( const auto& [path, library] : m_libMgr.GetLibraries() )
856 {
857 if( dynamic_cast<const SIM_LIBRARY_SPICE*>( &library.get() ) )
858 writeInclude( aFormatter, aNetlistOptions, path );
859 }
860
861 for( const wxString& path : m_rawIncludes )
862 writeInclude( aFormatter, aNetlistOptions, path );
863}
864
865
867{
868 std::set<std::string> emittedWrappers;
869
870 for( const SPICE_ITEM& item : m_items )
871 {
872 if( !item.model->IsEnabled() )
873 continue;
874
875 // Identical multi-unit wrappers share one content-derived .subckt definition, but each is
876 // a distinct item, so emit any given wrapper exactly once.
877 if( dynamic_cast<const SIM_MODEL_MULTIUNIT*>( item.model )
878 && !emittedWrappers.insert( item.modelName ).second )
879 {
880 continue;
881 }
882
883 aFormatter.Print( 0, "%s", item.model->SpiceGenerator().ModelLine( item ).c_str() );
884 }
885}
886
887
889{
890 for( const SPICE_ITEM& item : m_items )
891 {
892 if( !item.model->IsEnabled() )
893 continue;
894
895 aFormatter.Print( 0, "%s", item.model->SpiceGenerator().ItemLine( item ).c_str() );
896 }
897}
898
899
900void NETLIST_EXPORTER_SPICE::WriteDirectives( const wxString& aSimCommand, unsigned aSimOptions,
901 OUTPUTFORMATTER& aFormatter ) const
902{
903 if( aSimOptions & OPTION_SAVE_ALL_VOLTAGES )
904 aFormatter.Print( 0, ".save all\n" );
905
906 if( aSimOptions & OPTION_SAVE_ALL_CURRENTS )
907 aFormatter.Print( 0, ".probe alli\n" );
908
909 if( aSimOptions & OPTION_SAVE_ALL_DISSIPATIONS )
910 {
911 for( const SPICE_ITEM& item : m_items )
912 {
913 // ngspice (v39) does not support power measurement for XSPICE devices
914 // XPSICE devices are marked with 'A'
915 std::string itemName = item.model->SpiceGenerator().ItemName( item );
916
917 if( ( item.model->GetPinCount() >= 2 ) && ( itemName.size() > 0 )
918 && ( itemName.c_str()[0] != 'A' ) )
919 {
920 aFormatter.Print( 0, ".probe p(%s)\n", itemName.c_str() );
921 }
922 }
923 }
924
925 auto isSimCommand =
926 []( const wxString& candidate, const wxString& dir )
927 {
928 return candidate == dir || candidate.StartsWith( dir + wxS( " " ) );
929 };
930
931 for( const wxString& directive : m_directives )
932 {
933 bool simCommand = false;
934
935 if( directive.StartsWith( "." ) )
936 {
937 wxString candidate = directive.Upper();
938
939 simCommand = ( isSimCommand( candidate, wxS( ".AC" ) )
940 || isSimCommand( candidate, wxS( ".DC" ) )
941 || isSimCommand( candidate, wxS( ".TRAN" ) )
942 || isSimCommand( candidate, wxS( ".OP" ) )
943 || isSimCommand( candidate, wxS( ".DISTO" ) )
944 || isSimCommand( candidate, wxS( ".NOISE" ) )
945 || isSimCommand( candidate, wxS( ".PZ" ) )
946 || isSimCommand( candidate, wxS( ".SENS" ) )
947 || isSimCommand( candidate, wxS( ".TF" ) ) );
948 }
949
950 if( !simCommand || ( aSimOptions & OPTION_SIM_COMMAND ) )
951 aFormatter.Print( 0, "%s\n", UTF8( directive ).c_str() );
952 }
953}
954
955
956wxString NETLIST_EXPORTER_SPICE::GenerateItemPinNetName( const wxString& aNetName,
957 int& aNcCounter ) const
958{
959 wxString netName = UnescapeString( aNetName );
960
961 ConvertToSpiceMarkup( &netName );
962
963 if( netName.IsEmpty() )
964 netName.Printf( wxS( "NC-%d" ), aNcCounter++ );
965
966 return netName;
967}
968
969
971{
972 SCH_SHEET_LIST sheets;
973
974 if( aNetlistOptions & OPTION_CUR_SHEET_AS_ROOT )
975 sheets = SCH_SHEET_LIST( m_schematic->CurrentSheet().Last() );
976 else
977 sheets = m_schematic->Hierarchy();
978
979 std::erase_if( sheets,
980 [&]( const SCH_SHEET_PATH& sheet )
981 {
982 return sheet.GetExcludedFromSim();
983 } );
984
985 return sheets;
986}
987
const char * name
Used for text file output.
Definition richio.h:470
bool Finish() override
Flushes the temp file to disk and atomically renames it over the final target path.
Definition richio.cpp:642
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
int GetUnitCount() const override
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
std::unique_ptr< NODE > Parse()
std::unordered_set< std::string > m_names
std::string Generate(const std::string &aProposedName)
SCHEMATIC * m_schematic
The schematic we're generating a netlist for.
std::vector< PIN_INFO > CreatePinList(SCH_SYMBOL *aSymbol, const SCH_SHEET_PATH &aSheetPath, bool aKeepUnconnectedPins)
Find a symbol from the DrawList and builds its pin list.
SCH_SYMBOL * findNextSymbol(EDA_ITEM *aItem, const SCH_SHEET_PATH &aSheetPath)
Check if the given symbol should be processed for netlisting.
std::set< LIB_SYMBOL *, LIB_SYMBOL_LESS_THAN > m_libParts
unique library symbols used. LIB_SYMBOL items are sorted by names
NETLIST_EXPORTER_BASE(SCHEMATIC *aSchematic)
UNIQUE_STRINGS m_referencesAlreadyFound
Used for "multiple symbols per package" symbols to avoid processing a lib symbol more than once.
void readPinNetNames(SCH_SYMBOL &aSymbol, SPICE_ITEM &aItem, const std::vector< PIN_INFO > &aPins, int &aNcCounter)
void writeModels(OUTPUTFORMATTER &aFormatter)
void writeIncludes(OUTPUTFORMATTER &aFormatter, unsigned aNetlistOptions)
std::list< SPICE_ITEM > m_items
Owns the synthesized repeat-per-unit wrappers referenced by m_items.
void getNodePattern(SPICE_ITEM &aItem, std::vector< std::string > &aModifiers)
static void ConvertToSpiceMarkup(wxString *aNetName)
Remove formatting wrappers and replace illegal spice net name characters with underscores.
void ReadDirectives(unsigned aNetlistOptions)
SCH_SHEET_LIST BuildSheetList(unsigned aNetlistOptions=0) const
Return the paths of exported sheets (either all or the current one).
virtual wxString GenerateItemPinNetName(const wxString &aNetName, int &aNcCounter) const
std::set< wxString > m_nets
Items representing schematic symbols in Spice world.
void readRefName(SCH_SHEET_PATH &aSheet, SCH_SYMBOL &aSymbol, SPICE_ITEM &aItem, std::set< std::string > &aRefNames)
wxString GetItemName(const wxString &aRefName) const
Return name of Spice device corresponding to a schematic symbol.
void writeItems(OUTPUTFORMATTER &aFormatter)
std::vector< wxString > m_directives
Spice directives found in the schematic sheet.
virtual void WriteHead(OUTPUTFORMATTER &aFormatter, unsigned aNetlistOptions)
Write the netlist head (title and so on).
void writeInclude(OUTPUTFORMATTER &aFormatter, unsigned aNetlistOptions, const wxString &aPath)
virtual void WriteDirectives(const wxString &aSimCommand, unsigned aSimOptions, OUTPUTFORMATTER &candidate) const
NETLIST_EXPORTER_SPICE(SCHEMATIC *aSchematic)
wxString collectMergedSimPins(SCH_SYMBOL &aSymbol, const SCH_SHEET_PATH &aSheet, const wxString &aVariantName)
Collect merged Sim.Pins from all units of a multi-unit symbol.
SIM_LIB_MGR m_libMgr
Holds libraries and models.
const SPICE_ITEM * FindItem(const wxString &aRefName) const
Find and return the item corresponding to aRefName.
virtual bool ReadSchematicAndLibraries(unsigned aNetlistOptions, REPORTER &aReporter)
Process the schematic and Spice libraries to create net mapping and a list of SPICE_ITEMs.
SIM_DECOMPOSITION getDecomposition(SCH_SYMBOL &aSymbol, const SCH_SHEET_PATH &aSheet, const wxString &aVariantName) const
Read and parse the Sim.Decomposition field from the primary unit.
bool DoWriteNetlist(const wxString &aSimCommand, unsigned aSimOptions, OUTPUTFORMATTER &aFormatter, REPORTER &aReporter)
Write the netlist in aFormatter.
void readPinNumbers(SCH_SYMBOL &aSymbol, SPICE_ITEM &aItem, const std::vector< PIN_INFO > &aPins)
const std::list< SPICE_ITEM > & GetItems() const
Return the list of items representing schematic symbols in the Spice world.
void readNodePattern(SPICE_ITEM &aItem)
std::set< wxString > m_rawIncludes
include directives found in symbols
void readModel(SCH_SHEET_PATH &aSheet, SCH_SYMBOL &aSymbol, SPICE_ITEM &aItem, const wxString &aVariantName, REPORTER &aReporter)
virtual void WriteTail(OUTPUTFORMATTER &aFormatter, unsigned aNetlistOptions)
Write the tail (.end).
NAME_GENERATOR m_modelNameGenerator
Generates unique model names.
std::vector< std::unique_ptr< SIM_MODEL_MULTIUNIT > > m_multiunitModels
bool WriteNetlist(const wxString &aOutFileName, unsigned aNetlistOptions, REPORTER &aReporter) override
Write to specified output file.
std::vector< UNIT_PIN_MAP > collectUnitPinMaps(SCH_SYMBOL &aSymbol, const SCH_SHEET_PATH &aSheet, const wxString &aVariantName)
Gather every unit's Sim.Pins mapping for a multi-unit symbol, one entry per unit.
An interface used to output 8 bit text in a convenient way.
Definition richio.h:291
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:422
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
Definition paths.cpp:460
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
Holds all the data relating to one schematic.
Definition schematic.h:90
EMBEDDED_FILES * GetEmbeddedFiles() override
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0, const wxString &aVariantName=wxEmptyString) const
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
int GetUnit() const
Definition sch_item.h:233
bool ResolveExcludedFromSim(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const
Definition sch_item.cpp:298
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
bool GetExcludedFromSim() const
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::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:177
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
Wraps a resolved single-unit base model and presents it as one component-level SPICE device.
virtual bool requiresSpiceModelLine(const SPICE_ITEM &aItem) const
const SPICE_GENERATOR & SpiceGenerator() const
Definition sim_model.h:428
TYPE GetType() const
Definition sim_model.h:458
std::string IbisDevice(const SPICE_ITEM &aItem, SCHEMATIC *aSchematic, const wxString &aCacheDir, REPORTER &aReporter) const
virtual std::string ModelName(const SPICE_ITEM &aItem) const
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition utf8.h:67
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.
This file is part of the common library.
#define _(s)
wxString ResolveFile(const wxString &aFileName, const ENV_VAR_MAP *aEnvVars, const PROJECT *aProject)
Search the default paths trying to find one with the requested file.
Helper functions to substitute paths with environmental variables.
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
must_if< error >::control< Rule > control
PGM_BASE & Pgm()
The global program "get" accessor.
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_NODES_FORMAT_FIELD
Definition sim_model.h:55
#define SIM_DECOMPOSITION_FIELD
Definition sim_model.h:51
std::vector< std::pair< wxString, wxString > > ParseSimPinsTokens(const wxString &aPins, const wxString &aRef)
Parse one unit's Sim.Pins text into (symbolPinNumber -> modelPinName) pairs, preserving the written o...
std::vector< FAB_LAYER_COLOR > dummy
wxString UnescapeString(const wxString &aSource)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Per-component decomposition descriptor stored in the Sim.Decomposition field.
static SIM_DECOMPOSITION Parse(const wxString &aField)
std::vector< wxString > sharedModelPins
SIM_MODEL & model
Definition sim_library.h:37
std::string name
Definition sim_library.h:36
Notes: spaces are allowed everywhere in any number ~ can only be before ?
std::string refName
std::vector< SCH_FIELD > fields
std::string modelName
const SIM_MODEL * model
std::vector< std::string > pinNetNames
std::string baseModelName
std::vector< std::string > pinNumbers
One functional unit's pin map, gathered from its Sim.Pins field.
std::vector< std::pair< wxString, wxString > > pins
@ USER
The field ID hasn't been set yet; field is invalid.
@ REFERENCE
Field Reference of part, i.e. "IC21".
std::string path
KIBIS_PIN * pin
wxString result
Test unit parsing edge cases and error handling.
@ SCH_SYMBOL_T
Definition typeinfo.h:169
@ SCH_TEXT_T
Definition typeinfo.h:148
@ SCH_TEXTBOX_T
Definition typeinfo.h:149