Blame view

GUI/SW2/SRC/src/process/PrcGnuCap.cpp 15.1 KB
886c558b   Steve Greedy   SACAMOS Public Re...
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//**************************************************************************************************
//                                          PrcGnuCap.cpp                                          *
//                                         ---------------                                         *
// Started     : 2003-09-01                                                                        *
// Last Update : 2016-07-24                                                                        *
// Copyright   : (C) 2003-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 "PrcGnuCap.hpp"

//**************************************************************************************************
// Constructor.

PrcGnuCap::PrcGnuCap( void ) : PrcSimEngBase( )
{
  // Set the simulation engine type
  m_eSimEng = eSIMR_GNUCAP;

  // Set the simulator binary file name if it can be found
  bSetBinFile( BIN_GNUCAP );
}

//**************************************************************************************************
// Destructor.

PrcGnuCap::~PrcGnuCap( )
{
}

//**************************************************************************************************
// Check / format the component lines in the simulation object so that they are
// compatible with GNU-Cap. Ie. add the substrate node to BJT components which
// are missing it, the substrate node is assumed to be connected to the
// collector. Eg. "Q1 nc nb ne BC109" would become "Q1 nc nb ne nc BC109".
//
// Argument List :
//   roaCpnts - An array of ArrayComponent objects
//
// Return Values :
//   true  - Success
//   false - Failure
/* ??? 17/08/2009
bool  PrcGnuCap::bFmtCpnts( ArrayComponent & roaCpnts )
{
  wxString  os1;
  size_t    sz1;

  for( sz1=0; sz1<roaCpnts.GetCount( ); sz1++ )
  {
    if( roaCpnts[ sz1 ].m_eType != eCPNT_BJT )        continue;
    if( roaCpnts[ sz1 ].m_osaNodes.GetCount( ) != 4 ) continue;

    os1 = roaCpnts[ sz1 ].m_osaNodes[ 0 ];  // Get the collector node label
    roaCpnts[ sz1 ].m_osaNodes.Add( os1 );  // Add substrate node to node list
  }

  return( true );
}
*/
//**************************************************************************************************
// This function is a generic results file formatter which works in most circumstance.
//
// Return Values :
//   true  - Success
//   false - Failure

bool  PrcGnuCap::bFmtGeneral( void )
{
  wxString  os1, os2;
  size_t    sz1, sz2;
  wxChar    oc1;

  // This function requires the result file to have been opened
  if( ! m_oFileResults.IsOpened( ) ) return( false );

  // Find the beginning of the data area (ie. the last line beginning with '#') and delete all
  // preceding lines
  for( sz1=sz2=0; sz1<m_oFileResults.GetLineCount( ); sz1++ )
  {
    os1 = m_oFileResults.GetLine( sz1 );
    if( ! os1.IsEmpty( ) )
      if( os1.GetChar( 0 ) == wxT('#') )
        sz2 = sz1;
  }
  for( sz1=0; sz1<sz2; sz1++ ) m_oFileResults.RemoveLine( 0 );

  // Delete any simulator error messages (eg. "open circuit: internal node 3"). All lines should
  // start with a digit except for the first line (ie. the column headers).
  for( sz1=1; !m_oFileResults.Eof( ) && sz1<m_oFileResults.GetLineCount( ); sz1++ )
  {
    os1 = m_oFileResults.GetLine( sz1 );

    if( os1.Length( ) <= 1 )
    { // Delete empty lines
      m_oFileResults.RemoveLine( sz1 );
      sz1--;
      continue;
    }

    oc1 = os1.GetChar( 1 );
    if( ! wxIsdigit( oc1 ) && oc1!=wxT('-') && oc1!=wxT('+') )
    { // Delete non-data lines
      m_oFileResults.RemoveLine( sz1 );
      sz1--;
      continue;
    }
  }

  // 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 headers in the results file. Set the label spacing and insert the input
// variable name into the column labels if necessary.
//
// Return Values :
//   true  - Success
//   false - Failure

bool  PrcGnuCap::bFmtColLabels( void )
{
  wxStringTokenizer  ostk1;
  wxString           osLine, os1, os2;
//  int                i1;

  // This function requires the result file to have already been opened
  if( ! m_oFileResults.IsOpened( ) ) return( false );

  // Get the column header line
  ostk1.SetString( m_oFileResults.GetFirstLine( ) );
  if( ostk1.CountTokens( ) < 1 )
  {
    SetErrMsg( wxT("There's insufficient column headers in the results file ie. should be 2 or more.") );
    return( false );
  }

  // GNU-Cap has a bug in the OP and DC analysis results. In the case of OP the column label for the
  // temperature values is missing ie. "Temp". For DC the column label for the sweep source values
  // is missing eg. "Vin". This section of code inserts these labels.
  os1 = ostk1.GetNextToken( );
  os2 = roGetResultsFile( ).GetFullName( ).Right( 3 );
// osLine = wxT("# "); // ??? 06/08/2005 Gwave breaks if there's a space after the initial '#'
  osLine = wxT('#');
  if(      os2 == wxT(".op") ) osLine << wxT("Temp");
  else if( os2 == wxT(".dc") ) osLine << m_osSwpSrcName;
  else                         osLine << os1.Right( os1.Length( )-1 );

  // Format each header field
  while( ostk1.HasMoreTokens( ) )
  {
    // Pad the column with spaces to the required width
//    i1 = GNUCAP_COL_WD - (osLine.Length( ) % GNUCAP_COL_WD);
//    osLine.Append( wxT(' '), i1 );

    // Add the next label to the line
    osLine << wxT(' ') << ostk1.GetNextToken( );
  }

  osLine.Trim( ); // Remove trailing space characters

  m_oFileResults.GetFirstLine( ) = osLine;

  return( true );
}

//**************************************************************************************************
// Format the data lines in the results file.
//
// Return Values :
//   true  - Success
//   false - Failure

bool  PrcGnuCap::bFmtDataLines( void )
{
  wxStringTokenizer  ostk1;
  wxString           osLine, os1;
  size_t             sz1;
  double             df1;
//  int                i1;

  // This function requires the result file to have been opened
  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( ) < 1 )   return( false );

    // Format each field
    osLine.Empty( );
    while( ostk1.HasMoreTokens( ) )
    {
      // Add the next parameter to the line
      os1 = ostk1.GetNextToken( );
      if( ! CnvtType::bStrToFlt( os1, &df1 ) ) df1 =      -9.9e-99;
      if( ! CnvtType::bFltToStr( df1,  os1 ) ) os1 = wxT("-9.9e-99");
      osLine << os1 << wxT(' ');

      // Pad the column with spaces to the required width
//      i1 = GNUCAP_COL_WD - (osLine.Length( ) % GNUCAP_COL_WD);
//      osLine.Append( wxT(' '), i1 );
    }

    osLine.Trim( ); // Remove trailing space characters

    m_oFileResults.GetLine( sz1 ) = osLine;
  }

  return( true );
}

