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