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