KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_netlist_exporter_spice.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21#include <boost/test/results_collector.hpp> // To check if the current test failed (to be moved?).
22#include <eeschema_test_utils.h>
24#include <sim/ngspice.h>
26#include <wx/ffile.h>
27#include <mock_pgm_base.h>
28#include <locale_io.h>
29
30// A relative max error accepted when comparing 2 values
31#define MAX_DEFAULT_REL_ERROR 2e-2
32
33
35{
36public:
38 {
39 public:
40 SPICE_TEST_REPORTER( std::shared_ptr<wxString> aLog ) :
41 m_log( std::move( aLog ) )
42 {}
43
44 REPORTER& Report( const wxString& aText,
45 SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override
46 {
47 *m_log << aText << "\n";
48
49 // You can add a debug trace here.
50 return *this;
51 }
52
53 bool HasMessage() const override { return false; }
54
55 void OnSimStateChange( SIMULATOR* aObject, SIM_STATE aNewState ) override { }
56
57 private:
58 std::shared_ptr<wxString> m_log;
59 };
60
63 m_simulator( SPICE_SIMULATOR::CreateInstance( "ngspice" ) ),
64 m_log( std::make_shared<wxString>() ),
65 m_reporter( std::make_unique<SPICE_TEST_REPORTER>( m_log ) ),
66 m_abort( false )
67 {
68 }
69
71 {
72 using namespace boost::unit_test;
73
74 // Detach the reporter before m_reporter destructs. SetReporter blocks
75 // until any in-flight bg callback that holds the reporter has returned.
76 if( m_simulator )
77 m_simulator->SetReporter( nullptr );
78
79 test_case::id_t id = framework::current_test_case().p_id;
80 test_results results = results_collector.results( id );
81
82 // Output a log if the test has failed.
83 // Don't use BOOST_CHECK_MESSAGE because it triggers a checkpoint which affects debugging
84 if( !results.passed() )
85 {
86 BOOST_TEST_MESSAGE( "\nNGSPICE LOG\n===========\n" << *m_log );
87 }
88 }
89
90 wxFileName SchematicQAPath( const wxString& aBaseName ) override
91 {
92 wxFileName fn( KI_TEST::GetEeschemaTestDataDir() );
93 fn.AppendDir( "spice_netlists" );
94 fn.AppendDir( aBaseName );
95 fn.SetName( aBaseName );
97
98 return fn;
99 }
100
101 wxString GetNetlistPath( bool aTest = false ) override
102 {
103 wxFileName netFile = m_schematic->Project().GetProjectFullName();
104
105 if( aTest )
106 netFile.SetName( netFile.GetName() + "_test" );
107
108 netFile.SetExt( "spice" );
109 return netFile.GetFullPath();
110 }
111
112 void CompareNetlists() override
113 {
114 wxString netlistPath = GetNetlistPath( true );
115 BOOST_TEST_CHECKPOINT( "Comparing netlist " << netlistPath );
116
117 m_abort = false;
118
119 // Our simulator is actually Ngspice.
120 NGSPICE* ngspice = dynamic_cast<NGSPICE*>( m_simulator.get() );
121 BOOST_REQUIRE( ngspice );
122
123 ngspice->SetReporter( m_reporter.get() );
124
125 wxFFile file( netlistPath, "rt" );
126 wxString netlist;
127
128 BOOST_REQUIRE( file.IsOpened() );
129 file.ReadAll( &netlist );
130
131 //ngspice->Init();
132 ngspice->Command( "set ngbehavior=ps" );
133 ngspice->Command( "setseed 1" );
134 BOOST_REQUIRE( ngspice->LoadNetlist( std::string( netlist.ToUTF8() ) ) );
135
136 if( ngspice->Run() )
137 {
138 // wait for end of simulation.
139 // calling wxYield() allows printing activity, and stopping ngspice from GUI
140 // Also note: do not user wxSafeYield, because when using it we cannot stop
141 // ngspice from the GUI
142 do
143 {
144 wxMilliSleep( 50 );
145 wxYield();
146 } while( ngspice->IsRunning() );
147 }
148
149 // Detach the reporter while we read m_log on the main thread. m_ngSpice_Running
150 // can return false while a cbSendChar callback is still in flight, and that
151 // callback writes to *m_log. SetReporter(nullptr) blocks until the in-flight
152 // call returns.
153 ngspice->SetReporter( nullptr );
154
155 // Test if ngspice cannot run a simulation (missing code models).
156 // in this case the log contains "MIF-ERROR" and/or "Error: circuit not parsed"
157 // when the simulation is not run the spice command "linearize" crashes.
158 bool mif_error = m_log->Find( wxT( "MIF-ERROR" ) ) != wxNOT_FOUND;
159
160 BOOST_TEST_INFO( "Cannot run ngspice. test skipped. Missing code model files?" );
161 BOOST_CHECK( !mif_error );
162
163 bool err_found = m_log->Find( wxT( "Error: circuit not parsed" ) ) != wxNOT_FOUND;
164
165 // Re-attach so the rest of the foreground ngspice->Command calls below feed
166 // their output back into the log. These run on the main thread, so the
167 // cbSendChar callback fires synchronously and there's no concurrent reader.
168 ngspice->SetReporter( m_reporter.get() );
169
170 BOOST_TEST_INFO( "Cannot run ngspice. test skipped. Install error?" );
171 BOOST_CHECK( !err_found );
172
173 if( mif_error || err_found )
174 {
175 m_abort = true;
176
177 // Still display the original netlist in this case.
178 *m_log << "Original Netlist\n";
179 *m_log << "----------------\n";
180 *m_log << netlist << "\n";
181
182 return;
183 }
184
185 // We need to make sure that the number of points always the same.
186 ngspice->Command( "linearize" );
187
188
189 // Debug info.
190
191 // Display all vectors.
192 *m_log << "\n";
193 ngspice->Command( "echo Available Vectors" );
194 ngspice->Command( "echo -----------------" );
195 ngspice->Command( "display" );
196
197 // Display the original netlist.
198 *m_log << "\n";
199 *m_log << "Original Netlist\n";
200 *m_log << "----------------\n";
201 *m_log << netlist << "\n";
202
203 // Display the expanded netlist.
204 ngspice->Command( "echo Expanded Netlist" );
205 ngspice->Command( "echo ----------------" );
206 ngspice->Command( "listing runnable" );
207 }
208
209 void TestOpPoint( double aRefValue, const std::string& aVectorName,
210 double aMaxRelError = MAX_DEFAULT_REL_ERROR )
211 {
212 BOOST_TEST_CONTEXT( "Vector name: " << aVectorName )
213 {
214 NGSPICE* ngspice = static_cast<NGSPICE*>( m_simulator.get() );
215
216 std::vector<double> vector = ngspice->GetRealVector( aVectorName );
217
218 BOOST_REQUIRE_EQUAL( vector.size(), 1 );
219
220 double maxError = abs( aRefValue * aMaxRelError );
221 BOOST_CHECK_LE( abs( vector[0] - aRefValue ), aMaxRelError );
222 }
223 }
224
225 void TestPoint( const std::string& aXVectorName, double aXValue,
226 const std::map<const std::string, double> aTestVectorsAndValues,
227 double aMaxRelError = MAX_DEFAULT_REL_ERROR )
228 {
229 // The default aMaxRelError is fairly large because we have some problems with determinism
230 // in QA pipeline. We don't need to fix this for now because, if this has to be fixed in
231 // the first place, this has to be done from Ngspice's side.
232
233 BOOST_TEST_CONTEXT( "X vector name: " << aXVectorName << ", X value: " << aXValue )
234 {
235 NGSPICE* ngspice = static_cast<NGSPICE*>( m_simulator.get() );
236
237 std::vector<double> xVector = ngspice->GetRealVector( aXVectorName );
238 std::size_t i = 0;
239
240 for(; i < xVector.size(); ++i )
241 {
242 double inf = std::numeric_limits<double>::infinity();
243
244 double leftDelta = ( aXValue - ( i >= 1 ? xVector[i - 1] : -inf ) );
245 double middleDelta = ( aXValue - xVector[i] );
246 double rightDelta = ( aXValue - ( i < xVector.size() - 1 ? xVector[i + 1] : inf ) );
247
248 // Check if this point is the closest one.
249 if( abs( middleDelta ) <= abs( leftDelta )
250 && abs( middleDelta ) <= abs( rightDelta ) )
251 {
252 break;
253 }
254 }
255
256 BOOST_REQUIRE_LT( i, xVector.size() );
257
258 for( auto& [vectorName, refValue] : aTestVectorsAndValues )
259 {
260 std::vector<double> yVector = ngspice->GetGainVector( vectorName );
261
262 BOOST_REQUIRE_GE( yVector.size(), i + 1 );
263
264 BOOST_TEST_CONTEXT( "Y vector name: " << vectorName
265 << ", Ref value: " << refValue
266 << ", Actual value: " << yVector[i] )
267 {
268 double maxError = abs( refValue * aMaxRelError );
269
270 if( maxError == 0 )
271 {
272 // If refValue is 0, we need a obtain the max. error differently.
273 maxError = aMaxRelError;
274 }
275
276 BOOST_CHECK_LE( abs( yVector[i] - refValue ), maxError );
277 }
278 }
279 }
280 }
281
282 void TestTranPoint( double aTime,
283 const std::map<const std::string, double> aTestVectorsAndValues,
284 double aMaxRelError = MAX_DEFAULT_REL_ERROR )
285 {
286 TestPoint( "time", aTime, aTestVectorsAndValues, aMaxRelError );
287 }
288
289 void TestACPoint( double aFrequency,
290 const std::map<const std::string, double> aTestVectorsAndValues,
291 double aMaxRelError = MAX_DEFAULT_REL_ERROR )
292 {
293 TestPoint( "frequency", aFrequency, aTestVectorsAndValues, aMaxRelError );
294 }
295
296 wxString GetResultsPath( bool aTest = false )
297 {
298 wxFileName netlistPath( GetNetlistPath( aTest ) );
299 netlistPath.SetExt( "csv" );
300
301 return netlistPath.GetFullPath();
302 }
303
313
314 std::shared_ptr<SPICE_SIMULATOR> m_simulator;
315 std::shared_ptr<wxString> m_log;
316 std::unique_ptr<SPICE_TEST_REPORTER> m_reporter;
317 bool m_abort; // set to true to force abort durint a test
318};
bool Command(const std::string &aCmd) override final
Definition ngspice.cpp:404
bool IsRunning() override final
Execute a Spice command as if it was typed into console.
Definition ngspice.cpp:361
bool Run() override final
Halt the simulation.
Definition ngspice.cpp:338
bool LoadNetlist(const std::string &aNetlist) override final
Execute the simulation with currently loaded netlist.
Definition ngspice.cpp:311
std::vector< double > GetGainVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with phase values.
Definition ngspice.cpp:221
std::vector< double > GetRealVector(const std::string &aName, int aMaxLen=-1) override final
Return a requested vector with imaginary values.
Definition ngspice.cpp:166
REPORTER()
Definition reporter.h:73
Interface to receive simulation updates from SPICE_SIMULATOR class.
virtual void SetReporter(SIMULATOR_REPORTER *aReporter)
Set a SIMULATOR_REPORTER object to receive the simulation log.
bool HasMessage() const override
Returns true if any messages were reported.
void OnSimStateChange(SIMULATOR *aObject, SIM_STATE aNewState) override
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
std::shared_ptr< SPICE_SIMULATOR > m_simulator
void TestOpPoint(double aRefValue, const std::string &aVectorName, double aMaxRelError=MAX_DEFAULT_REL_ERROR)
void TestPoint(const std::string &aXVectorName, double aXValue, const std::map< const std::string, double > aTestVectorsAndValues, double aMaxRelError=MAX_DEFAULT_REL_ERROR)
wxFileName SchematicQAPath(const wxString &aBaseName) override
void TestTranPoint(double aTime, const std::map< const std::string, double > aTestVectorsAndValues, double aMaxRelError=MAX_DEFAULT_REL_ERROR)
void TestACPoint(double aFrequency, const std::map< const std::string, double > aTestVectorsAndValues, double aMaxRelError=MAX_DEFAULT_REL_ERROR)
wxString GetNetlistPath(bool aTest=false) override
std::unique_ptr< SPICE_TEST_REPORTER > m_reporter
static const std::string KiCadSchematicFileExtension
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
STL namespace.
SEVERITY
@ RPT_SEVERITY_UNDEFINED
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
std::string netlist
BOOST_TEST_INFO("Two-port Series .op current = "<< iDevice)
#define MAX_DEFAULT_REL_ERROR
BOOST_TEST_MESSAGE("\n=== Real-World Polygon PIP Benchmark ===\n"<< formatTable(table))
BOOST_TEST_CONTEXT("Test Clearance")