Sim: Create a common base class for models sourced from Spice libs

This commit is contained in:
Mikolaj Wielgus 2022-09-18 21:22:59 +02:00
parent 1566bd8b14
commit 0efc1149af
13 changed files with 469 additions and 389 deletions

View File

@ -287,6 +287,7 @@ set( EESCHEMA_SRCS
sim/sim_model_ngspice_data.cpp sim/sim_model_ngspice_data.cpp
sim/sim_model_raw_spice.cpp sim/sim_model_raw_spice.cpp
sim/sim_model_source.cpp sim/sim_model_source.cpp
sim/sim_model_spice.cpp
sim/sim_model_subckt.cpp sim/sim_model_subckt.cpp
sim/sim_model_switch.cpp sim/sim_model_switch.cpp
sim/sim_model_tline.cpp sim/sim_model_tline.cpp

View File

@ -23,6 +23,7 @@
*/ */
#include <sim/sim_library_spice.h> #include <sim/sim_library_spice.h>
#include <sim/sim_model_spice.h>
#include <sim/spice_grammar.h> #include <sim/spice_grammar.h>
#include <ki_exception.h> #include <ki_exception.h>
#include <locale_io.h> #include <locale_io.h>
@ -70,7 +71,7 @@ void SIM_LIBRARY_SPICE::ReadFile( const wxString& aFilePath )
{ {
if( node->is_type<SIM_LIBRARY_SPICE_PARSER::modelUnit>() ) if( node->is_type<SIM_LIBRARY_SPICE_PARSER::modelUnit>() )
{ {
m_models.push_back( SIM_MODEL::Create( node->string() ) ); m_models.push_back( SIM_MODEL_SPICE::Create( node->string() ) );
if( node->children.size() < 1 if( node->children.size() < 1
|| !node->children.at( 0 )->is_type<SIM_LIBRARY_SPICE_PARSER::modelName>() ) || !node->children.at( 0 )->is_type<SIM_LIBRARY_SPICE_PARSER::modelName>() )

View File

@ -34,7 +34,6 @@
#include <sim/sim_model_tline.h> #include <sim/sim_model_tline.h>
#include <sim/sim_model_xspice.h> #include <sim/sim_model_xspice.h>
#include <sim/spice_grammar.h>
#include <locale_io.h> #include <locale_io.h>
#include <lib_symbol.h> #include <lib_symbol.h>
#include <confirm.h> #include <confirm.h>
@ -68,20 +67,6 @@ namespace SIM_MODEL_PARSER
} }
namespace SIM_MODEL_SPICE_PARSER
{
using namespace SPICE_GRAMMAR;
template <typename Rule> struct spiceUnitSelector : std::false_type {};
template <> struct spiceUnitSelector<dotModel> : std::true_type {};
template <> struct spiceUnitSelector<modelName> : std::true_type {};
template <> struct spiceUnitSelector<dotModelType> : std::true_type {};
template <> struct spiceUnitSelector<param> : std::true_type {};
template <> struct spiceUnitSelector<paramValue> : std::true_type {};
template <> struct spiceUnitSelector<dotSubckt> : std::true_type {};
}
SIM_MODEL::DEVICE_INFO SIM_MODEL::DeviceTypeInfo( DEVICE_TYPE_ aDeviceType ) SIM_MODEL::DEVICE_INFO SIM_MODEL::DeviceTypeInfo( DEVICE_TYPE_ aDeviceType )
{ {
switch( aDeviceType ) switch( aDeviceType )
@ -239,7 +224,7 @@ SIM_MODEL::INFO SIM_MODEL::TypeInfo( TYPE aType )
case TYPE::SUBCKT: return { DEVICE_TYPE_::SUBCKT, "", "" }; case TYPE::SUBCKT: return { DEVICE_TYPE_::SUBCKT, "", "" };
case TYPE::XSPICE: return { DEVICE_TYPE_::XSPICE, "", "" }; case TYPE::XSPICE: return { DEVICE_TYPE_::XSPICE, "", "" };
case TYPE::SPICE: return { DEVICE_TYPE_::SPICE, "", "" }; case TYPE::RAWSPICE: return { DEVICE_TYPE_::SPICE, "", "" };
case TYPE::_ENUM_END: break; case TYPE::_ENUM_END: break;
} }
@ -370,7 +355,7 @@ SIM_MODEL::SPICE_INFO SIM_MODEL::SpiceInfo( TYPE aType )
case TYPE::XSPICE: return { "A" }; case TYPE::XSPICE: return { "A" };
case TYPE::NONE: case TYPE::NONE:
case TYPE::SPICE: case TYPE::RAWSPICE:
return {}; return {};
case TYPE::_ENUM_END: case TYPE::_ENUM_END:
@ -382,88 +367,6 @@ SIM_MODEL::SPICE_INFO SIM_MODEL::SpiceInfo( TYPE aType )
} }
TYPE SIM_MODEL::ReadTypeFromSpiceCode( const wxString& aSpiceCode )
{
tao::pegtl::string_input<> in( aSpiceCode.ToUTF8(), "Spice_Code" );
std::unique_ptr<tao::pegtl::parse_tree::node> root;
try
{
root = tao::pegtl::parse_tree::parse<SIM_MODEL_SPICE_PARSER::spiceUnitGrammar,
SIM_MODEL_SPICE_PARSER::spiceUnitSelector,
tao::pegtl::nothing,
SIM_MODEL_SPICE_PARSER::control>
( in );
}
catch( const tao::pegtl::parse_error& e )
{
wxLogDebug( "%s", e.what() );
return TYPE::NONE;
}
for( const auto& node : root->children )
{
if( node->is_type<SIM_MODEL_SPICE_PARSER::dotModel>() )
{
wxString paramName;
wxString typeString;
wxString level;
wxString version;
for( const auto& subnode : node->children )
{
if( subnode->is_type<SIM_MODEL_SPICE_PARSER::modelName>() )
{
// Do nothing.
}
else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::dotModelType>() )
{
typeString = subnode->string();
TYPE type = readTypeFromSpiceStrings( typeString );
if( type != TYPE::SPICE )
return type;
}
else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::param>() )
{
paramName = subnode->string();
}
else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::paramValue>() )
{
wxASSERT( paramName != "" );
if( paramName == "level" )
level = subnode->string();
else if( paramName == "version" )
version = subnode->string();
}
else
{
wxFAIL_MSG( "Unhandled parse tree subnode" );
return TYPE::NONE;
}
}
// Type was not determined from Spice type string alone, so now we take `level` and
// `version` variables into account too. This is suboptimal since we read the model
// twice this way, and moreover the code is now somewhat duplicated.
return readTypeFromSpiceStrings( typeString, level, version, false );
}
else if( node->is_type<SIM_MODEL_SPICE_PARSER::dotSubckt>() )
return TYPE::SUBCKT;
else
{
wxFAIL_MSG( "Unhandled parse tree node" );
return TYPE::NONE;
}
}
wxFAIL_MSG( "Could not derive type from Spice code" );
return TYPE::NONE;
}
template TYPE SIM_MODEL::ReadTypeFromFields( const std::vector<SCH_FIELD>& aFields ); template TYPE SIM_MODEL::ReadTypeFromFields( const std::vector<SCH_FIELD>& aFields );
template TYPE SIM_MODEL::ReadTypeFromFields( const std::vector<LIB_FIELD>& aFields ); template TYPE SIM_MODEL::ReadTypeFromFields( const std::vector<LIB_FIELD>& aFields );
@ -606,7 +509,7 @@ TYPE SIM_MODEL::InferTypeFromRefAndValue( const wxString& aRef, const wxString&
} }
} }
} }
catch( const tao::pegtl::parse_error& ) catch( const tao::pegtl::parse_error& e )
{ {
} }
@ -628,7 +531,7 @@ TYPE SIM_MODEL::InferTypeFromLegacyFields( const std::vector<T>& aFields )
|| GetFieldValue( &aFields, SIM_MODEL_RAW_SPICE::LEGACY_ENABLED_FIELD ) != "" || GetFieldValue( &aFields, SIM_MODEL_RAW_SPICE::LEGACY_ENABLED_FIELD ) != ""
|| GetFieldValue( &aFields, SIM_MODEL_RAW_SPICE::LEGACY_LIB_FIELD ) != "" ) || GetFieldValue( &aFields, SIM_MODEL_RAW_SPICE::LEGACY_LIB_FIELD ) != "" )
{ {
return TYPE::SPICE; return TYPE::RAWSPICE;
} }
else else
return TYPE::NONE; return TYPE::NONE;
@ -696,7 +599,7 @@ void SIM_MODEL::WriteDataLibFields( std::vector<LIB_FIELD>& aFields ) const
std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( TYPE aType, unsigned aSymbolPinCount ) std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( TYPE aType, unsigned aSymbolPinCount )
{ {
std::unique_ptr<SIM_MODEL> model = create( aType ); std::unique_ptr<SIM_MODEL> model = Create( aType );
// Passing nullptr to ReadDataFields will make it act as if all fields were empty. // Passing nullptr to ReadDataFields will make it act as if all fields were empty.
model->ReadDataFields( aSymbolPinCount, static_cast<const std::vector<void>*>( nullptr ) ); model->ReadDataFields( aSymbolPinCount, static_cast<const std::vector<void>*>( nullptr ) );
@ -704,32 +607,10 @@ std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( TYPE aType, unsigned aSymbolPinCou
} }
std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( const wxString& aSpiceCode )
{
std::unique_ptr<SIM_MODEL> model = create( ReadTypeFromSpiceCode( aSpiceCode ) );
try
{
model->ReadSpiceCode( aSpiceCode );
}
catch( const IO_ERROR& e )
{
DisplayErrorMessage( nullptr, e.What() );
// Demote to raw Spice element and try again.
std::unique_ptr<SIM_MODEL> rawSpiceModel = create( TYPE::SPICE );
rawSpiceModel->ReadSpiceCode( aSpiceCode );
return rawSpiceModel;
}
return model;
}
std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( const SIM_MODEL& aBaseModel, std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( const SIM_MODEL& aBaseModel,
unsigned aSymbolPinCount ) unsigned aSymbolPinCount )
{ {
std::unique_ptr<SIM_MODEL> model = create( aBaseModel.GetType() ); std::unique_ptr<SIM_MODEL> model = Create( aBaseModel.GetType() );
model->SetBaseModel( aBaseModel ); model->SetBaseModel( aBaseModel );
model->ReadDataFields( aSymbolPinCount, static_cast<const std::vector<void>*>( nullptr ) ); model->ReadDataFields( aSymbolPinCount, static_cast<const std::vector<void>*>( nullptr ) );
@ -741,7 +622,7 @@ template <typename T>
std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( const SIM_MODEL& aBaseModel, unsigned aSymbolPinCount, std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( const SIM_MODEL& aBaseModel, unsigned aSymbolPinCount,
const std::vector<T>& aFields ) const std::vector<T>& aFields )
{ {
std::unique_ptr<SIM_MODEL> model = create( aBaseModel.GetType() ); std::unique_ptr<SIM_MODEL> model = Create( aBaseModel.GetType() );
model->SetBaseModel( aBaseModel ); model->SetBaseModel( aBaseModel );
model->ReadDataFields( aSymbolPinCount, &aFields ); model->ReadDataFields( aSymbolPinCount, &aFields );
@ -761,7 +642,7 @@ template <typename T>
std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( unsigned aSymbolPinCount, std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( unsigned aSymbolPinCount,
const std::vector<T>& aFields ) const std::vector<T>& aFields )
{ {
std::unique_ptr<SIM_MODEL> model = SIM_MODEL::create( ReadTypeFromFields( aFields ) ); std::unique_ptr<SIM_MODEL> model = SIM_MODEL::Create( ReadTypeFromFields( aFields ) );
model->ReadDataFields( aSymbolPinCount, &aFields ); model->ReadDataFields( aSymbolPinCount, &aFields );
return model; return model;
@ -844,75 +725,6 @@ void SIM_MODEL::SetFieldValue( std::vector<T>& aFields, const wxString& aFieldNa
SIM_MODEL::~SIM_MODEL() = default; SIM_MODEL::~SIM_MODEL() = default;
void SIM_MODEL::ReadSpiceCode( const wxString& aSpiceCode )
{
// The default behavior is to treat the Spice param=value pairs as the model parameters and
// values (for many models the correspondence is not exact, so this function is overridden).
tao::pegtl::string_input<> in( aSpiceCode.ToUTF8(), "Spice_Code" );
std::unique_ptr<tao::pegtl::parse_tree::node> root;
try
{
root = tao::pegtl::parse_tree::parse<SIM_MODEL_SPICE_PARSER::spiceUnitGrammar,
SIM_MODEL_SPICE_PARSER::spiceUnitSelector,
tao::pegtl::nothing,
SIM_MODEL_SPICE_PARSER::control>
( in );
}
catch( tao::pegtl::parse_error& e )
{
THROW_IO_ERROR( e.what() );
}
for( const auto& node : root->children )
{
if( node->is_type<SIM_MODEL_SPICE_PARSER::dotModel>() )
{
wxString paramName = "";
for( const auto& subnode : node->children )
{
if( subnode->is_type<SIM_MODEL_SPICE_PARSER::modelName>() )
{
// Do nothing.
}
else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::dotModelType>() )
{
// Do nothing.
}
else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::param>() )
{
paramName = subnode->string();
}
else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::paramValue>() )
{
wxASSERT( !paramName.IsEmpty() );
if( !SetParamFromSpiceCode( paramName, subnode->string() ) )
{
THROW_IO_ERROR( wxString::Format(
_( "Failed to set parameter '%s' to '%s'" ),
paramName,
subnode->string() ) );
}
}
else
{
wxFAIL_MSG( "Unhandled parse tree subnode" );
}
}
}
else
{
wxFAIL_MSG( "Unhandled parse tree node" );
}
}
m_spiceCode = aSpiceCode;
}
void SIM_MODEL::AddPin( const PIN& aPin ) void SIM_MODEL::AddPin( const PIN& aPin )
{ {
m_pins.push_back( aPin ); m_pins.push_back( aPin );
@ -1003,10 +815,6 @@ const SIM_MODEL::PARAM& SIM_MODEL::GetBaseParam( unsigned aParamIndex ) const
bool SIM_MODEL::SetParamValue( unsigned aParamIndex, const wxString& aValue, bool SIM_MODEL::SetParamValue( unsigned aParamIndex, const wxString& aValue,
SIM_VALUE_GRAMMAR::NOTATION aNotation ) SIM_VALUE_GRAMMAR::NOTATION aNotation )
{ {
// Models sourced from a library are immutable.
if( m_spiceCode != "" )
return false;
return m_params.at( aParamIndex ).value->FromString( aValue.ToStdString(), aNotation ); return m_params.at( aParamIndex ).value->FromString( aValue.ToStdString(), aNotation );
} }
@ -1065,6 +873,78 @@ bool SIM_MODEL::HasSpiceNonInstanceOverrides() const
} }
std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( TYPE aType )
{
switch( aType )
{
case TYPE::R:
case TYPE::C:
case TYPE::L:
return std::make_unique<SIM_MODEL_IDEAL>( aType );
case TYPE::L_MUTUAL:
return std::make_unique<SIM_MODEL_MUTUAL_INDUCTOR>();
case TYPE::R_BEHAVIORAL:
case TYPE::C_BEHAVIORAL:
case TYPE::L_BEHAVIORAL:
case TYPE::V_BEHAVIORAL:
case TYPE::I_BEHAVIORAL:
return std::make_unique<SIM_MODEL_BEHAVIORAL>( aType );
case TYPE::TLINE_Z0:
case TYPE::TLINE_RLGC:
return std::make_unique<SIM_MODEL_TLINE>( aType );
case TYPE::SW_V:
case TYPE::SW_I:
return std::make_unique<SIM_MODEL_SWITCH>( aType );
case TYPE::V:
case TYPE::I:
case TYPE::V_SIN:
case TYPE::I_SIN:
case TYPE::V_PULSE:
case TYPE::I_PULSE:
case TYPE::V_EXP:
case TYPE::I_EXP:
/*case TYPE::V_SFAM:
case TYPE::I_SFAM:
case TYPE::V_SFFM:
case TYPE::I_SFFM:*/
case TYPE::V_PWL:
case TYPE::I_PWL:
case TYPE::V_WHITENOISE:
case TYPE::I_WHITENOISE:
case TYPE::V_PINKNOISE:
case TYPE::I_PINKNOISE:
case TYPE::V_BURSTNOISE:
case TYPE::I_BURSTNOISE:
case TYPE::V_RANDUNIFORM:
case TYPE::I_RANDUNIFORM:
case TYPE::V_RANDNORMAL:
case TYPE::I_RANDNORMAL:
case TYPE::V_RANDEXP:
case TYPE::I_RANDEXP:
//case TYPE::V_RANDPOISSON:
//case TYPE::I_RANDPOISSON:
return std::make_unique<SIM_MODEL_SOURCE>( aType );
case TYPE::SUBCKT:
return std::make_unique<SIM_MODEL_SUBCKT>( aType );
case TYPE::XSPICE:
return std::make_unique<SIM_MODEL_XSPICE>( aType );
case TYPE::RAWSPICE:
return std::make_unique<SIM_MODEL_RAW_SPICE>();
default:
return std::make_unique<SIM_MODEL_NGSPICE>( aType );
}
}
SIM_MODEL::SIM_MODEL( TYPE aType ) : SIM_MODEL::SIM_MODEL( TYPE aType ) :
SIM_MODEL( aType, std::make_unique<SPICE_GENERATOR>( *this ) ) SIM_MODEL( aType, std::make_unique<SPICE_GENERATOR>( *this ) )
{ {
@ -1270,119 +1150,6 @@ void SIM_MODEL::ParseDisabledField( const wxString& aDisabledField )
} }
bool SIM_MODEL::SetParamFromSpiceCode( const wxString& aParamName, const wxString& aParamValue,
SIM_VALUE_GRAMMAR::NOTATION aNotation )
{
return SetParamValue( aParamName, aParamValue, aNotation );
}
std::unique_ptr<SIM_MODEL> SIM_MODEL::create( TYPE aType )
{
switch( aType )
{
case TYPE::R:
case TYPE::C:
case TYPE::L:
return std::make_unique<SIM_MODEL_IDEAL>( aType );
case TYPE::L_MUTUAL:
return std::make_unique<SIM_MODEL_MUTUAL_INDUCTOR>();
case TYPE::R_BEHAVIORAL:
case TYPE::C_BEHAVIORAL:
case TYPE::L_BEHAVIORAL:
case TYPE::V_BEHAVIORAL:
case TYPE::I_BEHAVIORAL:
return std::make_unique<SIM_MODEL_BEHAVIORAL>( aType );
case TYPE::TLINE_Z0:
case TYPE::TLINE_RLGC:
return std::make_unique<SIM_MODEL_TLINE>( aType );
case TYPE::SW_V:
case TYPE::SW_I:
return std::make_unique<SIM_MODEL_SWITCH>( aType );
case TYPE::V:
case TYPE::I:
case TYPE::V_SIN:
case TYPE::I_SIN:
case TYPE::V_PULSE:
case TYPE::I_PULSE:
case TYPE::V_EXP:
case TYPE::I_EXP:
/*case TYPE::V_SFAM:
case TYPE::I_SFAM:
case TYPE::V_SFFM:
case TYPE::I_SFFM:*/
case TYPE::V_PWL:
case TYPE::I_PWL:
case TYPE::V_WHITENOISE:
case TYPE::I_WHITENOISE:
case TYPE::V_PINKNOISE:
case TYPE::I_PINKNOISE:
case TYPE::V_BURSTNOISE:
case TYPE::I_BURSTNOISE:
case TYPE::V_RANDUNIFORM:
case TYPE::I_RANDUNIFORM:
case TYPE::V_RANDNORMAL:
case TYPE::I_RANDNORMAL:
case TYPE::V_RANDEXP:
case TYPE::I_RANDEXP:
//case TYPE::V_RANDPOISSON:
//case TYPE::I_RANDPOISSON:
return std::make_unique<SIM_MODEL_SOURCE>( aType );
case TYPE::SUBCKT:
return std::make_unique<SIM_MODEL_SUBCKT>( aType );
case TYPE::XSPICE:
return std::make_unique<SIM_MODEL_XSPICE>( aType );
case TYPE::SPICE:
return std::make_unique<SIM_MODEL_RAW_SPICE>( aType );
default:
return std::make_unique<SIM_MODEL_NGSPICE>( aType );
}
}
TYPE SIM_MODEL::readTypeFromSpiceStrings( const wxString& aTypeString,
const wxString& aLevel,
const wxString& aVersion,
bool aSkipDefaultLevel )
{
std::unique_ptr<SIM_VALUE> readLevel = SIM_VALUE::Create( SIM_VALUE::TYPE_INT,
aLevel.ToStdString() );
for( TYPE type : TYPE_ITERATOR() )
{
wxString typePrefix = SpiceInfo( type ).modelType;
wxString level = SpiceInfo( type ).level;
wxString version = SpiceInfo( type ).version;
bool isDefaultLevel = SpiceInfo( type ).isDefaultLevel;
if( typePrefix == "" )
continue;
// Check if `aTypeString` starts with `typePrefix`.
if( aTypeString.Upper().StartsWith( typePrefix )
&& ( level == readLevel->ToString()
|| ( !aSkipDefaultLevel && isDefaultLevel && aLevel == "" ) )
&& version == aVersion )
{
return type;
}
}
// If the type string is not recognized, demote to a raw Spice element. This way the user won't
// have an error if there is a type KiCad does not recognize.
return TYPE::SPICE;
}
template <typename T> template <typename T>
void SIM_MODEL::doReadDataFields( unsigned aSymbolPinCount, const std::vector<T>* aFields ) void SIM_MODEL::doReadDataFields( unsigned aSymbolPinCount, const std::vector<T>* aFields )
{ {

View File

@ -296,7 +296,7 @@ public:
SUBCKT, SUBCKT,
XSPICE, XSPICE,
SPICE RAWSPICE
) )
struct INFO struct INFO
@ -427,8 +427,6 @@ public:
static SPICE_INFO SpiceInfo( TYPE aType ); static SPICE_INFO SpiceInfo( TYPE aType );
static TYPE ReadTypeFromSpiceCode( const wxString& aSpiceCode );
template <typename T> template <typename T>
static TYPE ReadTypeFromFields( const std::vector<T>& aFields ); static TYPE ReadTypeFromFields( const std::vector<T>& aFields );
@ -438,8 +436,7 @@ public:
static TYPE InferTypeFromLegacyFields( const std::vector<T>& aFields ); static TYPE InferTypeFromLegacyFields( const std::vector<T>& aFields );
static std::unique_ptr<SIM_MODEL> Create( TYPE aType, unsigned aSymbolPinCount = 0 ); static std::unique_ptr<SIM_MODEL> Create( TYPE aType, unsigned aSymbolPinCount );
static std::unique_ptr<SIM_MODEL> Create( const wxString& aSpiceCode );
static std::unique_ptr<SIM_MODEL> Create( const SIM_MODEL& aBaseModel, static std::unique_ptr<SIM_MODEL> Create( const SIM_MODEL& aBaseModel,
unsigned aSymbolPinCount ); unsigned aSymbolPinCount );
@ -469,8 +466,6 @@ public:
SIM_MODEL( SIM_MODEL&& aOther ) = default; SIM_MODEL( SIM_MODEL&& aOther ) = default;
SIM_MODEL& operator=(SIM_MODEL&& aOther ) = delete; SIM_MODEL& operator=(SIM_MODEL&& aOther ) = delete;
virtual void ReadSpiceCode( const wxString& aSpiceCode );
template <typename T> template <typename T>
void ReadDataFields( unsigned aSymbolPinCount, const std::vector<T>* aFields ); void ReadDataFields( unsigned aSymbolPinCount, const std::vector<T>* aFields );
@ -543,6 +538,8 @@ public:
bool IsEnabled() const { return m_isEnabled; } bool IsEnabled() const { return m_isEnabled; }
protected: protected:
static std::unique_ptr<SIM_MODEL> Create( TYPE aType );
SIM_MODEL( TYPE aType ); SIM_MODEL( TYPE aType );
SIM_MODEL( TYPE aType, std::unique_ptr<SPICE_GENERATOR> aSpiceGenerator ); SIM_MODEL( TYPE aType, std::unique_ptr<SPICE_GENERATOR> aSpiceGenerator );
@ -559,19 +556,12 @@ protected:
void ParsePinsField( unsigned aSymbolPinCount, const wxString& aPinsField ); void ParsePinsField( unsigned aSymbolPinCount, const wxString& aPinsField );
void ParseDisabledField( const wxString& aDisabledField ); void ParseDisabledField( const wxString& aDisabledField );
virtual bool SetParamFromSpiceCode( const wxString& aParamName, const wxString& aParamValue,
SIM_VALUE_GRAMMAR::NOTATION aNotation
= SIM_VALUE_GRAMMAR::NOTATION::SPICE );
template <typename T> template <typename T>
void InferredReadDataFields( unsigned aSymbolPinCount, const std::vector<T>* aFields, void InferredReadDataFields( unsigned aSymbolPinCount, const std::vector<T>* aFields,
bool aAllowOnlyFirstValue = false, bool aAllowOnlyFirstValue = false,
bool aAllowParamValuePairs = true ); bool aAllowParamValuePairs = true );
wxString m_spiceCode;
private: private:
static std::unique_ptr<SIM_MODEL> create( TYPE aType );
static TYPE readTypeFromSpiceStrings( const wxString& aTypeString, static TYPE readTypeFromSpiceStrings( const wxString& aTypeString,
const wxString& aLevel = "", const wxString& aLevel = "",
const wxString& aVersion = "", const wxString& aVersion = "",

View File

@ -61,8 +61,8 @@ std::vector<wxString> SPICE_GENERATOR_NGSPICE::CurrentNames( const wxString& aRe
} }
SIM_MODEL_NGSPICE::SIM_MODEL_NGSPICE( TYPE aType ) SIM_MODEL_NGSPICE::SIM_MODEL_NGSPICE( TYPE aType ) :
: SIM_MODEL( aType, std::make_unique<SPICE_GENERATOR_NGSPICE>( *this ) ) SIM_MODEL_SPICE( aType, std::make_unique<SPICE_GENERATOR_NGSPICE>( *this ) )
{ {
const MODEL_INFO& modelInfo = ModelInfo( getModelType() ); const MODEL_INFO& modelInfo = ModelInfo( getModelType() );

View File

@ -25,20 +25,20 @@
#ifndef SIM_MODEL_NGSPICE_H #ifndef SIM_MODEL_NGSPICE_H
#define SIM_MODEL_NGSPICE_H #define SIM_MODEL_NGSPICE_H
#include <sim/sim_model.h> #include <sim/sim_model_spice.h>
#include <sim/spice_generator.h> #include <sim/spice_generator.h>
class SPICE_GENERATOR_NGSPICE : public SPICE_GENERATOR class SPICE_GENERATOR_NGSPICE : public SPICE_GENERATOR_SPICE
{ {
public: public:
using SPICE_GENERATOR::SPICE_GENERATOR; using SPICE_GENERATOR_SPICE::SPICE_GENERATOR_SPICE;
std::vector<wxString> CurrentNames( const wxString& aRefName ) const override; std::vector<wxString> CurrentNames( const wxString& aRefName ) const override;
}; };
class SIM_MODEL_NGSPICE : public SIM_MODEL class SIM_MODEL_NGSPICE : public SIM_MODEL_SPICE
{ {
public: public:
friend struct MODEL_INFO_MAP; friend struct MODEL_INFO_MAP;

View File

@ -114,8 +114,8 @@ wxString SPICE_GENERATOR_RAW_SPICE::Preview( const wxString& aModelName ) const
} }
SIM_MODEL_RAW_SPICE::SIM_MODEL_RAW_SPICE( TYPE aType ) : SIM_MODEL_RAW_SPICE::SIM_MODEL_RAW_SPICE() :
SIM_MODEL( aType, std::make_unique<SPICE_GENERATOR_RAW_SPICE>( *this ) ) SIM_MODEL( TYPE::RAWSPICE, std::make_unique<SPICE_GENERATOR_RAW_SPICE>( *this ) )
{ {
static std::vector<PARAM::INFO> paramInfos = makeParamInfos(); static std::vector<PARAM::INFO> paramInfos = makeParamInfos();
@ -166,34 +166,6 @@ void SIM_MODEL_RAW_SPICE::CreatePins( unsigned aSymbolPinCount )
} }
bool SIM_MODEL_RAW_SPICE::SetParamFromSpiceCode( const wxString& aParamName,
const wxString& aParamValue,
SIM_VALUE_GRAMMAR::NOTATION aNotation )
{
int paramIndex = 0;
for(; paramIndex < GetParamCount(); ++paramIndex )
{
if( GetParam( paramIndex ).info.name == aParamName.Lower() )
break;
}
if( paramIndex == GetParamCount() )
{
// No parameter with this name found. Create a new one.
std::unique_ptr<PARAM::INFO> paramInfo = std::make_unique<PARAM::INFO>();
paramInfo->name = aParamName.Lower();
paramInfo->type = SIM_VALUE::TYPE_STRING;
m_paramInfos.push_back( std::move( paramInfo ) );
AddParam( *m_paramInfos.back() );
}
return GetParam( paramIndex ).value->FromString( aParamValue.ToStdString(), aNotation );
}
std::vector<SIM_MODEL::PARAM::INFO> SIM_MODEL_RAW_SPICE::makeParamInfos() std::vector<SIM_MODEL::PARAM::INFO> SIM_MODEL_RAW_SPICE::makeParamInfos()
{ {
std::vector<PARAM::INFO> paramInfos; std::vector<PARAM::INFO> paramInfos;

View File

@ -25,7 +25,7 @@
#ifndef SIM_MODEL_RAW_SPICE_H #ifndef SIM_MODEL_RAW_SPICE_H
#define SIM_MODEL_RAW_SPICE_H #define SIM_MODEL_RAW_SPICE_H
#include <sim/sim_model.h> #include <sim/sim_model_spice.h>
#include <sim/spice_generator.h> #include <sim/spice_generator.h>
@ -51,6 +51,8 @@ public:
class SIM_MODEL_RAW_SPICE : public SIM_MODEL class SIM_MODEL_RAW_SPICE : public SIM_MODEL
{ {
public: public:
friend class SPICE_GENERATOR_RAW_SPICE;
DEFINE_ENUM_CLASS_WITH_ITERATOR( SPICE_PARAM, DEFINE_ENUM_CLASS_WITH_ITERATOR( SPICE_PARAM,
TYPE, TYPE,
MODEL, MODEL,
@ -64,7 +66,7 @@ public:
static constexpr auto LEGACY_LIB_FIELD = "Spice_Lib_File"; static constexpr auto LEGACY_LIB_FIELD = "Spice_Lib_File";
SIM_MODEL_RAW_SPICE( TYPE aType ); SIM_MODEL_RAW_SPICE();
//bool ReadSpiceCode( const std::string& aSpiceCode ) override; //bool ReadSpiceCode( const std::string& aSpiceCode ) override;
void ReadDataSchFields( unsigned aSymbolPinCount, const std::vector<SCH_FIELD>* aFields ) override; void ReadDataSchFields( unsigned aSymbolPinCount, const std::vector<SCH_FIELD>* aFields ) override;
@ -76,11 +78,6 @@ public:
protected: protected:
void CreatePins( unsigned aSymbolPinCount ) override; void CreatePins( unsigned aSymbolPinCount ) override;
bool SetParamFromSpiceCode( const wxString& aParamName, const wxString& aParamValue,
SIM_VALUE_GRAMMAR::NOTATION aNotation
= SIM_VALUE_GRAMMAR::NOTATION::SPICE ) override;
private: private:
static std::vector<PARAM::INFO> makeParamInfos(); static std::vector<PARAM::INFO> makeParamInfos();

View File

@ -0,0 +1,288 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* 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_spice.h>
#include <sim/sim_model_raw_spice.h>
#include <sim/spice_grammar.h>
#include <confirm.h>
#include <pegtl.hpp>
#include <pegtl/contrib/parse_tree.hpp>
namespace SIM_MODEL_SPICE_PARSER
{
using namespace SPICE_GRAMMAR;
template <typename Rule> struct spiceUnitSelector : std::false_type {};
template <> struct spiceUnitSelector<dotModel> : std::true_type {};
template <> struct spiceUnitSelector<modelName> : std::true_type {};
template <> struct spiceUnitSelector<dotModelType> : std::true_type {};
template <> struct spiceUnitSelector<param> : std::true_type {};
template <> struct spiceUnitSelector<paramValue> : std::true_type {};
template <> struct spiceUnitSelector<dotSubckt> : std::true_type {};
}
wxString SPICE_GENERATOR_SPICE::Preview( const wxString& aModelName ) const
{
wxString spiceCode = ModelLine( aModelName );
if( spiceCode == "" )
spiceCode = static_cast<const SIM_MODEL_SPICE&>( m_model ).m_spiceCode;
if( spiceCode == "" && m_model.GetBaseModel() )
spiceCode = static_cast<const SIM_MODEL_SPICE*>( m_model.GetBaseModel() )->m_spiceCode;
wxString itemLine = ItemLine( "", aModelName );
if( spiceCode != "" )
spiceCode << "\n";
spiceCode << itemLine;
return spiceCode.Trim();
}
SIM_MODEL::TYPE SIM_MODEL_SPICE::ReadTypeFromSpiceCode( const wxString& aSpiceCode )
{
tao::pegtl::string_input<> in( aSpiceCode.ToUTF8(), "Spice_Code" );
std::unique_ptr<tao::pegtl::parse_tree::node> root;
try
{
root = tao::pegtl::parse_tree::parse<SIM_MODEL_SPICE_PARSER::spiceUnitGrammar,
SIM_MODEL_SPICE_PARSER::spiceUnitSelector,
tao::pegtl::nothing,
SIM_MODEL_SPICE_PARSER::control>
( in );
}
catch( const tao::pegtl::parse_error& e )
{
wxLogDebug( "%s", e.what() );
return TYPE::NONE;
}
for( const auto& node : root->children )
{
if( node->is_type<SIM_MODEL_SPICE_PARSER::dotModel>() )
{
wxString paramName;
wxString typeString;
wxString level;
wxString version;
for( const auto& subnode : node->children )
{
if( subnode->is_type<SIM_MODEL_SPICE_PARSER::modelName>() )
{
// Do nothing.
}
else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::dotModelType>() )
{
typeString = subnode->string();
TYPE type = readTypeFromSpiceStrings( typeString );
if( type != TYPE::RAWSPICE )
return type;
}
else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::param>() )
{
paramName = subnode->string();
}
else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::paramValue>() )
{
wxASSERT( paramName != "" );
if( paramName == "level" )
level = subnode->string();
else if( paramName == "version" )
version = subnode->string();
}
else
{
wxFAIL_MSG( "Unhandled parse tree subnode" );
return TYPE::NONE;
}
}
// Type was not determined from Spice type string alone, so now we take `level` and
// `version` variables into account too. This is suboptimal since we read the model
// twice this way, and moreover the code is now somewhat duplicated.
return readTypeFromSpiceStrings( typeString, level, version, false );
}
else if( node->is_type<SIM_MODEL_SPICE_PARSER::dotSubckt>() )
return TYPE::SUBCKT;
else
{
wxFAIL_MSG( "Unhandled parse tree node" );
return TYPE::NONE;
}
}
wxFAIL_MSG( "Could not derive type from Spice code" );
return TYPE::NONE;
}
std::unique_ptr<SIM_MODEL_SPICE> SIM_MODEL_SPICE::Create( const wxString& aSpiceCode )
{
auto model = static_cast<SIM_MODEL_SPICE*>(
SIM_MODEL::Create( ReadTypeFromSpiceCode( aSpiceCode ) ).release() );
try
{
model->ReadSpiceCode( aSpiceCode );
}
catch( const IO_ERROR& e )
{
DisplayErrorMessage( nullptr, e.What() );
}
return std::unique_ptr<SIM_MODEL_SPICE>( model );
}
void SIM_MODEL_SPICE::ReadSpiceCode( const wxString& aSpiceCode )
{
// The default behavior is to treat the Spice param=value pairs as the model parameters and
// values (for many models the correspondence is not exact, so this function is overridden).
tao::pegtl::string_input<> in( aSpiceCode.ToUTF8(), "Spice_Code" );
std::unique_ptr<tao::pegtl::parse_tree::node> root;
try
{
root = tao::pegtl::parse_tree::parse<SIM_MODEL_SPICE_PARSER::spiceUnitGrammar,
SIM_MODEL_SPICE_PARSER::spiceUnitSelector,
tao::pegtl::nothing,
SIM_MODEL_SPICE_PARSER::control>
( in );
}
catch( tao::pegtl::parse_error& e )
{
THROW_IO_ERROR( e.what() );
}
for( const auto& node : root->children )
{
if( node->is_type<SIM_MODEL_SPICE_PARSER::dotModel>() )
{
wxString paramName = "";
for( const auto& subnode : node->children )
{
if( subnode->is_type<SIM_MODEL_SPICE_PARSER::modelName>() )
{
// Do nothing.
}
else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::dotModelType>() )
{
// Do nothing.
}
else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::param>() )
{
paramName = subnode->string();
}
else if( subnode->is_type<SIM_MODEL_SPICE_PARSER::paramValue>() )
{
wxASSERT( !paramName.IsEmpty() );
if( !SetParamFromSpiceCode( paramName, subnode->string() ) )
{
THROW_IO_ERROR( wxString::Format(
_( "Failed to set parameter '%s' to '%s'" ),
paramName,
subnode->string() ) );
}
}
else
{
wxFAIL_MSG( "Unhandled parse tree subnode" );
}
}
}
else
{
wxFAIL_MSG( "Unhandled parse tree node" );
}
}
m_spiceCode = aSpiceCode;
}
bool SIM_MODEL_SPICE::SetParamValue( unsigned aParamIndex, const wxString& aParamValue,
SIM_VALUE_GRAMMAR::NOTATION aNotation )
{
// Models sourced from a library are immutable.
if( m_spiceCode != "" )
return false;
return SIM_MODEL::SetParamValue( aParamIndex, aParamValue, aNotation );
}
bool SIM_MODEL_SPICE::SetParamFromSpiceCode( const wxString& aParamName,
const wxString& aParamValue,
SIM_VALUE_GRAMMAR::NOTATION aNotation )
{
return SIM_MODEL::SetParamValue( aParamName, aParamValue, aNotation );
}
SIM_MODEL::TYPE SIM_MODEL_SPICE::readTypeFromSpiceStrings( const wxString& aTypeString,
const wxString& aLevel,
const wxString& aVersion,
bool aSkipDefaultLevel )
{
std::unique_ptr<SIM_VALUE> readLevel = SIM_VALUE::Create( SIM_VALUE::TYPE_INT,
aLevel.ToStdString() );
for( TYPE type : TYPE_ITERATOR() )
{
wxString typePrefix = SpiceInfo( type ).modelType;
wxString level = SpiceInfo( type ).level;
wxString version = SpiceInfo( type ).version;
bool isDefaultLevel = SpiceInfo( type ).isDefaultLevel;
if( typePrefix == "" )
continue;
// Check if `aTypeString` starts with `typePrefix`.
if( aTypeString.Upper().StartsWith( typePrefix )
&& ( level == readLevel->ToString()
|| ( !aSkipDefaultLevel && isDefaultLevel && aLevel == "" ) )
&& version == aVersion )
{
return type;
}
}
// If the type string is not recognized, demote to a raw Spice element. This way the user won't
// have an error if there is a type KiCad does not recognize.
return TYPE::RAWSPICE;
}

View File

@ -0,0 +1,70 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* 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
*/
#ifndef SIM_MODEL_SPICE_H
#define SIM_MODEL_SPICE_H
#include <sim/sim_model.h>
#include <sim/spice_generator.h>
class SPICE_GENERATOR_SPICE : public SPICE_GENERATOR
{
using SPICE_GENERATOR::SPICE_GENERATOR;
wxString Preview( const wxString& aModelName ) const override;
};
class SIM_MODEL_SPICE : public SIM_MODEL
{
public:
friend class SPICE_GENERATOR_SPICE;
static TYPE ReadTypeFromSpiceCode( const wxString& aSpiceCode );
static std::unique_ptr<SIM_MODEL_SPICE> Create( const wxString& aSpiceCode );
virtual void ReadSpiceCode( const wxString& aSpiceCode );
protected:
using SIM_MODEL::SIM_MODEL;
bool SetParamValue( unsigned aParamIndex, const wxString& aParamValue,
SIM_VALUE_GRAMMAR::NOTATION aNotation
= SIM_VALUE_GRAMMAR::NOTATION::SI ) override;
virtual bool SetParamFromSpiceCode( const wxString& aParamName, const wxString& aParamValue,
SIM_VALUE_GRAMMAR::NOTATION aNotation
= SIM_VALUE_GRAMMAR::NOTATION::SPICE );
wxString m_spiceCode;
private:
static TYPE readTypeFromSpiceStrings( const wxString& aTypeString,
const wxString& aLevel = "",
const wxString& aVersion = "",
bool aSkipDefaultLevel = true );
};
#endif // SIM_MODEL_SPICE_H

View File

@ -68,7 +68,7 @@ std::vector<wxString> SPICE_GENERATOR_SUBCKT::CurrentNames( const wxString& aRef
SIM_MODEL_SUBCKT::SIM_MODEL_SUBCKT( TYPE aType ) : SIM_MODEL_SUBCKT::SIM_MODEL_SUBCKT( TYPE aType ) :
SIM_MODEL( aType, std::make_unique<SPICE_GENERATOR_SUBCKT>( *this ) ) SIM_MODEL_SPICE( aType, std::make_unique<SPICE_GENERATOR_SUBCKT>( *this ) )
{ {
} }

View File

@ -25,7 +25,7 @@
#ifndef SIM_MODEL_SUBCIRCUIT_H #ifndef SIM_MODEL_SUBCIRCUIT_H
#define SIM_MODEL_SUBCIRCUIT_H #define SIM_MODEL_SUBCIRCUIT_H
#include <sim/sim_model.h> #include <sim/sim_model_spice.h>
#include <sim/spice_generator.h> #include <sim/spice_generator.h>
@ -39,10 +39,10 @@ public:
}; };
class SIM_MODEL_SUBCKT : public SIM_MODEL class SIM_MODEL_SUBCKT : public SIM_MODEL_SPICE
{ {
public: public:
SIM_MODEL_SUBCKT( TYPE aType ); SIM_MODEL_SUBCKT( SIM_MODEL::TYPE aType );
void ReadSpiceCode( const wxString& aSpiceCode ) override; void ReadSpiceCode( const wxString& aSpiceCode ) override;
void SetBaseModel( const SIM_MODEL& aBaseModel ) override; void SetBaseModel( const SIM_MODEL& aBaseModel ) override;

View File

@ -181,12 +181,6 @@ wxString SPICE_GENERATOR::Preview( const wxString& aModelName ) const
{ {
wxString spiceCode = ModelLine( aModelName ); wxString spiceCode = ModelLine( aModelName );
if( spiceCode == "" )
spiceCode = m_model.m_spiceCode;
if( spiceCode == "" && m_model.GetBaseModel() )
spiceCode = m_model.GetBaseModel()->m_spiceCode;
wxString itemLine = ItemLine( "", aModelName ); wxString itemLine = ItemLine( "", aModelName );
if( spiceCode != "" ) if( spiceCode != "" )
spiceCode << "\n"; spiceCode << "\n";