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