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-2023 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>
31
32#include <boost/algorithm/string/trim.hpp>
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{
60 SIM_MODEL::TYPE modelType = SPICE_MODEL_PARSER::ReadType( aLibrary, aSpiceCode );
61 std::unique_ptr<SIM_MODEL> model = SIM_MODEL::Create( modelType );
62
63 if( SIM_MODEL_SPICE* spiceModel = dynamic_cast<SIM_MODEL_SPICE*>( model.release() ) )
64 {
65 try
66 {
67 spiceModel->m_spiceModelParser->ReadModel( aLibrary, aSpiceCode );
68 return std::unique_ptr<SIM_MODEL_SPICE>( spiceModel );
69 }
70 catch( const IO_ERROR& )
71 {
72 // Fall back to raw spice code
73 }
74 }
75
76 // Fall back to raw spice code
77 return std::make_unique<SIM_MODEL_SPICE_FALLBACK>( modelType, aSpiceCode );
78}
79
80
81SIM_MODEL_SPICE::SIM_MODEL_SPICE( TYPE aType, std::unique_ptr<SPICE_GENERATOR> aSpiceGenerator ) :
82 SIM_MODEL( aType, std::move( aSpiceGenerator ) ),
83 m_spiceModelParser( std::make_unique<SPICE_MODEL_PARSER>( *this ) )
84{
85}
86
87
88SIM_MODEL_SPICE::SIM_MODEL_SPICE( TYPE aType, std::unique_ptr<SPICE_GENERATOR> aSpiceGenerator,
89 std::unique_ptr<SPICE_MODEL_PARSER> aSpiceModelParser ) :
90 SIM_MODEL( aType, std::move( aSpiceGenerator ) ),
91 m_spiceModelParser( std::move( aSpiceModelParser ) )
92{
93}
94
95
96void SIM_MODEL_SPICE::SetParamFromSpiceCode( const std::string& aParamName,
97 const std::string& aParamValue,
99{
100 SIM_MODEL::SetParamValue( aParamName, aParamValue, aNotation );
101}
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
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)
const SIM_MODEL * GetBaseModel() const
Definition: sim_model.h:467
static std::unique_ptr< SIM_MODEL > Create(TYPE aType, const std::vector< SCH_PIN * > &aPins, REPORTER &aReporter)
Definition: sim_model.cpp:492
void SetParamValue(int aParamIndex, const std::string &aValue, SIM_VALUE::NOTATION aNotation=SIM_VALUE::NOTATION::SI)
Definition: sim_model.cpp:848
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 SIM_MODEL::TYPE ReadType(const SIM_LIBRARY_SPICE &aLibrary, const std::string &aSpiceCode)
STL namespace.
SIM_MODEL::TYPE TYPE
Definition: sim_model.cpp:53
std::string refName