Sim: Rename SIM_MODEL_SPICE to SIM_MODEL_RAW_SPICE

This commit is contained in:
Mikolaj Wielgus 2022-09-18 07:25:56 +02:00
parent 4128a5533f
commit a8d7a845b6
6 changed files with 12 additions and 435 deletions

View File

@ -285,7 +285,7 @@ set( EESCHEMA_SRCS
sim/sim_model_mutual_inductor.cpp
sim/sim_model_ngspice.cpp
sim/sim_model_ngspice_data.cpp
sim/sim_model_spice.cpp
sim/sim_model_raw_spice.cpp
sim/sim_model_source.cpp
sim/sim_model_subckt.cpp
sim/sim_model_switch.cpp

View File

@ -25,7 +25,7 @@
#include <dialog_sim_model.h>
#include <sim/sim_property.h>
#include <sim/sim_library_spice.h>
#include <sim/sim_model_spice.h>
#include <sim/sim_model_raw_spice.h>
#include <widgets/wx_grid.h>
#include <kiplatform/ui.h>
#include <confirm.h>
@ -329,7 +329,7 @@ void DIALOG_SIM_MODEL<T>::updateModelCodeTab()
m_codePreview->SetEditable( true ); // ???
if( dynamic_cast<SIM_MODEL_SPICE*>( &curModel() ) )
if( dynamic_cast<SIM_MODEL_RAW_SPICE*>( &curModel() ) )
{
// For raw Spice models display the whole file instead.

View File

@ -24,7 +24,7 @@
*/
#include "netlist_exporter_spice.h"
#include <sim/sim_model_spice.h>
#include <sim/sim_model_raw_spice.h>
#include <sim/spice_grammar.h>
#include <common.h>
#include <confirm.h>
@ -348,9 +348,9 @@ bool NETLIST_EXPORTER_SPICE::readModel( SCH_SYMBOL& aSymbol, ITEM& aItem )
}
// Special case for legacy models.
if( auto model = dynamic_cast<const SIM_MODEL_SPICE*>( aItem.model.get() ) )
if( auto model = dynamic_cast<const SIM_MODEL_RAW_SPICE*>( aItem.model.get() ) )
{
unsigned libParamIndex = static_cast<unsigned>( SIM_MODEL_SPICE::SPICE_PARAM::LIB );
unsigned libParamIndex = static_cast<unsigned>( SIM_MODEL_RAW_SPICE::SPICE_PARAM::LIB );
wxString path = model->GetParam( libParamIndex ).value->ToString();
if( path != "" )

View File

@ -28,7 +28,7 @@
#include <sim/sim_model_mutual_inductor.h>
#include <sim/sim_model_ngspice.h>
#include <sim/sim_model_source.h>
#include <sim/sim_model_spice.h>
#include <sim/sim_model_raw_spice.h>
#include <sim/sim_model_subckt.h>
#include <sim/sim_model_switch.h>
#include <sim/sim_model_tline.h>
@ -623,10 +623,10 @@ TYPE SIM_MODEL::InferTypeFromRefAndValue( const wxString& aRef, const wxString&
template <typename T>
TYPE SIM_MODEL::InferTypeFromLegacyFields( const std::vector<T>& aFields )
{
if( GetFieldValue( &aFields, SIM_MODEL_SPICE::LEGACY_TYPE_FIELD ) != ""
|| GetFieldValue( &aFields, SIM_MODEL_SPICE::LEGACY_MODEL_FIELD ) != ""
|| GetFieldValue( &aFields, SIM_MODEL_SPICE::LEGACY_ENABLED_FIELD ) != ""
|| GetFieldValue( &aFields, SIM_MODEL_SPICE::LEGACY_LIB_FIELD ) != "" )
if( GetFieldValue( &aFields, SIM_MODEL_RAW_SPICE::LEGACY_TYPE_FIELD ) != ""
|| GetFieldValue( &aFields, SIM_MODEL_RAW_SPICE::LEGACY_MODEL_FIELD ) != ""
|| GetFieldValue( &aFields, SIM_MODEL_RAW_SPICE::LEGACY_ENABLED_FIELD ) != ""
|| GetFieldValue( &aFields, SIM_MODEL_RAW_SPICE::LEGACY_LIB_FIELD ) != "" )
{
return TYPE::SPICE;
}
@ -1341,7 +1341,7 @@ std::unique_ptr<SIM_MODEL> SIM_MODEL::create( TYPE aType )
return std::make_unique<SIM_MODEL_XSPICE>( aType );
case TYPE::SPICE:
return std::make_unique<SIM_MODEL_SPICE>( aType );
return std::make_unique<SIM_MODEL_RAW_SPICE>( aType );
default:
return std::make_unique<SIM_MODEL_NGSPICE>( aType );

View File

@ -1,325 +0,0 @@
/*
* 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 <pegtl.hpp>
#include <pegtl/contrib/parse_tree.hpp>
namespace SIM_MODEL_SPICE_PARSER
{
using namespace SIM_MODEL_GRAMMAR;
template <typename Rule> struct legacyPinSequenceSelector : std::false_type {};
template <> struct legacyPinSequenceSelector<legacyPinNumber> : std::true_type {};
}
wxString SPICE_GENERATOR_SPICE::ModelLine( const wxString& aModelName ) const
{
return "";
}
wxString SPICE_GENERATOR_SPICE::ItemName( const wxString& aRefName ) const
{
wxString elementType = m_model.GetParam(
static_cast<int>( SIM_MODEL_SPICE::SPICE_PARAM::TYPE ) ).value->ToString();
if( aRefName != "" && aRefName.StartsWith( elementType ) )
return aRefName;
else
return elementType + aRefName;
}
wxString SPICE_GENERATOR_SPICE::ItemPins( const wxString& aRefName,
const wxString& aModelName,
const std::vector<wxString>& aSymbolPinNumbers,
const std::vector<wxString>& aPinNetNames ) const
{
wxString result;
for( const SIM_MODEL::PIN& pin : GetPins() )
{
auto it = std::find( aSymbolPinNumbers.begin(), aSymbolPinNumbers.end(),
pin.symbolPinNumber );
if( it != aSymbolPinNumbers.end() )
{
long symbolPinIndex = std::distance( aSymbolPinNumbers.begin(), it );
result << " " << aPinNetNames.at( symbolPinIndex );
}
}
return result;
}
wxString SPICE_GENERATOR_SPICE::ItemModelName( const wxString& aModelName ) const
{
return "";
}
wxString SPICE_GENERATOR_SPICE::ItemParams() const
{
wxString result;
for( const SIM_MODEL::PARAM& param : GetInstanceParams() )
{
if( param.info.name != "model" )
result << "";
else
result << " " << param.value->ToString();
}
return result;
}
wxString SPICE_GENERATOR_SPICE::Preview( const wxString& aModelName ) const
{
std::vector<wxString> pinNumbers;
std::vector<wxString> pinNetNames;
for( int i = 0; i < m_model.GetPinCount(); ++i )
{
pinNumbers.push_back( wxString::FromCDouble( i + 1 ) );
pinNetNames.push_back( wxString::FromCDouble( i + 1 ) );
}
return ItemLine( "", aModelName, pinNumbers, pinNetNames );
}
SIM_MODEL_SPICE::SIM_MODEL_SPICE( TYPE aType ) :
SIM_MODEL( aType, std::make_unique<SPICE_GENERATOR_SPICE>( *this ) )
{
static std::vector<PARAM::INFO> paramInfos = makeParamInfos();
for( const PARAM::INFO& paramInfo : paramInfos )
AddParam( paramInfo );
}
void SIM_MODEL_SPICE::ReadDataSchFields( unsigned aSymbolPinCount,
const std::vector<SCH_FIELD>* aFields )
{
SIM_MODEL::ReadDataSchFields( aSymbolPinCount, aFields );
readLegacyDataFields( aSymbolPinCount, aFields );
}
void SIM_MODEL_SPICE::ReadDataLibFields( unsigned aSymbolPinCount,
const std::vector<LIB_FIELD>* aFields )
{
SIM_MODEL::ReadDataLibFields( aSymbolPinCount, aFields );
readLegacyDataFields( aSymbolPinCount, aFields );
}
void SIM_MODEL_SPICE::WriteDataSchFields( std::vector<SCH_FIELD>& aFields ) const
{
SIM_MODEL::WriteDataSchFields( aFields );
// Erase the legacy fields.
SetFieldValue( aFields, LEGACY_TYPE_FIELD, "" );
SetFieldValue( aFields, LEGACY_PINS_FIELD, "" );
SetFieldValue( aFields, LEGACY_MODEL_FIELD, "" );
SetFieldValue( aFields, LEGACY_ENABLED_FIELD, "" );
SetFieldValue( aFields, LEGACY_LIB_FIELD, "" );
}
void SIM_MODEL_SPICE::WriteDataLibFields( std::vector<LIB_FIELD>& aFields ) const
{
SIM_MODEL::WriteDataLibFields( aFields );
}
void SIM_MODEL_SPICE::CreatePins( unsigned aSymbolPinCount )
{
for( unsigned symbolPinIndex = 0; symbolPinIndex < aSymbolPinCount; ++symbolPinIndex )
AddPin( { "", wxString::FromCDouble( symbolPinIndex + 1 ) } );
}
bool SIM_MODEL_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_SPICE::makeParamInfos()
{
std::vector<PARAM::INFO> paramInfos;
for( SPICE_PARAM spiceParam : SPICE_PARAM_ITERATOR() )
{
PARAM::INFO paramInfo;
switch( spiceParam )
{
case SPICE_PARAM::TYPE:
paramInfo.name = "type";
paramInfo.type = SIM_VALUE::TYPE_STRING;
paramInfo.unit = "";
paramInfo.category = SIM_MODEL::PARAM::CATEGORY::PRINCIPAL;
paramInfo.defaultValue = "";
paramInfo.description = "Spice element type";
paramInfo.isSpiceInstanceParam = true;
paramInfos.push_back( paramInfo );
break;
case SPICE_PARAM::MODEL:
paramInfo.name = "model";
paramInfo.type = SIM_VALUE::TYPE_STRING;
paramInfo.unit = "";
paramInfo.category = SIM_MODEL::PARAM::CATEGORY::PRINCIPAL;
paramInfo.defaultValue = "";
paramInfo.description = "Model name or value";
paramInfo.isSpiceInstanceParam = true;
paramInfos.push_back( paramInfo );
break;
case SPICE_PARAM::LIB:
paramInfo.name = "lib";
paramInfo.type = SIM_VALUE::TYPE_STRING;
paramInfo.unit = "";
paramInfo.category = SIM_MODEL::PARAM::CATEGORY::PRINCIPAL;
paramInfo.defaultValue = "";
paramInfo.description = "Library path to include";
paramInfo.isSpiceInstanceParam = true;
paramInfos.push_back( paramInfo );
break;
case SPICE_PARAM::_ENUM_END:
break;
}
}
return paramInfos;
}
template <typename T>
void SIM_MODEL_SPICE::readLegacyDataFields( unsigned aSymbolPinCount, const std::vector<T>* aFields )
{
// Fill in the blanks with the legacy parameters.
if( GetParam( static_cast<int>( SPICE_PARAM::TYPE ) ).value->ToString() == "" )
{
SetParamValue( static_cast<int>( SPICE_PARAM::TYPE ),
GetFieldValue( aFields, LEGACY_TYPE_FIELD ) );
}
if( GetFieldValue( aFields, PINS_FIELD ) == "" )
parseLegacyPinsField( aSymbolPinCount, GetFieldValue( aFields, LEGACY_PINS_FIELD ) );
if( GetParam( static_cast<int>( SPICE_PARAM::MODEL ) ).value->ToString() == "" )
{
SetParamValue( static_cast<int>( SPICE_PARAM::MODEL ),
GetFieldValue( aFields, LEGACY_MODEL_FIELD ) );
}
// If model param is still empty, then use Value field.
if( GetParam( static_cast<int>( SPICE_PARAM::MODEL ) ).value->ToString() == "" )
{
SetParamValue( static_cast<int>( SPICE_PARAM::MODEL ),
GetFieldValue( aFields, SIM_MODEL::VALUE_FIELD ) );
}
if( GetParam( static_cast<int>( SPICE_PARAM::LIB ) ).value->ToString() == "" )
{
SetParamValue( static_cast<int>( SPICE_PARAM::LIB ),
GetFieldValue( aFields, LEGACY_LIB_FIELD ) );
}
}
void SIM_MODEL_SPICE::parseLegacyPinsField( unsigned aSymbolPinCount,
const wxString& aLegacyPinsField )
{
if( aLegacyPinsField == "" )
return;
// Initially set all pins to Not Connected to match the legacy behavior.
for( int modelPinIndex = 0; modelPinIndex < GetPinCount(); ++modelPinIndex )
SetPinSymbolPinNumber( static_cast<int>( modelPinIndex ), "" );
tao::pegtl::string_input<> in( aLegacyPinsField.ToUTF8(), PINS_FIELD );
std::unique_ptr<tao::pegtl::parse_tree::node> root;
try
{
root = tao::pegtl::parse_tree::parse<SIM_MODEL_SPICE_PARSER::legacyPinSequenceGrammar,
SIM_MODEL_SPICE_PARSER::legacyPinSequenceSelector>
( in );
}
catch( const tao::pegtl::parse_error& e )
{
THROW_IO_ERROR( e.what() );
}
for( int pinIndex = 0; pinIndex < static_cast<int>( root->children.size() ); ++pinIndex )
{
std::string symbolPinStr = root->children.at( pinIndex )->string();
int symbolPinIndex = std::stoi( symbolPinStr ) - 1;
if( symbolPinIndex < 0 || symbolPinIndex >= static_cast<int>( aSymbolPinCount ) )
{
THROW_IO_ERROR( wxString::Format( _( "Invalid symbol pin index: '%s'" ),
symbolPinStr ) );
}
SetPinSymbolPinNumber( pinIndex, root->children.at( pinIndex )->string() );
}
}

View File

@ -1,98 +0,0 @@
/*
* 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
{
public:
using SPICE_GENERATOR::SPICE_GENERATOR;
wxString ModelLine( const wxString& aModelName ) const override;
wxString ItemName( const wxString& aRefName ) const override;
wxString ItemPins( const wxString& aRefName,
const wxString& aModelName,
const std::vector<wxString>& aSymbolPinNumbers,
const std::vector<wxString>& aPinNetNames ) const override;
wxString ItemModelName( const wxString& aModelName ) const override;
wxString ItemParams() const override;
wxString Preview( const wxString& aModelName ) const override;
};
class SIM_MODEL_SPICE : public SIM_MODEL
{
public:
DEFINE_ENUM_CLASS_WITH_ITERATOR( SPICE_PARAM,
TYPE,
MODEL,
LIB
)
static constexpr auto LEGACY_TYPE_FIELD = "Spice_Primitive";
static constexpr auto LEGACY_PINS_FIELD = "Spice_Node_Sequence";
static constexpr auto LEGACY_MODEL_FIELD = "Spice_Model";
static constexpr auto LEGACY_ENABLED_FIELD = "Spice_Netlist_Enabled";
static constexpr auto LEGACY_LIB_FIELD = "Spice_Lib_File";
SIM_MODEL_SPICE( TYPE aType );
//bool ReadSpiceCode( const std::string& aSpiceCode ) override;
void ReadDataSchFields( unsigned aSymbolPinCount, const std::vector<SCH_FIELD>* aFields ) override;
void ReadDataLibFields( unsigned aSymbolPinCount, const std::vector<LIB_FIELD>* aFields ) override;
void WriteDataSchFields( std::vector<SCH_FIELD>& aFields ) const override;
void WriteDataLibFields( std::vector<LIB_FIELD>& aFields ) const override;
protected:
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:
static std::vector<PARAM::INFO> makeParamInfos();
template <typename T>
void readLegacyDataFields( unsigned aSymbolPinCount, const std::vector<T>* aFields );
void parseLegacyPinsField( unsigned aSymbolPinCount, const wxString& aLegacyPinsField );
bool requiresSpiceModelLine() const override { return false; }
std::vector<std::unique_ptr<PARAM::INFO>> m_paramInfos;
};
#endif // SIM_MODEL_SPICE_H