KiCad PCB EDA Suite
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, l, g, c, len;
35
36 switch( m_model.GetType() )
37 {
38 case SIM_MODEL::TYPE::TLINE_Z0:
39 {
40 auto z0 = static_cast<const SIM_VALUE_FLOAT&>( *m_model.FindParam( "z0" )->value );
41 auto td = static_cast<const SIM_VALUE_FLOAT&>( *m_model.FindParam( "td" )->value );
42
43 if( !z0.HasValue() || !td.HasValue() )
44 return fmt::format( ".model {} LTRA()\n", aItem.modelName );
45
46 r = SIM_VALUE_FLOAT( 0 ).ToSpiceString();
47 l = ( td * z0 ).ToSpiceString();
48 g = SIM_VALUE_FLOAT( 0 ).ToSpiceString();
49 c = ( td / z0 ).ToSpiceString();
50 len = SIM_VALUE_FLOAT( 1 ).ToSpiceString();
51 break;
52 }
53
54 case SIM_MODEL::TYPE::TLINE_RLGC:
55 r = m_model.FindParam( "r" )->value->ToSpiceString();
56 l = m_model.FindParam( "l" )->value->ToSpiceString();
57 g = m_model.FindParam( "g" )->value->ToSpiceString();
58 c = m_model.FindParam( "c" )->value->ToSpiceString();
59 len = m_model.FindParam( "len" )->value->ToSpiceString();
60 break;
61
62 default:
63 wxFAIL_MSG( "Unhandled SIM_MODEL type in SIM_MODEL_TLINE" );
64 return "";
65 }
66
67 return fmt::format( ".model {} LTRA( r={} l={} g={} c={} len={} )\n",
68 aItem.modelName, r, l, g, c, len );
69}
70
71
73 SIM_MODEL( aType, std::make_unique<SPICE_GENERATOR_TLINE>( *this ) )
74{
75 static std::vector<PARAM::INFO> z0 = makeZ0ParamInfos();
76 static std::vector<PARAM::INFO> rlgc = makeRlgcParamInfos();
77
78 switch( aType )
79 {
80 case TYPE::TLINE_Z0:
81 for( const PARAM::INFO& paramInfo : z0 )
82 AddParam( paramInfo );
83 break;
84
85 case TYPE::TLINE_RLGC:
86 for( const PARAM::INFO& paramInfo : rlgc )
87 AddParam( paramInfo );
88 break;
89
90 default:
91 wxFAIL_MSG( "Unhandled SIM_MODEL type in SIM_MODEL_TLINE" );
92 break;
93 }
94}
95
96
97std::vector<PARAM::INFO> SIM_MODEL_TLINE::makeZ0ParamInfos()
98{
99 std::vector<PARAM::INFO> paramInfos;
100 PARAM::INFO paramInfo = {};
101
102 paramInfo.name = "z0";
103 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
104 paramInfo.unit = "Ω";
106 paramInfo.defaultValue = "";
107 paramInfo.description = "Characteristic impedance";
108 paramInfo.isSpiceInstanceParam = false;
109 paramInfo.isInstanceParam = true;
110 paramInfos.push_back( paramInfo );
111
112 paramInfo.name = "td";
113 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
114 paramInfo.unit = "s";
116 paramInfo.defaultValue = "";
117 paramInfo.description = "Transmission delay";
118 paramInfo.isSpiceInstanceParam = false;
119 paramInfo.isInstanceParam = true;
120 paramInfos.push_back( paramInfo );
121
122 return paramInfos;
123}
124
125
126std::vector<PARAM::INFO> SIM_MODEL_TLINE::makeRlgcParamInfos()
127{
128 std::vector<PARAM::INFO> paramInfos;
129 PARAM::INFO paramInfo = {};
130
131 paramInfo.name = "len";
132 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
133 paramInfo.unit = "m";
135 paramInfo.defaultValue = "";
136 paramInfo.description = "Length";
137 paramInfo.isSpiceInstanceParam = false;
138 paramInfo.isInstanceParam = true;
139 paramInfos.push_back( paramInfo );
140
141 paramInfo.name = "r";
142 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
143 paramInfo.unit = "Ω/m";
145 paramInfo.defaultValue = "0";
146 paramInfo.description = "Resistance per length";
147 paramInfo.isSpiceInstanceParam = false;
148 paramInfo.isInstanceParam = false;
149 paramInfos.push_back( paramInfo );
150
151 paramInfo.name = "l";
152 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
153 paramInfo.unit = "H/m";
155 paramInfo.defaultValue = "0";
156 paramInfo.description = "Inductance per length";
157 paramInfo.isSpiceInstanceParam = false;
158 paramInfo.isInstanceParam = false;
159 paramInfos.push_back( paramInfo );
160
161 paramInfo.name = "g";
162 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
163 paramInfo.unit = "1/(Ω m)";
165 paramInfo.defaultValue = "0";
166 paramInfo.description = "Conductance per length";
167 paramInfo.isSpiceInstanceParam = false;
168 paramInfo.isInstanceParam = false;
169 paramInfos.push_back( paramInfo );
170
171 paramInfo.name = "c";
172 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
173 paramInfo.unit = "C/m";
175 paramInfo.defaultValue = "0";
176 paramInfo.description = "Capacitance per length";
177 paramInfo.isSpiceInstanceParam = false;
178 paramInfo.isInstanceParam = false;
179 paramInfos.push_back( paramInfo );
180
181 return paramInfos;
182}
static std::vector< PARAM::INFO > makeRlgcParamInfos()
SIM_MODEL_TLINE(TYPE aType)
static std::vector< PARAM::INFO > makeZ0ParamInfos()
const PARAM * FindParam(const std::string &aParamName) const
Definition: sim_model.cpp:794
void AddParam(const PARAM::INFO &aInfo, bool aIsOtherVariant=false)
Definition: sim_model.cpp:714
TYPE GetType() const
Definition: sim_model.h:462
@ TYPE_FLOAT
Definition: sim_value.h:69
std::string ModelLine(const SPICE_ITEM &aItem) const override
const SIM_MODEL & m_model
Definition: bitmap.cpp:64
SIM_MODEL::TYPE TYPE
Definition: sim_model.cpp:54
SIM_MODEL::PARAM PARAM
SIM_VALUE_INST< double > SIM_VALUE_FLOAT
Definition: sim_value.h:150
SIM_VALUE::TYPE type
Definition: sim_model.h:332
std::string defaultValue
Definition: sim_model.h:336
std::string description
Definition: sim_model.h:338
std::unique_ptr< SIM_VALUE > value
Definition: sim_model.h:380
std::string modelName