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