2022-09-22 05:38:45 +00:00
|
|
|
/* This program source code file is part of KiCad, a free EDA CAD application.
|
2022-08-29 02:28:56 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2022 Mikolaj Wielgus
|
|
|
|
* Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 3
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* https://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 3 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sim/sim_model_switch.h>
|
|
|
|
|
2022-09-22 05:38:45 +00:00
|
|
|
#include <fmt/core.h>
|
2022-08-29 02:28:56 +00:00
|
|
|
|
2022-09-22 05:38:45 +00:00
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
std::string SPICE_GENERATOR_SWITCH::ItemLine( const SPICE_ITEM& aItem ) const
|
2022-09-14 07:19:25 +00:00
|
|
|
{
|
2022-09-22 05:38:45 +00:00
|
|
|
std::string result;
|
2022-09-14 07:19:25 +00:00
|
|
|
|
|
|
|
switch( m_model.GetType() )
|
2022-08-29 02:28:56 +00:00
|
|
|
{
|
2022-09-15 03:25:57 +00:00
|
|
|
case SIM_MODEL::TYPE::SW_V:
|
2022-09-14 07:19:25 +00:00
|
|
|
{
|
2022-10-10 23:23:00 +00:00
|
|
|
result = SPICE_GENERATOR::ItemLine( aItem );
|
2022-08-29 02:28:56 +00:00
|
|
|
break;
|
2022-09-14 07:19:25 +00:00
|
|
|
}
|
2022-08-29 02:28:56 +00:00
|
|
|
|
2022-09-15 03:25:57 +00:00
|
|
|
case SIM_MODEL::TYPE::SW_I:
|
2022-09-14 07:19:25 +00:00
|
|
|
{
|
2022-10-10 23:23:00 +00:00
|
|
|
std::string vsourceName = fmt::format( "V__{}", aItem.refName );
|
2022-09-14 07:19:25 +00:00
|
|
|
|
|
|
|
// Current switches measure input current through a voltage source.
|
2022-10-10 23:23:00 +00:00
|
|
|
result.append( fmt::format( "{0} {1} 0\n", aItem.pinNetNames[0], aItem.pinNetNames[1] ) );
|
|
|
|
|
|
|
|
SPICE_ITEM item = aItem;
|
|
|
|
item.modelName = fmt::format( "{0} {1}", vsourceName, aItem.modelName );
|
|
|
|
result.append( SPICE_GENERATOR::ItemLine( item ) );
|
2022-08-29 02:28:56 +00:00
|
|
|
break;
|
2022-09-14 07:19:25 +00:00
|
|
|
}
|
2022-08-29 02:28:56 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
wxFAIL_MSG( "Unhandled SIM_MODEL type in SIM_MODEL_SWITCH" );
|
|
|
|
break;
|
|
|
|
}
|
2022-08-30 07:00:52 +00:00
|
|
|
|
2022-09-14 07:19:25 +00:00
|
|
|
return result;
|
2022-08-29 02:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-22 05:38:45 +00:00
|
|
|
std::string SPICE_GENERATOR_SWITCH::ItemParams() const
|
2022-08-29 02:28:56 +00:00
|
|
|
{
|
2022-09-22 05:38:45 +00:00
|
|
|
std::string result;
|
2022-09-14 01:15:42 +00:00
|
|
|
|
2022-09-15 03:25:57 +00:00
|
|
|
for( const SIM_MODEL::PARAM& param : GetInstanceParams() )
|
2022-09-14 01:15:42 +00:00
|
|
|
{
|
|
|
|
// The only instance param is "ic", which is positional.
|
2022-09-22 05:38:45 +00:00
|
|
|
std::string value = param.value->ToSpiceString();
|
2022-09-14 01:15:42 +00:00
|
|
|
|
2022-09-22 05:38:45 +00:00
|
|
|
if( value != "none" )
|
|
|
|
result.append( value );
|
2022-09-14 01:15:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2022-08-29 02:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-15 03:25:57 +00:00
|
|
|
std::vector<std::reference_wrapper<const SIM_MODEL::PIN>> SPICE_GENERATOR_SWITCH::GetPins() const
|
2022-08-29 02:28:56 +00:00
|
|
|
{
|
2022-09-14 07:19:25 +00:00
|
|
|
switch( m_model.GetType() )
|
2022-08-29 02:28:56 +00:00
|
|
|
{
|
2022-09-15 03:25:57 +00:00
|
|
|
case SIM_MODEL::TYPE::SW_V:
|
2022-09-14 07:19:25 +00:00
|
|
|
return { m_model.GetPin( 2 ), m_model.GetPin( 3 ), m_model.GetPin( 0 ), m_model.GetPin( 1 ) };
|
2022-08-29 02:28:56 +00:00
|
|
|
|
2022-09-15 03:25:57 +00:00
|
|
|
case SIM_MODEL::TYPE::SW_I:
|
2022-09-14 07:19:25 +00:00
|
|
|
return { m_model.GetPin( 2 ), m_model.GetPin( 3 ) };
|
|
|
|
|
|
|
|
default:
|
|
|
|
wxFAIL_MSG( "Unhandled SIM_MODEL type in SIM_MODEL_SWITCH" );
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
2022-08-29 02:28:56 +00:00
|
|
|
|
|
|
|
|
2022-09-14 07:19:25 +00:00
|
|
|
SIM_MODEL_SWITCH::SIM_MODEL_SWITCH( TYPE aType ) :
|
2022-11-11 02:07:34 +00:00
|
|
|
SIM_MODEL( aType,
|
2022-11-30 11:40:50 +00:00
|
|
|
std::make_unique<SPICE_GENERATOR_SWITCH>( *this ) )
|
2022-09-14 07:19:25 +00:00
|
|
|
{
|
|
|
|
static std::vector<PARAM::INFO> vsw = makeSwVParamInfos();
|
|
|
|
static std::vector<PARAM::INFO> isw = makeSwIParamInfos();
|
|
|
|
|
|
|
|
switch( aType )
|
|
|
|
{
|
|
|
|
case TYPE::SW_V:
|
|
|
|
for( const PARAM::INFO& paramInfo : vsw )
|
|
|
|
AddParam( paramInfo );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE::SW_I:
|
|
|
|
for( const PARAM::INFO& paramInfo : isw )
|
|
|
|
AddParam( paramInfo );
|
2022-08-29 02:28:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
wxFAIL_MSG( "Unhandled SIM_MODEL type in SIM_MODEL_SWITCH" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const std::vector<SIM_MODEL::PARAM::INFO> SIM_MODEL_SWITCH::makeSwVParamInfos()
|
|
|
|
{
|
|
|
|
std::vector<PARAM::INFO> paramInfos;
|
|
|
|
PARAM::INFO paramInfo;
|
|
|
|
|
|
|
|
paramInfo.name = "thr";
|
|
|
|
paramInfo.type = SIM_VALUE::TYPE_FLOAT;
|
|
|
|
paramInfo.unit = "V";
|
|
|
|
paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
|
|
|
|
paramInfo.defaultValue = "0";
|
|
|
|
paramInfo.description = "Threshold voltage";
|
|
|
|
paramInfo.isSpiceInstanceParam = false;
|
|
|
|
paramInfo.spiceModelName = "vt";
|
2022-08-30 07:00:52 +00:00
|
|
|
paramInfo.enumValues = {};
|
2022-08-29 02:28:56 +00:00
|
|
|
paramInfos.push_back( paramInfo );
|
|
|
|
|
|
|
|
paramInfo.name = "his";
|
|
|
|
paramInfo.type = SIM_VALUE::TYPE_FLOAT;
|
|
|
|
paramInfo.unit = "V";
|
|
|
|
paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
|
|
|
|
paramInfo.defaultValue = "0";
|
|
|
|
paramInfo.description = "Hysteresis voltage";
|
|
|
|
paramInfo.isSpiceInstanceParam = false;
|
|
|
|
paramInfo.spiceModelName = "vh";
|
2022-08-30 07:00:52 +00:00
|
|
|
paramInfo.enumValues = {};
|
2022-08-29 02:28:56 +00:00
|
|
|
paramInfos.push_back( paramInfo );
|
|
|
|
|
|
|
|
paramInfo.name = "ron";
|
|
|
|
paramInfo.type = SIM_VALUE::TYPE_FLOAT;
|
|
|
|
paramInfo.unit = "Ω";
|
|
|
|
paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
|
|
|
|
paramInfo.defaultValue = "1";
|
|
|
|
paramInfo.description = "Resistance when open";
|
|
|
|
paramInfo.isSpiceInstanceParam = false;
|
|
|
|
paramInfo.spiceModelName = "";
|
2022-08-30 07:00:52 +00:00
|
|
|
paramInfo.enumValues = {};
|
2022-08-29 02:28:56 +00:00
|
|
|
paramInfos.push_back( paramInfo );
|
|
|
|
|
|
|
|
paramInfo.name = "roff";
|
|
|
|
paramInfo.type = SIM_VALUE::TYPE_FLOAT;
|
|
|
|
paramInfo.unit = "Ω";
|
|
|
|
paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
|
|
|
|
paramInfo.defaultValue = "1e+12";
|
|
|
|
paramInfo.description = "Resistance when closed";
|
|
|
|
paramInfo.isSpiceInstanceParam = false;
|
|
|
|
paramInfo.spiceModelName = "";
|
2022-08-30 07:00:52 +00:00
|
|
|
paramInfo.enumValues = {};
|
2022-08-29 02:28:56 +00:00
|
|
|
paramInfos.push_back( paramInfo );
|
|
|
|
|
|
|
|
paramInfo.name = "ic";
|
|
|
|
paramInfo.type = SIM_VALUE::TYPE_STRING;
|
|
|
|
paramInfo.unit = "";
|
|
|
|
paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
|
2022-11-30 11:40:50 +00:00
|
|
|
paramInfo.defaultValue = "none";
|
2022-08-30 07:00:52 +00:00
|
|
|
paramInfo.description = "Initial state";
|
2022-08-29 02:28:56 +00:00
|
|
|
paramInfo.isSpiceInstanceParam = true;
|
|
|
|
paramInfo.spiceModelName = "";
|
2022-08-30 07:00:52 +00:00
|
|
|
paramInfo.enumValues = { "none", "off", "on" };
|
2022-08-29 02:28:56 +00:00
|
|
|
paramInfos.push_back( paramInfo );
|
|
|
|
|
|
|
|
return paramInfos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const std::vector<SIM_MODEL::PARAM::INFO> SIM_MODEL_SWITCH::makeSwIParamInfos()
|
|
|
|
{
|
|
|
|
std::vector<PARAM::INFO> paramInfos;
|
|
|
|
PARAM::INFO paramInfo;
|
|
|
|
|
|
|
|
paramInfo.name = "thr";
|
|
|
|
paramInfo.type = SIM_VALUE::TYPE_FLOAT;
|
|
|
|
paramInfo.unit = "A";
|
|
|
|
paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
|
|
|
|
paramInfo.defaultValue = "0";
|
|
|
|
paramInfo.description = "Threshold current";
|
|
|
|
paramInfo.isSpiceInstanceParam = false;
|
|
|
|
paramInfo.spiceModelName = "it";
|
2022-08-30 07:00:52 +00:00
|
|
|
paramInfo.enumValues = {};
|
2022-08-29 02:28:56 +00:00
|
|
|
paramInfos.push_back( paramInfo );
|
|
|
|
|
|
|
|
paramInfo.name = "his";
|
|
|
|
paramInfo.type = SIM_VALUE::TYPE_FLOAT;
|
|
|
|
paramInfo.unit = "A";
|
|
|
|
paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
|
|
|
|
paramInfo.defaultValue = "0";
|
|
|
|
paramInfo.description = "Hysteresis current";
|
|
|
|
paramInfo.isSpiceInstanceParam = false;
|
|
|
|
paramInfo.spiceModelName = "ih";
|
2022-08-30 07:00:52 +00:00
|
|
|
paramInfo.enumValues = {};
|
2022-08-29 02:28:56 +00:00
|
|
|
paramInfos.push_back( paramInfo );
|
|
|
|
|
|
|
|
paramInfo.name = "ron";
|
|
|
|
paramInfo.type = SIM_VALUE::TYPE_FLOAT;
|
|
|
|
paramInfo.unit = "Ω";
|
|
|
|
paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
|
|
|
|
paramInfo.defaultValue = "1";
|
|
|
|
paramInfo.description = "Resistance when open";
|
|
|
|
paramInfo.isSpiceInstanceParam = false;
|
|
|
|
paramInfo.spiceModelName = "";
|
2022-08-30 07:00:52 +00:00
|
|
|
paramInfo.enumValues = {};
|
2022-08-29 02:28:56 +00:00
|
|
|
paramInfos.push_back( paramInfo );
|
|
|
|
|
|
|
|
paramInfo.name = "roff";
|
|
|
|
paramInfo.type = SIM_VALUE::TYPE_FLOAT;
|
|
|
|
paramInfo.unit = "Ω";
|
|
|
|
paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
|
|
|
|
paramInfo.defaultValue = "1e+12";
|
|
|
|
paramInfo.description = "Resistance when closed";
|
|
|
|
paramInfo.isSpiceInstanceParam = false;
|
|
|
|
paramInfo.spiceModelName = "";
|
2022-08-30 07:00:52 +00:00
|
|
|
paramInfo.enumValues = {};
|
2022-08-29 02:28:56 +00:00
|
|
|
paramInfos.push_back( paramInfo );
|
|
|
|
|
|
|
|
paramInfo.name = "ic";
|
|
|
|
paramInfo.type = SIM_VALUE::TYPE_STRING;
|
|
|
|
paramInfo.unit = "";
|
|
|
|
paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
|
|
|
|
paramInfo.defaultValue = "1";
|
2022-08-30 07:00:52 +00:00
|
|
|
paramInfo.description = "Initial state";
|
2022-08-29 02:28:56 +00:00
|
|
|
paramInfo.isSpiceInstanceParam = true;
|
|
|
|
paramInfo.spiceModelName = "";
|
2022-08-30 07:00:52 +00:00
|
|
|
paramInfo.enumValues = { "none", "off", "on" };
|
2022-08-29 02:28:56 +00:00
|
|
|
paramInfos.push_back( paramInfo );
|
|
|
|
|
|
|
|
return paramInfos;
|
|
|
|
}
|