KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sim_model_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) 2022 Mikolaj Wielgus
5 * Copyright (C) 2022-2024 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 simulator headers after wxWidgets headers to avoid conflicts with Windows headers
26// (especially on msys2 + wxWidgets 3.0.x)
27#include <sim/sim_model_spice.h>
30
31#include <boost/algorithm/string/trim.hpp>
32#include <ki_exception.h>
33
34
35std::string SPICE_GENERATOR_SPICE::Preview( const SPICE_ITEM& aItem ) const
36{
37 std::string spiceCode = ModelLine( aItem );
38
39 if( spiceCode == "" )
40 spiceCode = static_cast<const SIM_MODEL_SPICE&>( m_model ).m_spiceCode;
41
42 if( spiceCode == "" && m_model.GetBaseModel() )
43 spiceCode = static_cast<const SIM_MODEL_SPICE*>( m_model.GetBaseModel() )->m_spiceCode;
44
45 SPICE_ITEM item = aItem;
46 item.refName = "";
47 std::string itemLine = ItemLine( item );
48
49 if( spiceCode != "" && itemLine != "" )
50 spiceCode.append( "\n\n" );
51
52 spiceCode.append( itemLine );
53 return boost::trim_copy( spiceCode );
54}
55
56
57std::unique_ptr<SIM_MODEL_SPICE> SIM_MODEL_SPICE::Create( const SIM_LIBRARY_SPICE& aLibrary,
58 const std::string& aSpiceCode,
59 bool aFirstPass, REPORTER& aReporter )
60{
61 SIM_MODEL::TYPE type = SIM_MODEL::TYPE::NONE;
62
63 if( !SPICE_MODEL_PARSER::ReadType( aLibrary, aSpiceCode, &type, aFirstPass ) )
64 return nullptr;
65
66 std::unique_ptr<SIM_MODEL> model = SIM_MODEL::Create( type );
67
68 if( dynamic_cast<SIM_MODEL_SPICE*>( model.get() ) )
69 {
70 std::unique_ptr<SIM_MODEL_SPICE> spiceModel( static_cast<SIM_MODEL_SPICE*>( model.release() ) );
71
72 try
73 {
74 spiceModel->m_spiceModelParser->ReadModel( aLibrary, aSpiceCode );
75 return spiceModel;
76 }
77 catch( const IO_ERROR& )
78 {
79 // Fall back to raw spice code
80 }
81 }
82
83 // Fall back to raw spice code
84 return std::make_unique<SIM_MODEL_SPICE_FALLBACK>( type, aSpiceCode );
85}
86
87
88SIM_MODEL_SPICE::SIM_MODEL_SPICE( TYPE aType, std::unique_ptr<SPICE_GENERATOR> aSpiceGenerator ) :
89 SIM_MODEL( aType, std::move( aSpiceGenerator ) ),
90 m_spiceModelParser( std::make_unique<SPICE_MODEL_PARSER>( *this ) )
91{
92}
93
94
95SIM_MODEL_SPICE::SIM_MODEL_SPICE( TYPE aType, std::unique_ptr<SPICE_GENERATOR> aSpiceGenerator,
96 std::unique_ptr<SPICE_MODEL_PARSER> aSpiceModelParser ) :
97 SIM_MODEL( aType, std::move( aSpiceGenerator ) ),
98 m_spiceModelParser( std::move( aSpiceModelParser ) )
99{
100}
101
102
103void SIM_MODEL_SPICE::SetParamFromSpiceCode( const std::string& aParamName,
104 const std::string& aParamValue,
106{
107 SIM_MODEL::SetParamValue( aParamName, aParamValue, aNotation );
108}
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:72
SIM_MODEL_SPICE(TYPE aType, std::unique_ptr< SPICE_GENERATOR > aSpiceGenerator)
virtual void SetParamFromSpiceCode(const std::string &aParamName, const std::string &aParamValue, SIM_VALUE_GRAMMAR::NOTATION aNotation=SIM_VALUE_GRAMMAR::NOTATION::SPICE)
static std::unique_ptr< SIM_MODEL_SPICE > Create(const SIM_LIBRARY_SPICE &aLibrary, const std::string &aSpiceCode, bool aFirstPass, REPORTER &aReporter)
const SIM_MODEL * GetBaseModel() const
Definition: sim_model.h:466
static std::unique_ptr< SIM_MODEL > Create(TYPE aType, const std::vector< SCH_PIN * > &aPins, REPORTER &aReporter)
Definition: sim_model.cpp:496
void SetParamValue(int aParamIndex, const std::string &aValue, SIM_VALUE::NOTATION aNotation=SIM_VALUE::NOTATION::SI)
Definition: sim_model.cpp:845
std::string Preview(const SPICE_ITEM &aItem) const override
virtual std::string ItemLine(const SPICE_ITEM &aItem) const
virtual std::string ModelLine(const SPICE_ITEM &aItem) const
const SIM_MODEL & m_model
static bool ReadType(const SIM_LIBRARY_SPICE &aLibrary, const std::string &aSpiceCode, SIM_MODEL::TYPE *aType, bool aFirstPass)
STL namespace.
SIM_MODEL::TYPE TYPE
Definition: sim_model.cpp:57
std::string refName