KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sim_model_tline.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 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 <sim/sim_model_tline.h>
26
27#include <fmt/core.h>
28
30
31
32std::string SPICE_GENERATOR_TLINE::ModelLine( const SPICE_ITEM& aItem ) const
33{
34 std::string r="0", l="0", g="0", c="0", len="1";
35
36 switch( m_model.GetType() )
37 {
38 case SIM_MODEL::TYPE::TLINE_Z0:
39 {
40 double z0 = SIM_VALUE::ToDouble( m_model.FindParam( "z0" )->value );
41 double td = SIM_VALUE::ToDouble( m_model.FindParam( "td" )->value );
42
43 if( std::isnan( z0 ) || std::isnan( td ) )
44 return fmt::format( ".model {} LTRA()\n", aItem.modelName );
45
46 l = fmt::format( "{:g}", td * z0 );
47 c = fmt::format( "{:g}", td / z0 );
48 break;
49 }
50 case SIM_MODEL::TYPE::TLINE_RLGC:
51 {
52 if( m_model.FindParam( "r" ) )
54 if( m_model.FindParam( "l" ) )
56 if( m_model.FindParam( "g" ) )
58 if( m_model.FindParam( "c" ) )
60 if( m_model.FindParam( "len" ) )
61 len = SIM_VALUE::ToSpice( m_model.FindParam( "len" )->value );
62 break;
63 }
64 default:
65 wxFAIL_MSG( "Unhandled SIM_MODEL type in SIM_MODEL_TLINE" );
66 return "";
67 }
68
69 return fmt::format( ".model {} LTRA( r={} l={} g={} c={} len={} )\n",
70 aItem.modelName, r, l, g, c, len );
71}
72
73
75 SIM_MODEL( aType, std::make_unique<SPICE_GENERATOR_TLINE>( *this ) )
76{
77 static std::vector<PARAM::INFO> z0 = makeZ0ParamInfos();
78 static std::vector<PARAM::INFO> rlgc = makeRlgcParamInfos();
79
80 switch( aType )
81 {
82 case TYPE::TLINE_Z0:
83 for( const PARAM::INFO& paramInfo : z0 )
84 AddParam( paramInfo );
85 break;
86
87 case TYPE::TLINE_RLGC:
88 for( const PARAM::INFO& paramInfo : rlgc )
89 AddParam( paramInfo );
90 break;
91
92 default:
93 wxFAIL_MSG( "Unhandled SIM_MODEL type in SIM_MODEL_TLINE" );
94 break;
95 }
96}
97
98
99std::vector<PARAM::INFO> SIM_MODEL_TLINE::makeZ0ParamInfos()
100{
101 std::vector<PARAM::INFO> paramInfos;
102 PARAM::INFO paramInfo = {};
103
104 paramInfo.name = "z0";
105 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
106 paramInfo.unit = "Ω";
108 paramInfo.defaultValue = "";
109 paramInfo.description = "Characteristic impedance";
110 paramInfo.isSpiceInstanceParam = false;
111 paramInfo.isInstanceParam = true;
112 paramInfos.push_back( paramInfo );
113
114 paramInfo.name = "td";
115 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
116 paramInfo.unit = "s";
118 paramInfo.defaultValue = "";
119 paramInfo.description = "Transmission delay";
120 paramInfo.isSpiceInstanceParam = false;
121 paramInfo.isInstanceParam = true;
122 paramInfos.push_back( paramInfo );
123
124 return paramInfos;
125}
126
127
128std::vector<PARAM::INFO> SIM_MODEL_TLINE::makeRlgcParamInfos()
129{
130 std::vector<PARAM::INFO> paramInfos;
131 PARAM::INFO paramInfo = {};
132
133 paramInfo.name = "len";
134 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
135 paramInfo.unit = "m";
137 paramInfo.defaultValue = "";
138 paramInfo.description = "Length";
139 paramInfo.isSpiceInstanceParam = false;
140 paramInfo.isInstanceParam = true;
141 paramInfos.push_back( paramInfo );
142
143 paramInfo.name = "r";
144 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
145 paramInfo.unit = "Ω/m";
147 paramInfo.defaultValue = "0";
148 paramInfo.description = "Resistance per length";
149 paramInfo.isSpiceInstanceParam = false;
150 paramInfo.isInstanceParam = false;
151 paramInfos.push_back( paramInfo );
152
153 paramInfo.name = "l";
154 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
155 paramInfo.unit = "H/m";
157 paramInfo.defaultValue = "0";
158 paramInfo.description = "Inductance per length";
159 paramInfo.isSpiceInstanceParam = false;
160 paramInfo.isInstanceParam = false;
161 paramInfos.push_back( paramInfo );
162
163 paramInfo.name = "g";
164 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
165 paramInfo.unit = "1/(Ω m)";
167 paramInfo.defaultValue = "0";
168 paramInfo.description = "Conductance per length";
169 paramInfo.isSpiceInstanceParam = false;
170 paramInfo.isInstanceParam = false;
171 paramInfos.push_back( paramInfo );
172
173 paramInfo.name = "c";
174 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
175 paramInfo.unit = "F/m";
177 paramInfo.defaultValue = "0";
178 paramInfo.description = "Capacitance per length";
179 paramInfo.isSpiceInstanceParam = false;
180 paramInfo.isInstanceParam = false;
181 paramInfos.push_back( paramInfo );
182
183 return paramInfos;
184}
static std::vector< PARAM::INFO > makeRlgcParamInfos()
SIM_MODEL_TLINE(TYPE aType)
static std::vector< PARAM::INFO > makeZ0ParamInfos()
void AddParam(const PARAM::INFO &aInfo)
Definition: sim_model.cpp:720
const PARAM * FindParam(const std::string &aParamName) const
Definition: sim_model.cpp:808
TYPE GetType() const
Definition: sim_model.h:465
static std::string ToSpice(const std::string &aString)
Definition: sim_value.h:84
@ TYPE_FLOAT
Definition: sim_value.h:69
static double ToDouble(const std::string &aString, double aDefault=NAN)
Definition: sim_value.cpp:418
std::string ModelLine(const SPICE_ITEM &aItem) const override
const SIM_MODEL & m_model
STL namespace.
SIM_MODEL::TYPE TYPE
Definition: sim_model.cpp:53
SIM_VALUE::TYPE type
Definition: sim_model.h:380
std::string defaultValue
Definition: sim_model.h:383
std::string description
Definition: sim_model.h:384
std::string value
Definition: sim_model.h:401
std::string modelName