2016-08-11 12:41:30 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 CERN
|
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
2021-04-14 07:54:29 +00:00
|
|
|
* Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.TXT for contributors.
|
|
|
|
*
|
2016-08-11 12:41:30 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2016-08-11 12:41:43 +00:00
|
|
|
* as published by the Free Software Foundation; either version 3
|
2016-08-11 12:41:30 +00:00
|
|
|
* 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:
|
2016-08-11 12:41:43 +00:00
|
|
|
* https://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 3 license,
|
2016-08-11 12:41:30 +00:00
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "netlist_exporter_pspice_sim.h"
|
2021-07-29 09:56:22 +00:00
|
|
|
#include <string_utils.h>
|
2021-04-14 07:54:29 +00:00
|
|
|
#include <macros.h> // for TO_UTF8 def
|
2020-04-15 01:51:58 +00:00
|
|
|
#include <wx/regex.h>
|
2019-05-27 22:13:00 +00:00
|
|
|
#include <wx/tokenzr.h>
|
2016-08-11 12:41:52 +00:00
|
|
|
|
2020-04-15 01:51:58 +00:00
|
|
|
wxString NETLIST_EXPORTER_PSPICE_SIM::ComponentToVector(
|
|
|
|
const wxString& aName, SIM_PLOT_TYPE aType, const wxString& aParam ) const
|
2016-08-11 12:41:52 +00:00
|
|
|
{
|
|
|
|
wxString res;
|
|
|
|
|
|
|
|
// Some of the flags should exclude mutually
|
2021-04-14 07:54:29 +00:00
|
|
|
wxASSERT( ( ( aType & SPT_VOLTAGE ) == 0 ) != ( ( aType & SPT_CURRENT ) == 0 ) );
|
|
|
|
wxASSERT( ( ( aType & SPT_AC_PHASE ) == 0 ) || ( ( aType & SPT_AC_MAG ) == 0 ) );
|
2016-08-11 12:41:52 +00:00
|
|
|
|
|
|
|
if( aType & SPT_VOLTAGE )
|
|
|
|
{
|
2019-06-09 15:46:02 +00:00
|
|
|
// netnames are escaped (can contain "{slash}" for '/') Unscape them:
|
|
|
|
wxString spicenet = UnescapeString( aName );
|
|
|
|
|
2019-09-12 13:44:08 +00:00
|
|
|
// Spice netlist netnames does not accept some chars, which are replaced
|
2016-09-16 18:36:19 +00:00
|
|
|
// by eeschema netlist generator.
|
|
|
|
// Replace these forbidden chars to find the actual spice net name
|
|
|
|
NETLIST_EXPORTER_PSPICE::ReplaceForbiddenChars( spicenet );
|
2016-08-11 12:41:52 +00:00
|
|
|
|
2019-06-09 15:46:02 +00:00
|
|
|
return wxString::Format( "V(%s)", spicenet );
|
2016-08-11 12:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if( aType & SPT_CURRENT )
|
|
|
|
{
|
2019-09-12 13:44:08 +00:00
|
|
|
wxString device = GetSpiceDevice( aName ).Lower();
|
|
|
|
wxString param = aParam.Lower();
|
|
|
|
|
2020-12-29 12:39:49 +00:00
|
|
|
if( device.length() > 0 && device[0] == 'x' )
|
2019-09-12 13:44:08 +00:00
|
|
|
{
|
|
|
|
return "current probe of .subckt not yet implemented";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return wxString::Format( "@%s[%s]",
|
|
|
|
device,
|
|
|
|
param.IsEmpty() ? "i" : param );
|
|
|
|
}
|
2016-08-11 12:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-15 01:51:58 +00:00
|
|
|
SIM_PLOT_TYPE NETLIST_EXPORTER_PSPICE_SIM::VectorToSignal(
|
|
|
|
const std::string& aVector, wxString& aSignal ) const
|
|
|
|
{
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
// See ngspice manual chapt. 31.1 "Accessing internal device parameters"
|
|
|
|
wxRegEx internalDevParameter( "^@(\\w*[\\.\\w+]*)\\[(\\w*)\\]$", wxRE_ADVANCED );
|
|
|
|
wxString vector( aVector );
|
|
|
|
|
|
|
|
if( !internalDevParameter.Matches( vector ) )
|
|
|
|
{
|
|
|
|
// any text is a node name, which returns voltage
|
|
|
|
aSignal = "V(" + aVector + ")";
|
|
|
|
return SPT_VOLTAGE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxString paramType = internalDevParameter.GetMatch( vector, 2 );
|
|
|
|
|
|
|
|
if( paramType.Lower()[0] == 'i' )
|
|
|
|
{
|
|
|
|
// this is a branch current
|
|
|
|
paramType[0] = 'I';
|
|
|
|
aSignal = paramType + "(";
|
|
|
|
aSignal += internalDevParameter.GetMatch( vector, 1 ).Upper() + ")";
|
|
|
|
return SPT_CURRENT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return SPT_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 12:41:52 +00:00
|
|
|
const std::vector<wxString>& NETLIST_EXPORTER_PSPICE_SIM::GetCurrents( SPICE_PRIMITIVE aPrimitive )
|
|
|
|
{
|
|
|
|
static const std::vector<wxString> passive = { "I" };
|
|
|
|
static const std::vector<wxString> diode = { "Id" };
|
|
|
|
static const std::vector<wxString> bjt = { "Ib", "Ic", "Ie" };
|
|
|
|
static const std::vector<wxString> mos = { "Ig", "Id", "Is" };
|
|
|
|
static const std::vector<wxString> empty;
|
|
|
|
|
|
|
|
switch( aPrimitive )
|
|
|
|
{
|
|
|
|
case SP_RESISTOR:
|
|
|
|
case SP_CAPACITOR:
|
|
|
|
case SP_INDUCTOR:
|
|
|
|
case SP_VSOURCE:
|
|
|
|
return passive;
|
|
|
|
|
|
|
|
case SP_DIODE:
|
|
|
|
return diode;
|
|
|
|
|
|
|
|
case SP_BJT:
|
|
|
|
return bjt;
|
|
|
|
|
|
|
|
case SP_MOSFET:
|
|
|
|
return mos;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 12:41:30 +00:00
|
|
|
wxString NETLIST_EXPORTER_PSPICE_SIM::GetSheetSimCommand()
|
|
|
|
{
|
|
|
|
wxString simCmd;
|
|
|
|
|
|
|
|
UpdateDirectives( NET_ALL_FLAGS );
|
|
|
|
|
|
|
|
for( const auto& dir : GetDirectives() )
|
|
|
|
{
|
2016-08-11 12:41:34 +00:00
|
|
|
if( IsSimCommand( dir ) )
|
2016-08-11 12:41:30 +00:00
|
|
|
simCmd += wxString::Format( "%s\r\n", dir );
|
|
|
|
}
|
|
|
|
|
|
|
|
return simCmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 22:13:00 +00:00
|
|
|
wxString NETLIST_EXPORTER_PSPICE_SIM::GetUsedSimCommand()
|
|
|
|
{
|
|
|
|
return m_simCommand.IsEmpty() ? GetSheetSimCommand() : m_simCommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 12:41:34 +00:00
|
|
|
SIM_TYPE NETLIST_EXPORTER_PSPICE_SIM::GetSimType()
|
|
|
|
{
|
2019-05-27 22:13:00 +00:00
|
|
|
return CommandToSimType( GetUsedSimCommand() );
|
2016-08-11 12:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SIM_TYPE NETLIST_EXPORTER_PSPICE_SIM::CommandToSimType( const wxString& aCmd )
|
|
|
|
{
|
2020-04-15 01:51:58 +00:00
|
|
|
const std::vector<std::pair<wxString, SIM_TYPE>> simCmds = {
|
|
|
|
{ "^.ac\\M.*", ST_AC },
|
|
|
|
{ "^.dc\\M.*", ST_DC },
|
|
|
|
{ "^.tran\\M.*", ST_TRANSIENT },
|
|
|
|
{ "^.op\\M.*", ST_OP },
|
|
|
|
{ "^.disto\\M.*", ST_DISTORTION },
|
|
|
|
{ "^.noise\\M.*", ST_NOISE },
|
|
|
|
{ "^.pz\\M.*", ST_POLE_ZERO },
|
|
|
|
{ "^.sens\\M.*", ST_SENSITIVITY },
|
|
|
|
{ "^.tf\\M.*", ST_TRANS_FUNC } };
|
|
|
|
wxRegEx simCmd;
|
2016-08-11 12:41:34 +00:00
|
|
|
|
|
|
|
for( const auto& c : simCmds )
|
|
|
|
{
|
2020-04-15 01:51:58 +00:00
|
|
|
simCmd.Compile( c.first, wxRE_ADVANCED | wxRE_NOSUB | wxRE_ICASE );
|
|
|
|
|
|
|
|
if( simCmd.Matches( aCmd ) )
|
2016-08-11 12:41:34 +00:00
|
|
|
return c.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ST_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 22:13:00 +00:00
|
|
|
bool NETLIST_EXPORTER_PSPICE_SIM::ParseDCCommand( const wxString& aCmd, SPICE_DC_PARAMS* aSource1,
|
|
|
|
SPICE_DC_PARAMS* aSource2 )
|
|
|
|
{
|
|
|
|
if( !aCmd.Lower().StartsWith( ".dc" ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
wxString cmd = aCmd.Mid( 3 ).Trim().Trim( false );
|
|
|
|
|
|
|
|
wxStringTokenizer tokens( cmd );
|
|
|
|
|
|
|
|
size_t num = tokens.CountTokens();
|
|
|
|
|
|
|
|
if( num != 4 && num != 8 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
aSource1->m_source = tokens.GetNextToken();
|
|
|
|
aSource1->m_vstart = SPICE_VALUE( tokens.GetNextToken() );
|
|
|
|
aSource1->m_vend = SPICE_VALUE( tokens.GetNextToken() );
|
|
|
|
aSource1->m_vincrement = SPICE_VALUE( tokens.GetNextToken() );
|
|
|
|
|
|
|
|
if( num == 8 )
|
|
|
|
{
|
|
|
|
aSource2->m_source = tokens.GetNextToken();
|
|
|
|
aSource2->m_vstart = SPICE_VALUE( tokens.GetNextToken() );
|
|
|
|
aSource2->m_vend = SPICE_VALUE( tokens.GetNextToken() );
|
|
|
|
aSource2->m_vincrement = SPICE_VALUE( tokens.GetNextToken() );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 12:41:30 +00:00
|
|
|
void NETLIST_EXPORTER_PSPICE_SIM::writeDirectives( OUTPUTFORMATTER* aFormatter, unsigned aCtl ) const
|
|
|
|
{
|
2016-08-11 12:41:52 +00:00
|
|
|
// Add a directive to obtain currents
|
|
|
|
//aFormatter->Print( 0, ".options savecurrents\n" ); // does not work :(
|
|
|
|
|
|
|
|
for( const auto& item : GetSpiceItems() )
|
|
|
|
{
|
|
|
|
for( const auto& current :
|
|
|
|
NETLIST_EXPORTER_PSPICE_SIM::GetCurrents( (SPICE_PRIMITIVE) item.m_primitive ) )
|
|
|
|
{
|
2016-10-07 14:48:07 +00:00
|
|
|
if( !item.m_enabled )
|
|
|
|
continue;
|
|
|
|
|
2016-08-11 12:41:52 +00:00
|
|
|
/// @todo is it required to switch to lowercase
|
|
|
|
aFormatter->Print( 0, ".save %s\n",
|
2021-04-14 07:54:29 +00:00
|
|
|
TO_UTF8( ComponentToVector( item.m_refName, SPT_CURRENT, current ) ) );
|
2016-08-11 12:41:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we print out .save directives for currents, then it needs to be done for voltages as well
|
|
|
|
for( const auto& netMap : GetNetIndexMap() )
|
|
|
|
{
|
2021-06-09 19:32:58 +00:00
|
|
|
// the "0" and the "GND" nets are automatically saved internally by ngspice.
|
2019-07-07 15:55:43 +00:00
|
|
|
// Skip them
|
2020-04-15 01:51:58 +00:00
|
|
|
wxString netname = ComponentToVector( netMap.first, SPT_VOLTAGE );
|
2019-07-07 15:55:43 +00:00
|
|
|
|
|
|
|
if( netname == "V(0)" || netname == "V(GND)" )
|
|
|
|
continue;
|
|
|
|
|
2021-04-14 07:54:29 +00:00
|
|
|
aFormatter->Print( 0, ".save %s\n", TO_UTF8( netname ) );
|
2016-08-11 12:41:52 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 12:41:30 +00:00
|
|
|
if( m_simCommand.IsEmpty() )
|
|
|
|
{
|
|
|
|
// Fallback to the default behavior and just write all directives
|
|
|
|
NETLIST_EXPORTER_PSPICE::writeDirectives( aFormatter, aCtl );
|
|
|
|
}
|
2016-08-11 12:41:52 +00:00
|
|
|
else
|
2016-08-11 12:41:30 +00:00
|
|
|
{
|
2016-08-11 12:41:52 +00:00
|
|
|
// Dump all directives but simulation commands
|
|
|
|
for( const auto& dir : GetDirectives() )
|
|
|
|
{
|
|
|
|
if( !IsSimCommand( dir ) )
|
2021-04-14 07:54:29 +00:00
|
|
|
aFormatter->Print( 0, "%s\n", TO_UTF8( dir ) );
|
2016-08-11 12:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finish with our custom simulation command
|
2021-04-14 07:54:29 +00:00
|
|
|
aFormatter->Print( 0, "%s\n", TO_UTF8( m_simCommand ) );
|
2016-08-11 12:41:30 +00:00
|
|
|
}
|
|
|
|
}
|