SPICE_SIMULATOR: Code formatting & clean up

This commit is contained in:
Maciej Suminski 2016-08-11 14:41:07 +02:00
parent 9748b65a6d
commit 9e2485ea97
8 changed files with 349 additions and 280 deletions

View File

@ -180,6 +180,7 @@ set( EESCHEMA_SRCS
sim/sim_plot_frame_base.cpp
sim/sim_plot_frame.cpp
sim/sim_plot_panel.cpp
sim/spice_simulator.cpp
sim/ngspice.cpp
netlist_exporters/netlist_exporter.cpp

View File

@ -1,122 +1,142 @@
#include "sharedspice.h"
#include <cstdio>
#include <sstream>
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2016 CERN
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
* @author Maciej Suminski <maciej.suminski@cern.ch>
*
* 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 2
* 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:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "ngspice.h"
#include <wx/dynlib.h>
#include <string>
#include <vector>
#include "spice_simulator.h"
#include <reporter.h>
#include <sstream>
// TODO cmake modules to add include directory for ngspice
using namespace std;
class NGSPICE : public SPICE_SIMULATOR {
public:
NGSPICE();
virtual ~NGSPICE();
void Init();
bool LoadNetlist(const string& netlist);
bool Command(const string& cmd);
string GetConsole() const;
const vector<double> GetPlot( std::string name, int max_len = -1);
void dump();
private:
typedef void (*ngSpice_Init)(SendChar*, SendStat*, ControlledExit*,
SendData*, SendInitData*, BGThreadRunning*, void*);
typedef int (*ngSpice_Circ)(char** circarray);
typedef int (*ngSpice_Command)(char* command);
typedef pvector_info (*ngGet_Vec_Info)(char* vecname);
typedef char** (*ngSpice_AllVecs)(char* plotname);
typedef char** (*ngSpice_AllPlots)(void);
ngSpice_Init m_ngSpice_Init;
ngSpice_Circ m_ngSpice_Circ;
ngSpice_Command m_ngSpice_Command;
ngGet_Vec_Info m_ngGet_Vec_Info;
ngSpice_AllPlots m_ngSpice_AllPlots;
ngSpice_AllVecs m_ngSpice_AllVecs;
wxDynamicLibrary *m_dll;
static int cbSendChar( char* what, int id, void* user)
{
NGSPICE *sim = reinterpret_cast<NGSPICE*>(user);
printf("sim %p cr %p\n",sim, sim->m_consoleReporter );
if(sim->m_consoleReporter)
sim->m_consoleReporter->Report(what);
return 0;
}
static int cbSendStat( char* what, int id, void* user)
{
/* NGSPICE *sim = reinterpret_cast<NGSPICE*>(user);
if(sim->m_consoleReporter)
sim->m_consoleReporter->Report(what);*/
return 0;
}
};
NGSPICE::NGSPICE()
{
m_dll = new wxDynamicLibrary("/home/twl/projects_sw/ngspice-26/src/.libs/libngspice.so.0.0.0"); //, wxDL_LAZY);
printf("DLL at %p\n", m_dll);
assert(m_dll);
m_ngSpice_Init = (ngSpice_Init) m_dll->GetSymbol("ngSpice_Init");
printf("Init @ %p\n", m_ngSpice_Init);
m_ngSpice_Circ = (ngSpice_Circ) m_dll->GetSymbol("ngSpice_Circ");
m_ngSpice_Command = (ngSpice_Command) m_dll->GetSymbol("ngSpice_Command");
m_ngGet_Vec_Info = (ngGet_Vec_Info) m_dll->GetSymbol("ngGet_Vec_Info");
m_ngSpice_AllPlots = (ngSpice_AllPlots) m_dll->GetSymbol("ngSpice_AllPlots");
m_ngSpice_AllVecs = (ngSpice_AllVecs) m_dll->GetSymbol("ngSpice_AllVecs");
m_dll = new wxDynamicLibrary( "libngspice.so" );
assert( m_dll );
// Obtain function pointers
m_ngSpice_Init = (ngSpice_Init) m_dll->GetSymbol( "ngSpice_Init" );
m_ngSpice_Circ = (ngSpice_Circ) m_dll->GetSymbol( "ngSpice_Circ" );
m_ngSpice_Command = (ngSpice_Command) m_dll->GetSymbol( "ngSpice_Command" );
m_ngGet_Vec_Info = (ngGet_Vec_Info) m_dll->GetSymbol( "ngGet_Vec_Info" );
m_ngSpice_AllPlots = (ngSpice_AllPlots) m_dll->GetSymbol( "ngSpice_AllPlots" );
m_ngSpice_AllVecs = (ngSpice_AllVecs) m_dll->GetSymbol( "ngSpice_AllVecs" );
}
NGSPICE::~NGSPICE()
{
delete m_dll;
}
void NGSPICE::Init()
{
m_ngSpice_Init( &cbSendChar, &cbSendStat, NULL, NULL, NULL, NULL, this);
}
const vector<double> NGSPICE::GetPlot( std::string name, int max_len )
const vector<double> NGSPICE::GetPlot( const string& aName, int aMaxLen )
{
vector<double> data;
vector_info *vi = m_ngGet_Vec_Info((char*)name.c_str());
vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() );
if(vi->v_realdata)
for(int i = 0; i<vi->v_length;i++)
data.push_back(vi->v_realdata[i]);
if( vi->v_realdata )
{
for( int i = 0; i < vi->v_length; i++ )
data.push_back( vi->v_realdata[i] );
}
return data;
}
bool NGSPICE::LoadNetlist(const string& aNetlist)
{
// TODO remove the hard limit
char* lines[16384];
stringstream ss( aNetlist );
int n = 0;
while( !ss.eof() && n < 16384 )
{
char line[1024];
ss.getline( line, sizeof(line) );
lines[n++] = strdup(line);
printf("l '%s'\n", line);
}
lines[n] = NULL;
m_ngSpice_Circ( lines );
for(int i = 0; i < n; i++)
delete lines[i];
return true;
}
bool NGSPICE::Command( const string& aCmd )
{
m_ngSpice_Command( (char*)( aCmd + string( "\n" ) ).c_str() );
dump();
return true;
}
void NGSPICE::dump()
{
// m_ngSpice_Command("run\n");
char** plots = m_ngSpice_AllPlots();
for( int i = 0; plots[i]; ++i )
{
printf( "-> plot : %s\n", plots[i] );
char** vecs = m_ngSpice_AllVecs( plots[i] );
for( int j = 0; vecs[j]; j++ )
{
printf( " - vector %s\n", vecs[j] );
vector_info* vi = m_ngGet_Vec_Info( vecs[j] );
printf( " - v_type %x\n", vi->v_type );
printf( " - v_flags %x\n", vi->v_flags );
printf( " - v_length %d\n", vi->v_length );
}
}
}
#if 0
static string loadFile(const string& filename)
{
@ -128,77 +148,7 @@ static string loadFile(const string& filename)
return buf;
}
bool NGSPICE::LoadNetlist(const string& netlist)
{
char *lines[16384];
stringstream ss(netlist);
int n = 0;
while(!ss.eof())
{
char line[1024];
ss.getline(line, 1024);
lines[n++] = strdup(line);
printf("l '%s'\n", line);
}
lines[n]= NULL;
printf("netlist contains %d lines\n", n);
m_ngSpice_Circ(lines);
for(int i = 0; i < n; i++)
delete lines[i];
return true;
}
bool NGSPICE::Command(const string& cmd )
{
m_ngSpice_Command( (char*)(cmd + string("\n")).c_str());
dump();
return true;
}
void NGSPICE::dump()
{
// m_ngSpice_Command("run\n");
char **plots = m_ngSpice_AllPlots();
for(int i = 0; plots[i]; i++)
{
printf("-> plot : %s\n", plots[i]);
char **vecs = m_ngSpice_AllVecs(plots[i]);
for(int j = 0; vecs[j]; j++)
{
printf(" - vector %s\n", vecs[j]);
vector_info *vi = m_ngGet_Vec_Info(vecs[j]);
printf(" - v_type %x\n", vi->v_type);
printf(" - v_flags %x\n", vi->v_flags);
printf(" - v_length %d\n", vi->v_length);
}
}
}
NGSPICE::~NGSPICE()
{
printf("Killing ngspice\n");
delete m_dll;
}
#if 0
main()
{
NGSPICE spice;
@ -238,16 +188,28 @@ main()
std::string NGSPICE::GetConsole() const {
string NGSPICE::GetConsole() const {
return "";
}
SPICE_SIMULATOR::~SPICE_SIMULATOR()
{
int NGSPICE::cbSendChar( char* what, int id, void* user)
{
NGSPICE* sim = reinterpret_cast<NGSPICE*>( user );
printf("sim %p cr %p\n",sim, sim->m_consoleReporter );
if( sim->m_consoleReporter )
sim->m_consoleReporter->Report( what );
return 0;
}
SPICE_SIMULATOR *SPICE_SIMULATOR::CreateInstance( const std::string name )
int NGSPICE::cbSendStat( char* what, int id, void* user)
{
return new NGSPICE;
/* NGSPICE *sim = reinterpret_cast<NGSPICE*>(user);
if(sim->m_consoleReporter)
sim->m_consoleReporter->Report(what);*/
return 0;
}

View File

@ -1,20 +1,73 @@
#ifndef __NGSPICE_H
#define __NGSPICE_H
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2016 CERN
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
*
* 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 2
* 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:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <string>
#include <vector>
#ifndef NGSPICE_H
#define NGSPICE_H
class SPICE_SIMULATOR {
#include "sharedspice.h"
#include "spice_simulator.h"
class wxDynamicLibrary;
class NGSPICE : public SPICE_SIMULATOR {
public:
SPICE_SIMULATOR();
~SPICE_SIMULATOR();
NGSPICE();
virtual ~NGSPICE();
virtual void Init();
virtual bool LoadNetlist(const std::string& netlist);
virtual bool Command(const std::string& cmd);
void Init();
bool LoadNetlist( const std::string& aNetlist );
bool Command( const std::string& aCmd );
const std::vector<double> GetPlot( std::string name, int max_len = -1);
std::string GetConsole() const;
const std::vector<double> GetPlot( const std::string& aName, int aMaxLen = -1 );
void dump();
private:
typedef void (*ngSpice_Init)( SendChar*, SendStat*, ControlledExit*,
SendData*, SendInitData*, BGThreadRunning*, void* );
// ngspice library functions
typedef int (*ngSpice_Circ)(char** circarray);
typedef int (*ngSpice_Command)(char* command);
typedef pvector_info (*ngGet_Vec_Info)(char* vecname);
typedef char** (*ngSpice_AllVecs)(char* plotname);
typedef char** (*ngSpice_AllPlots)(void);
ngSpice_Init m_ngSpice_Init;
ngSpice_Circ m_ngSpice_Circ;
ngSpice_Command m_ngSpice_Command;
ngGet_Vec_Info m_ngGet_Vec_Info;
ngSpice_AllPlots m_ngSpice_AllPlots;
ngSpice_AllVecs m_ngSpice_AllVecs;
wxDynamicLibrary* m_dll;
static int cbSendChar( char* what, int id, void* user );
static int cbSendStat( char* what, int id, void* user );
};
#endif
#endif /* NGSPICE_H */

View File

@ -44,18 +44,22 @@
#include "sim_plot_panel.h"
#include "spice_simulator.h"
#ifdef KICAD_SCRIPTING
#include <python_scripting.h>
#endif
class SIM_REPORTER : public REPORTER
{
public:
SIM_REPORTER( wxRichTextCtrl *console )
SIM_REPORTER( wxRichTextCtrl* console )
{
m_console = console;
}
~SIM_REPORTER( )
~SIM_REPORTER()
{
}
virtual REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED )
@ -66,35 +70,37 @@ public:
}
private:
wxRichTextCtrl *m_console;
wxRichTextCtrl* m_console;
};
SIM_PLOT_FRAME::SIM_PLOT_FRAME( KIWAY *aKiway, wxWindow* parent )
: SIM_PLOT_FRAME_BASE( aKiway, parent )
SIM_PLOT_FRAME::SIM_PLOT_FRAME( KIWAY* aKiway, wxWindow* aParent )
: SIM_PLOT_FRAME_BASE( aKiway, aParent )
{
m_exporter = NULL;
m_simulator = NULL;
m_currentPlot = NULL;
m_pyConsole = NULL;
NewPlot();
TogglePythonConsole();
}
SIM_PLOT_FRAME::~SIM_PLOT_FRAME()
{
}
void SIM_PLOT_FRAME::StartSimulation()
{
if(m_exporter)
if( m_exporter )
delete m_exporter;
if(m_simulator)
if( m_simulator )
delete m_simulator;
m_simulator = SPICE_SIMULATOR::CreateInstance("ngspice");
m_simulator = SPICE_SIMULATOR::CreateInstance( "ngspice" );
m_simulator->SetConsoleReporter( new SIM_REPORTER( m_simConsole ) );
m_simulator->Init();
//m_simulator->SetConsoleReporter( , this );
@ -131,9 +137,10 @@ void SIM_PLOT_FRAME::StartSimulation()
//m_simulator->Command("quit\n");
}
void SIM_PLOT_FRAME::NewPlot()
{
SIM_PLOT_PANEL *plot = new SIM_PLOT_PANEL ( this, wxID_ANY );
m_plotNotebook->AddPage ( plot, wxT("Plot1"), true );
SIM_PLOT_PANEL* plot = new SIM_PLOT_PANEL( this, wxID_ANY );
m_plotNotebook->AddPage( plot, wxT( "Plot1" ), true );
m_currentPlot = plot;
}

View File

@ -20,34 +20,32 @@ class SIM_PLOT_PANEL;
/** Implementing SIM_PLOT_FRAME_BASE */
class SIM_PLOT_FRAME : public SIM_PLOT_FRAME_BASE
{
public:
/** Constructor */
SIM_PLOT_FRAME(KIWAY *aKiway, wxWindow* parent );
~SIM_PLOT_FRAME();
public:
/** Constructor */
SIM_PLOT_FRAME( KIWAY* aKiway, wxWindow* aParent );
~SIM_PLOT_FRAME();
void SetSchFrame( SCH_EDIT_FRAME* schFrame )
{
m_schematicFrame = schFrame;
}
void StartSimulation();
void SetSchFrame( SCH_EDIT_FRAME* schFrame )
{
m_schematicFrame = schFrame;
}
void NewPlot();
void StartSimulation();
void TogglePythonConsole();
void NewPlot();
private:
virtual void onNewPlot( wxCommandEvent& event ) { NewPlot(); }
SIM_PLOT_PANEL* m_currentPlot;
SCH_EDIT_FRAME* m_schematicFrame;
NETLIST_EXPORTER_PSPICE* m_exporter;
SPICE_SIMULATOR* m_simulator;
wxWindow* m_pyConsole;
private:
virtual void onNewPlot( wxCommandEvent& event ) { NewPlot(); }
SIM_PLOT_PANEL *m_currentPlot;
SCH_EDIT_FRAME *m_schematicFrame;
NETLIST_EXPORTER_PSPICE *m_exporter;
SPICE_SIMULATOR *m_simulator;
//// end generated class members
//// end generated class members
};
#endif // __sim_plot_frame__

View File

@ -4,71 +4,65 @@ static SIM_PLOT_PANEL *panel = NULL;
static int drawPlotFunc( mglGraph *graph )
{
printf("DrawPlot [%d traces]!\n", panel->m_traces.size());
printf("DrawPlot [%d traces]!\n", panel->m_traces.size());
graph->Clf();
//graph->SetRanges(-10e-3,10e-3,-2,2);
graph->Axis("x");
graph->Label('x',"Time",0);
graph->SetRange('x', 0, 10e-3);
graph->Clf();
//graph->SetRanges(-10e-3,10e-3,-2,2);
graph->Axis("x");
graph->Label('x',"Time",0);
graph->SetRange('x', 0, 10e-3);
graph->Axis("y");
graph->Label('y',"Voltage",0);
graph->SetRange('y', -1.5, 1.5);
graph->Axis("y");
graph->Label('y',"Voltage",0);
graph->SetRange('y', -1.5, 1.5);
for( auto t : panel->m_traces )
{
graph->AddLegend( (const char*) t.name.c_str(), "" );
graph->Plot( t.y );
}
graph->Box();
graph->Grid();
if ( panel->m_traces.size() )
graph->Legend(1,"-#");
for(auto t : panel->m_traces)
{
graph->AddLegend((const char *)t.name.c_str(),"");
graph->Plot(t.y);
}
graph->Box();
graph->Grid();
if ( panel->m_traces.size() )
graph->Legend(1,"-#");
return 0;
return 0;
}
SIM_PLOT_PANEL::SIM_PLOT_PANEL( wxWindow * parent,
wxWindowID id,
const wxPoint & pos,
const wxSize & size,
long style,
const wxString & name )
: wxMathGL ( parent, id, pos, size, style, name )
SIM_PLOT_PANEL::SIM_PLOT_PANEL( wxWindow* parent, wxWindowID id, const wxPoint& pos,
const wxSize& size, long style, const wxString& name )
: wxMathGL( parent, id, pos, size, style, name )
{
panel = this;
panel = this;
AutoResize = true;
SetDraw( drawPlotFunc );
// Update();
AutoResize = true;
SetDraw( drawPlotFunc );
// Update();
}
SIM_PLOT_PANEL::~SIM_PLOT_PANEL()
{
}
void SIM_PLOT_PANEL::AddTrace(const wxString& name, int n_points, double *t, double *x, int flags )
{
Trace trace;
Trace trace;
trace.name = name;
trace.x.Set(t, n_points);
trace.y.Set(x, n_points);
trace.name = name;
trace.x.Set(t, n_points);
trace.y.Set(x, n_points);
m_traces.push_back(trace);
Update();
m_traces.push_back(trace);
Update();
}
void SIM_PLOT_PANEL::DeleteTraces()
{
m_traces.clear();
Update();
m_traces.clear();
Update();
}

View File

@ -0,0 +1,31 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2016 CERN
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
*
* 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 2
* 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:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "ngspice.h"
SPICE_SIMULATOR* SPICE_SIMULATOR::CreateInstance( const std::string& )
{
return new NGSPICE;
}

View File

@ -1,44 +1,67 @@
#ifndef __NGSPICE_H
#define __NGSPICE_H
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2016 CERN
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
*
* 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 2
* 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:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef SPICE_SIMULATOR_H
#define SPICE_SIMULATOR_H
#include <string>
#include <vector>
enum SimTraceType
{
SIM_AC_MAG = 0x1,
SIM_AC_PHASE = 0x2,
SIM_TR_VOLTAGE = 0x4,
SIM_TR_CURRENT = 0x8,
SIM_TR_FFT = 0x10
};
class REPORTER;
enum SIM_TRACE_TYPE
{
SIM_AC_MAG = 0x1,
SIM_AC_PHASE = 0x2,
SIM_TR_VOLTAGE = 0x4,
SIM_TR_CURRENT = 0x8,
SIM_TR_FFT = 0x10
};
class SPICE_SIMULATOR {
public:
typedef void (*ConsoleCallback)( bool isError, const wxString& message, void *userData );
SPICE_SIMULATOR() {}
virtual ~SPICE_SIMULATOR() {}
static SPICE_SIMULATOR *CreateInstance( const std::string name );
static SPICE_SIMULATOR* CreateInstance( const std::string& aName );
SPICE_SIMULATOR(){}
virtual ~SPICE_SIMULATOR() = 0;
typedef void (*ConsoleCallback)( bool isError, const std::string& message, void* userData );
virtual void Init() = 0;
virtual bool LoadNetlist(const std::string& netlist) = 0;
virtual bool Command(const std::string& cmd) = 0;
virtual void SetConsoleReporter ( REPORTER *rep )
virtual bool LoadNetlist( const std::string& aNetlist ) = 0;
virtual bool Command( const std::string& aCmd ) = 0;
virtual void SetConsoleReporter( REPORTER* aReporter )
{
m_consoleReporter = rep;
m_consoleReporter = aReporter;
}
virtual const std::vector<double> GetPlot( std::string name, int max_len = -1) = 0;
virtual const std::vector<double> GetPlot( const std::string& aName, int aMaxLen = -1) = 0;
protected:
REPORTER *m_consoleReporter;
REPORTER* m_consoleReporter;
};
#endif
#endif /* SPICE_SIMULATOR_H */