KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sim_model_behavioral.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
26
27#include <boost/algorithm/string/replace.hpp>
28#include <boost/algorithm/string/trim.hpp>
29#include <fmt/core.h>
30
31
32std::string SPICE_GENERATOR_BEHAVIORAL::ModelLine( const SPICE_ITEM& aItem ) const
33{
34 return "";
35}
36
37
38std::string SPICE_GENERATOR_BEHAVIORAL::ItemLine( const SPICE_ITEM& aItem ) const
39{
40 switch( m_model.GetType() )
41 {
42 case SIM_MODEL::TYPE::R_BEHAVIORAL:
43 case SIM_MODEL::TYPE::C_BEHAVIORAL:
44 case SIM_MODEL::TYPE::L_BEHAVIORAL:
45 {
46 SPICE_ITEM item = aItem;
48 return SPICE_GENERATOR::ItemLine( item );
49 }
50
51 case SIM_MODEL::TYPE::V_BEHAVIORAL:
52 {
53 SPICE_ITEM item = aItem;
54 item.modelName = fmt::format( "V={}", SIM_VALUE::ToSpice( m_model.GetParam( 0 ).value ) );
55 return SPICE_GENERATOR::ItemLine( item );
56 }
57
58 case SIM_MODEL::TYPE::I_BEHAVIORAL:
59 {
60 SPICE_ITEM item = aItem;
61 item.modelName = fmt::format( "I={}", SIM_VALUE::ToSpice( m_model.GetParam( 0 ).value ) );
62 return SPICE_GENERATOR::ItemLine( item );
63 }
64
65 default:
66 wxFAIL_MSG( "Unhandled SIM_MODEL type in SIM_MODEL_BEHAVIORAL" );
67 return "";
68 }
69}
70
71
73 SIM_MODEL( aType, std::make_unique<SPICE_GENERATOR_BEHAVIORAL>( *this ) )
74{
75 static PARAM::INFO resistor = makeParams( "r", "Expression for resistance", "Ω" );
76 static PARAM::INFO capacitor = makeParams( "c", "Expression for capacitance", "F" );
77 static PARAM::INFO inductor = makeParams( "l", "Expression for inductance", "H" );
78 static PARAM::INFO vsource = makeParams( "v", "Expression for voltage", "V" );
79 static PARAM::INFO isource = makeParams( "i", "Expression for current", "A" );
80
81 switch( aType )
82 {
83 case TYPE::R_BEHAVIORAL: AddParam( resistor ); break;
84 case TYPE::C_BEHAVIORAL: AddParam( capacitor ); break;
85 case TYPE::L_BEHAVIORAL: AddParam( inductor ); break;
86 case TYPE::V_BEHAVIORAL: AddParam( vsource ); break;
87 case TYPE::I_BEHAVIORAL: AddParam( isource ); break;
88 default:
89 wxFAIL_MSG( "Unhandled SIM_MODEL type in SIM_MODEL_IDEAL" );
90 }
91}
92
93
94bool SIM_MODEL_BEHAVIORAL::parseValueField( const std::string& aValueField )
95{
96 std::string expr = aValueField;
97
98 if( expr.find( "=" ) == std::string::npos )
99 return false;
100
101 boost::replace_first( expr, "=", "" );
102
103 SetParamValue( 0, boost::trim_copy( expr ) );
104 return true;
105}
106
107
109 const std::string& aDescription,
110 const std::string& aUnit )
111{
112 PARAM::INFO paramInfo( aName, 0, PARAM::DIR_INOUT, SIM_VALUE::TYPE_STRING, aUnit,
114
115 paramInfo.description = aDescription;
116
117 return paramInfo;
118}
bool parseValueField(const std::string &aValueField)
static PARAM::INFO makeParams(const std::string &aName, const std::string &aDescription, const std::string &aUnit)
void AddParam(const PARAM::INFO &aInfo)
Definition: sim_model.cpp:729
virtual const PARAM & GetParam(unsigned aParamIndex) const
Definition: sim_model.cpp:789
void SetParamValue(int aParamIndex, const std::string &aValue, SIM_VALUE::NOTATION aNotation=SIM_VALUE::NOTATION::SI)
Definition: sim_model.cpp:845
TYPE GetType() const
Definition: sim_model.h:464
@ TYPE_STRING
Definition: sim_value.h:71
static std::string ToSpice(const std::string &aString)
Definition: sim_value.cpp:419
std::string ModelLine(const SPICE_ITEM &aItem) const override
std::string ItemLine(const SPICE_ITEM &aItem) const override
virtual std::string ItemLine(const SPICE_ITEM &aItem) const
const SIM_MODEL & m_model
STL namespace.
SIM_MODEL::TYPE TYPE
Definition: sim_model.cpp:57
std::string description
Definition: sim_model.h:383
std::string value
Definition: sim_model.h:400
std::string modelName