//**************************************************************************************************
// This function is a results file formatter for Fourier analysis result.
//
// Return Values :
//   true  - Success
//   false - Failure

bool  PrcGnuCap::bFmtResultsFO( void )
{
  bool               bRtn;
  wxConfig         * poConfig;
  wxStringTokenizer  ostk1;
  wxString           os1, os2, os3;
  size_t             sz1, sz2;
  bool               b1;

  // This function requires the result file to have been opened
  if( ! m_oFileResults.IsOpened( ) ) return( false );

  // Find the beginning of the data area
  for( sz1=sz2=0; sz1<m_oFileResults.GetLineCount( ); sz1++ )
  {
    os1 = m_oFileResults.GetLine( sz1 );
    if( ! os1.IsEmpty( ) )
    {
      if( os1.Contains( wxT("--- actual ---") ) )
      {
        sz2 = sz1;
        break;
      }
    }
  }

  // Delete all the lines before the data area
  for( sz1=0; sz1<sz2; sz1++ ) m_oFileResults.RemoveLine( 0 );

  // Delete unwanted header lines
  for( os1=m_oFileResults.GetFirstLine( ); !m_oFileResults.Eof( );
                                                                 os1=m_oFileResults.GetNextLine( ) )
  {
    if( os1.StartsWith( wxT("#freq") ) )
      m_oFileResults.RemoveLine( m_oFileResults.GetCurrentLine( ) );
  }

  // Setup the configuration pointer
  poConfig = (wxConfig *) wxConfig::Get( );
  if( poConfig == NULL ) return( false );
  poConfig->SetPath( wxT("/GNU-Cap") );

  // Delete unwanted columns of data
  for( ostk1.SetString( m_oFileResults.GetFirstLine( ) ); !m_oFileResults.Eof( );
                                                  ostk1.SetString( m_oFileResults.GetNextLine( ) ) )
  {
    if( ostk1.CountTokens( ) != 7 ) continue;

    // Get the operator field ie. frequency
    os1 = ostk1.GetNextToken( );

    // Do we want actual or relative magnitude?
    poConfig->Read( wxT("FO_MagRel"), &b1, false );
    if( b1 ) for( sz1=0; sz1<3; sz1++ ) ostk1.GetNextToken( );

    // Do we want dB magnitude?
    poConfig->Read( wxT("FO_MagDB"), &b1, false );
    if( b1 )
    {
      ostk1.GetNextToken( );
      os1 << wxT(' ') << ostk1.GetNextToken( );
    }
    else
    {
      os1 << wxT(' ') << ostk1.GetNextToken( );
      ostk1.GetNextToken( );
    }

    // Do we want phase?
    poConfig->Read( wxT("FO_Phase"), &b1, false );
    if( b1 ) os1 << wxT(' ') << ostk1.GetNextToken( );

    // Record the data
    m_oFileResults[ m_oFileResults.GetCurrentLine( ) ] = os1;
  }

  // Construct the data column headers
  os1 = wxT("#Freq");
  for( ostk1.SetString( m_oFileResults.GetFirstLine( ) ); !m_oFileResults.Eof( );
                                                  ostk1.SetString( m_oFileResults.GetNextLine( ) ) )
  {
    // Look for header lines
    if( ostk1.GetNextToken( ) != wxT("#") ) continue;

    os2 = ostk1.GetNextToken( );

    // Do we want dB magnitude?
    os3 = os2;
    poConfig->Read( wxT("FO_MagDB"), &b1, false );
    if( b1 ) os3.insert( 1, wxT("DB") );
    os1 << wxT(' ') << os3;

    // Do we want phase?
    poConfig->Read( wxT("FO_Phase"), &b1, false );
    if( b1 )
    {
      os3 = os2;
      os3.insert( 1, wxT("P") );
      os1 << wxT(' ') << os3;
    }

    // Remove the unwanted header lines
    if( m_oFileResults.GetCurrentLine( ) > 0 )
      m_oFileResults.RemoveLine( m_oFileResults.GetCurrentLine( ) );
  }
  m_oFileResults[ 0 ] = os1;

  // Determine the number of unique data lines
  ostk1.SetString( m_oFileResults.GetFirstLine( ) );
  ostk1.SetString( m_oFileResults.GetNextLine( ) );
  os1 = ostk1.GetNextToken( );
  for( ostk1.SetString( m_oFileResults.GetNextLine( ) ), sz1=1; !m_oFileResults.Eof( );
                                           ostk1.SetString( m_oFileResults.GetNextLine( ) ), sz1++ )
  {
    if( os1 == ostk1.GetNextToken( ) )
      break;
  }

  // Concatinate data lines according to frequency
  for( sz2=1; sz2<=sz1; sz2++ )
  {
    m_oFileResults.GoToLine( sz2-1 );
    ostk1.SetString( m_oFileResults.GetNextLine( ) );
    os1 = ostk1.GetNextToken( );
    for( ostk1.SetString( m_oFileResults.GetNextLine( ) ); !m_oFileResults.Eof( );
                                                  ostk1.SetString( m_oFileResults.GetNextLine( ) ) )
    {
      if( os1 == ostk1.GetNextToken( ) )
        m_oFileResults[ sz2 ] << wxT(' ') << ostk1.GetString( );
    }
  }

  // Delete data lines which are no longer needed
  m_oFileResults.GoToLine( sz1+1 );
  while( ! m_oFileResults.Eof( ) )
    m_oFileResults.RemoveLine( m_oFileResults.GetCurrentLine( ) );

  // Format the column header line and insert it at the start of the file
  bRtn = bFmtColLabels( );

  // Format the data lines
  if( ! bFmtDataLines( ) ) bRtn = false;

  return( bRtn );
}

