PrcNgSpice.cpp
12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//**************************************************************************************************
// PrcNgSpice.cpp *
// ---------------- *
// Started : 2004-05-07 *
// Last Update : 2016-07-24 *
// Copyright : (C) 2004-2016 MSWaters *
//**************************************************************************************************
//**************************************************************************************************
// *
// 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. *
// *
//**************************************************************************************************
#include "PrcNgSpice.hpp"
//**************************************************************************************************
// Constructor.
PrcNgSpice::PrcNgSpice( void ) : PrcSimEngBase( )
{
// Set the simulation engine type
m_eSimEng = eSIMR_NGSPICE;
// Set the simulator binary file name if it can be found
bSetBinFile( BIN_NGSPICE );
}
//**************************************************************************************************
// Destructor.
PrcNgSpice::~PrcNgSpice( )
{
}
//**************************************************************************************************
// Match a component to a set of node labels.
//
// Component voltages cannot be specified using the component label in the NG-Spice print statement.
// The two node labels connected to the component must be specified. When parsing the print
// statement the component label must be derived from the node labels. This may not work where
// components are connected in parallel. The nodes are specified in the form "<Node1>,<Node2>"
// eg. "1,2".
//
// Argument List :
// roSimn - The simulation object
// rosToNodes - The nodes to match, also used to return the component label
//
// Return Values :
// true - Success (a component with the specified nodes was found)
// false - Failure
bool PrcNgSpice::bMatchCpnt( SimnNgSpice & roSimn, wxString & rosToNodes )
{
wxArrayString osaNodes;
Component tCpnt;
wxString os1;
size_t sz1;
// Argument validity checks
if( rosToNodes.IsEmpty( ) ) return( false );
if( roSimn.m_oaCpnts.GetCount( ) <= 0 ) return( false );
if( rosToNodes.Freq( wxT(',') ) != 1 ) return( false );
// Extract the node labels
osaNodes.Add( rosToNodes.BeforeFirst( wxT(',') ) );
osaNodes.Add( rosToNodes.AfterLast( wxT(',') ) );
// Attempt to match the nodes with a component
for( sz1=0; sz1<roSimn.m_oaCpnts.GetCount( ); sz1++ )
{
tCpnt = roSimn.m_oaCpnts.Item( sz1 );
if( ! tCpnt.bParse( ) ) continue;
if( tCpnt.rosaGetNodes( ) == osaNodes ) break;
}
// Was a match found?
if( ! tCpnt.bIsValid( ) ) return( false );
rosToNodes = tCpnt.rosGetName( );
return( true );
}
//**************************************************************************************************
// Convert a list of component labels to the corresponding list of node pairs.
//
// Argument List :
// rosaCpnts - A list of component labels
// roSimn - A simulation object
void PrcNgSpice::Convert2Nodes( wxArrayString & rosaCpnts, SimnNgSpice & roSimn )
{
Component tCpnt;
wxString os1;
size_t sz1;
for( sz1=0; sz1<rosaCpnts.GetCount( ); sz1++ )
{
os1 = rosaCpnts.Item( sz1 );
tCpnt = roSimn.roGetCpnt( os1 );
if( ! tCpnt.bParse( ) ) continue;
if( tCpnt.rosaGetNodes( ).GetCount( ) != 2 ) continue;
os1.Empty( );
os1 << tCpnt.rosaGetNodes( ).Item( 0 ) << wxT(',') << tCpnt.rosaGetNodes( ).Item( 1 );
rosaCpnts.Item( sz1 ) = os1;
}
}
//**************************************************************************************************
// Create a WIDTH command based on a PRINT command.
//
// By default raw output from NG-Spice uses an 80 column output width which allows about two
// dependant variables to be displayed. To display more information a .WIDTH statement must be added
// to increase the number columns in the output.
//
// Argument List :
// rosCmdPR - The Print command
//
// Return Values :
// Success - A .WIDTH command
// Failure - An empty string
wxString & PrcNgSpice::rosMakeCmdWIDTH( wxString & rosCmdPR )
{
static wxString osCmdWIDTH;
wxStringTokenizer ostk1;
wxString os1;
int i1=0;
osCmdWIDTH.Empty( );
ostk1.SetString( rosCmdPR );
if( ostk1.GetNextToken( ).StartsWith( wxT(".PR") ) )
{
os1 = ostk1.GetNextToken( );
if( os1.StartsWith( wxT("DC") ) )
i1 = 8 + 16 + ostk1.CountTokens( ) * 16;
else if( os1.StartsWith( wxT("AC") ) )
i1 = 8 + 2 * 16 + ostk1.CountTokens( ) * 16;
else if( os1.StartsWith( wxT("TR") ) )
i1 = 8 + 16 + ostk1.CountTokens( ) * 16;
if( i1 > 80 ) osCmdWIDTH << wxT(".WIDTH OUT=") << i1;
}
return( osCmdWIDTH );
}
//**************************************************************************************************
// This function is a generic results file formatter which works in most circumstance.
//
// Return Values :
// true - Success
// false - Failure
bool PrcNgSpice::bFmtGeneral( void )
{
wxString os1;
size_t sz1, sz2;
// This function requires the results file to be open
if( ! m_oFileResults.IsOpened( ) ) return( false );
// Find the beginning of the data area (ie. the first line starting with "Index")
for( sz1=0; sz1<m_oFileResults.GetLineCount( ); sz1++ )
{
os1 = m_oFileResults.GetLine( sz1 );
if( os1.StartsWith( wxT("Index") ) ) break;
}
// Delete everything before the data area
sz2 = sz1;
for( sz1=0; sz1<sz2; sz1++ ) m_oFileResults.RemoveLine( 0 );
if( m_oFileResults.GetLineCount( ) > 0 ) m_oFileResults.RemoveLine( 1 );
// Delete lines other than data lines
for( sz1=1; sz1<m_oFileResults.GetLineCount( ); sz1++ )
{
os1 = m_oFileResults.GetLine( sz1 );
if( ! wxIsdigit( os1.GetChar( 0 ) ) )
{
m_oFileResults.RemoveLine( sz1 );
sz1--;
}
}
// Check that a data section exists
if( m_oFileResults.GetLineCount( ) <= 1 )
{
SetErrMsg( wxT("Couldn't find a data section in the results file.") );
return( false );
}
return( true );
}
//**************************************************************************************************
// Format the column header in the results file.
//
// Return Values :
// true - Success
// false - Failure
bool PrcNgSpice::bFmtColLabels( void )
{
wxStringTokenizer ostk1;
wxString osLine, os1;
// This function requires the results file to be open
if( ! m_oFileResults.IsOpened( ) ) return( false );
// Get the column header line
ostk1.SetString( m_oFileResults.GetFirstLine( ) );
if( ostk1.CountTokens( ) < 3 )
{
SetErrMsg( wxT("There's insufficient column headers in the results file ie. should be 3 or more.") );
return( false );
}
// Dispose of the first field (ie. "Index")
ostk1.GetNextToken( );
// Extract the operator field and replace the NG-Spice column labels
os1 << ostk1.GetNextToken( ) << wxT(' ') << m_osColLbls;
ostk1.SetString( os1 );
// Format each field
osLine.Empty( );
while( ostk1.HasMoreTokens( ) )
{
// Add the next label to the line
// (06/08/2005) gWave breaks if there's a space after initial '#' ???
if( osLine.IsEmpty( ) ) osLine << wxT('#');
else osLine << wxT(' ');
osLine << ostk1.GetNextToken( );
}
osLine.Trim( ); // Remove trailing space characters
m_oFileResults.GetFirstLine( ) = osLine;
return( true );
}
//**************************************************************************************************
// Format a data lines in the results file.
//
// Return Values :
// true - Success
// false - Failure
bool PrcNgSpice::bFmtDataLines( void )
{
wxStringTokenizer ostk1;
wxString osLine, os1;
size_t sz1;
double df1;
// This function requires the results file to be open
if( ! m_oFileResults.IsOpened( ) ) return( false );
// Check that there is a data section
if( m_oFileResults.GetLineCount( ) <= 1 )
{
SetErrMsg( wxT("There's no data section in the results file.") );
return( false );
}
// Format the data lines
for( sz1=1; sz1<m_oFileResults.GetLineCount( ); sz1++ )
{
// Get a line of data
osLine = m_oFileResults.GetLine( sz1 );
// Tokenize the string
ostk1.SetString( osLine );
if( ostk1.CountTokens( ) < 3 ) return( false );
// Dispose of the first field
ostk1.GetNextToken( );
// Format each field
osLine.Empty( );
while( ostk1.HasMoreTokens( ) )
{
// Add the next parameter to the line
os1 = ostk1.GetNextToken( );
if( os1.Last( ) == wxT(',') )
{
os1.Last( ) = wxT(' ');
ostk1.GetNextToken( );
}
if( ! CnvtType::bStrToFlt( os1, &df1 ) ) df1 = -9.9e-99;
if( ! CnvtType::bFltToStr( df1, os1 ) ) os1 = wxT("-9.9e-99");
osLine << os1 << wxT(' ');
}
m_oFileResults.GetLine( sz1 ) = osLine;
osLine.Trim( ); // Remove trailing space characters
}
return( true );
}
//**************************************************************************************************
// Format any phase values contained in the results file.
//
// This is only pertinent for an AC analysis. It involves removing the discontinuity at 360 degree
// in the data.
//
// Return Values :
// true - Success
// false - Failure
bool PrcNgSpice::bFmtPhaseData( void )
{
// ??? (02/02/2006) Not yet implemented
return( true );
}
//**************************************************************************************************
// Make the process argument list based on the contents of a simulation object.
//
// Argument List :
// roSimn - The simulation object
//
// Return Values :
// true - Success
// false - Failure
bool PrcNgSpice::bMakeArgLst( SimnBase & roSimn )
{
wxString os1;
// Envoke the base class operations
if( ! PrcSimEngBase::bMakeArgLst( roSimn ) ) return( false );
// Construct the command line to execute the simulation
os1 = wxT("-n -b ") + roSimn.roGetSaveFile( ).GetFullPath( );
if( ! PrcBase::bSetArgLst( os1 ) )
{
SetErrMsg( wxT("Couldn't set argument list") );
return( false );
}
// Get results file column labels (replace node pairs with component names)
if( roSimn.eGetSimEng( ) == eSIMR_NGSPICE )
m_osColLbls = ((SimnNgSpice &) roSimn).m_oCmdPR.rosGetParamLst( );
return( true );
}
//**************************************************************************************************
// Format the contents of the results file so that gWave can read it.
//
// Ie. add a header line at the top containing parameter names followed by the columns of data.
//
// Return Values :
// true - Success
// false - Failure
bool PrcNgSpice::bFmtResults( void )
{
bool bRtn;
wxString os1;
m_osErrMsg.Empty( );
// Check that the results file exists
if( ! roGetResultsFile( ).FileExists( ) )
{
os1.Empty( );
os1 << wxT("Results file doesn't exist : \n\n") << roGetResultsFile( ).GetFullPath( );
SetErrMsg( os1 );
return( false );
}
// Attempt to open the results file
if( ! m_oFileResults.Open( roGetResultsFile( ).GetFullPath( ) ) )
{
os1.Empty( );
os1 << wxT("Results file couldn't be opened : \n\n") << roGetResultsFile( ).GetFullPath( );
SetErrMsg( os1 );
return( false );
}
// Format the simulation results
bRtn = bFmtGeneral( );
// Format the column header line
if( bRtn ) bRtn = bFmtColLabels( );
// Format the data lines
if( bRtn ) bRtn = bFmtDataLines( );
// Align all the columns in the results file
if( bRtn ) bRtn = PrcSimEngBase::bFmtResults( );
m_oFileResults.Write( ); // Save the changes to disk
m_oFileResults.Close( ); // Close the file
return( bRtn );
}
//**************************************************************************************************