//**************************************************************************************************
// 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  PrcGnuCap::bMakeArgLst( SimnBase & roSimn )
{
  wxString  os1;

  // Record the sweep source component name since GNU-Cap has a bug in the DC
  // analysis results. The column label for the sweep source values is missing.
  m_osSwpSrcName = roSimn.m_oCpntSwpSrc.rosGetName( );

  // Envoke the base class operations
  if( ! PrcSimEngBase::bMakeArgLst( roSimn ) ) return( false );

  // Construct the command line to execute the simulation
  os1 = wxT("-b ") + roSimn.roGetSaveFile( ).GetFullPath( );
  if( ! bSetArgLst( os1 ) )
  {
    SetErrMsg( wxT("Couldn't set argument list") );
    return( false );
  }

  return( true );
}

//**************************************************************************************************
// Format the contents of the results file so that gWave can read it ie. a header line at the top
// containing parameter names followed by the columns of data.
//
// Return Values :
//   true  - Success
//   false - Failure

bool  PrcGnuCap::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
  os1 = roGetResultsFile( ).GetFullName( ).Right( 3 );
  if( os1 != wxT(".fo") )
  {
    bRtn = bFmtGeneral( );

    // Format the column header line and insert it at the start of the file
    if( bRtn ) bRtn = bFmtColLabels( );

    // Format the data lines
    if( bRtn ) bRtn = bFmtDataLines( );
  }
  else bRtn = bFmtResultsFO( );

  // 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 );
}

//**************************************************************************************************