Blame view

GUI/SW2/SRC/src/base/PrcSimEngBase.cpp 9.02 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
//**************************************************************************************************
//                                        PrcSimEngBase.cpp                                        *
//                                       -------------------                                       *
// Started     : 2004-04-25                                                                        *
// Last Update : 2016-09-30                                                                        *
// 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 "PrcSimEngBase.hpp"

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

PrcSimEngBase::PrcSimEngBase( void ) : PrcBase( wxPROCESS_REDIRECT )
{
  m_eSimEng = eSIMR_NONE;
}

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

PrcSimEngBase::~PrcSimEngBase( )
{
}

//**************************************************************************************************
// Set the simulation results file name.
//
// Argument List :
//   rosFileName - A string containing the full path and file name
//
// Return Values :
//   true  - Success
//   false - Failure

bool  PrcSimEngBase::bSetResultsFile( const wxString & rosFileName )
{
  wxFileName  ofn1;

  ofn1 = rosFileName;
  if( ofn1.GetPath( ).IsEmpty( ) ) ofn1.SetPath( wxT(".") );

  if( ! ofn1.IsOk( ) ) return( false );

  m_oNameResults = ofn1;

  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  PrcSimEngBase::bMakeArgLst( SimnBase & roSimn )
{
  wxString  os1;
  int       i1;

  // Clear error attributes
  m_osErrMsg.Empty( );

  // Only proceed if the simulator isn't already running
  if( bIsExec( ) )
  {
    SetErrMsg( wxT("Simulator already running") );
    return( false );
  }

  // Create the results file name
  os1 = roSimn.roGetLoadFile( ).GetPath( ) + wxT('/')
      + roSimn.roGetLoadFile( ).GetName( );
  i1 = os1.Find( wxT(".gspiceui") );
  if( i1 > 0 ) os1 = os1.Truncate( (size_t) i1 );
  os1 << wxT('.') << roGetBinFile( ).GetName( );
  switch( roSimn.eGetAnaType( ) )
  {
    case eCMD_OP : os1 << wxT(".op"); break;
    case eCMD_DC : os1 << wxT(".dc"); break;
    case eCMD_AC : os1 << wxT(".ac"); break;
    case eCMD_TR : os1 << wxT(".tr"); break;
    case eCMD_FO : os1 << wxT(".fo"); break;
    case eCMD_DI : os1 << wxT(".di"); break;
    case eCMD_NO : os1 << wxT(".no"); break;
    case eCMD_PZ : os1 << wxT(".pz"); break;
    case eCMD_SE : os1 << wxT(".se"); break;
    case eCMD_TF : os1 << wxT(".tf"); break;
    default      : return( false );
  }

  // Set the results file name
  if( ! PrcSimEngBase::bSetResultsFile( os1 ) )
  {
    SetErrMsg( wxT("Couldn't set results file name") );
    return( false );
  }

  // Construct the default argument list
  os1 = roSimn.roGetSaveFile( ).GetFullPath( );
  if( ! bSetArgLst( os1 ) )
  {
    SetErrMsg( wxT("Couldn't set argument list") );
    return( false );
  }

  return( true );
}

//**************************************************************************************************
// Execute a simulation.
//
// Argument List :
//   roSimn - The simulation object
//
// Return Values :
//   true  - Success
//   false - Failure

bool  PrcSimEngBase::bExec( void )
{
  wxString  os1, os2;

  // Execute the simulation
  if( ! PrcBase::bExecAsync( ) )   return( false );

  // Save the simulation results to the log file
  if( ! PrcBase::bLogOutput( ) )   return( false );

  // Copy the log file to the results file
  os1 = roGetLogFile( ).GetFullPath( );
  os2 = roGetResultsFile( ).GetFullPath( );
  if( ! wxCopyFile( os1, os2 ) ) return( false );

  return( true );
}

//**************************************************************************************************
// Format the results file. All this function does is aline the data in columns and packs the
// columns together.
//
// Return Values :
//   true  - Success
//   false - Failure

bool  PrcSimEngBase::bFmtResults( void )
{
  wxStringTokenizer   ostk1;
  wxString            os1, os2;
  size_t              szColWd=0;
  size_t              sz1;

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

  // Determine the width of the largest field in the results file
  // First check the column label widths
  ostk1.SetString( m_oFileResults.GetFirstLine( ) );  // Get the column header line
  while( ostk1.HasMoreTokens( ) )
  {
    os1 = ostk1.GetNextToken( );
    sz1 = os1.Length( );
    if( os1[ 0 ] == wxT('#') ) sz1--;  // Ignore the comment specifier character ie. a leading '#'
    if( sz1 > szColWd ) szColWd = sz1;
  }

  // Now check the data field widths
  ostk1.SetString( m_oFileResults.GetNextLine( ) );   // Get the first line of data
  while( ostk1.HasMoreTokens( ) )
  {
    os1 = ostk1.GetNextToken( );
    sz1 = os1.Length( );
    if( os1[ 0 ] == wxT('-') ) sz1--;  // Ignore minus signs because they are not alway present
    if( sz1 > szColWd ) szColWd = sz1;
  }

  // The total column width is made up of the column label and 2 space characters OR the data field
  // and a sign character and 1 space character
  szColWd += 2;

  // Pad the column labels so the columns are spaced evenly and everything lines up
  ostk1.SetString( m_oFileResults.GetFirstLine( ) );  // Get the column header line
  os1.Empty( );
  while( ostk1.HasMoreTokens( ) )
  {
    os1 << ostk1.GetNextToken( );
    if( ! ostk1.HasMoreTokens( ) ) break;             // The last token has been processed
    sz1 = szColWd - (os1.Length( ) % szColWd);
    os1.Append( wxT(' '), sz1+1 );
  }
  m_oFileResults[ m_oFileResults.GetCurrentLine( ) ] = os1;

  // Pad the data fields so the columns are spaced evenly and everything lines up
  for( os1=m_oFileResults.GetNextLine(); !m_oFileResults.Eof(); os1=m_oFileResults.GetNextLine() )
  {
    ostk1.SetString( os1 );

    os1.Empty( );
    while( ostk1.HasMoreTokens( ) )
    {
      os2 = ostk1.GetNextToken( );
      if( os2[ 0 ] != wxT('-') ) os1 << wxT(' ');     // Make allowance for a leading sign character
      os1 << os2;
      if( ! ostk1.HasMoreTokens( ) ) break;           // The last token has been processed
      sz1 = szColWd - (os1.Length( ) % szColWd);
      os1.Append( wxT(' '), sz1 );
    }

    m_oFileResults[ m_oFileResults.GetCurrentLine( ) ] = os1;
  }

  return( true );
}

//**************************************************************************************************
// Print the object attributes.
//
// Argument List :
//   rosPrefix - A prefix to every line displayed (usually just spaces)

void  PrcSimEngBase::Print( const wxString & rosPrefix )
{
  bool      bIsOpen;
  wxString  os1;

  PrcBase::Print( rosPrefix + wxT("PrcBase::") );

  std::cout << rosPrefix.mb_str( ) << "m_eSimEng      : ";
  switch( m_eSimEng )
  {
    case eSIMR_GNUCAP  : std::cout << "eSIMR_GNUCAP"  ; break;  // GNU-Cap
    case eSIMR_NGSPICE : std::cout << "eSIMR_NGSPICE" ; break;  // NG-Spice
    default            : std::cout << "eSIMR_NONE";
  }
  std::cout << '\n';

  std::cout << rosPrefix.mb_str( ) << "m_oNameResults : " << m_oNameResults.GetFullPath( ).mb_str( ) << '\n';

  std::cout << rosPrefix.mb_str( ) << "m_oFileResults :";

  bIsOpen = m_oFileResults.IsOpened( );
  if( ! bIsOpen )
#if wxCHECK_VERSION( 3,0,0 )
    if( m_oNameResults.Exists( ) )
#else
    if( m_oNameResults.FileExists( ) )
#endif
      m_oFileResults.Open( m_oNameResults.GetFullPath( ) );

  if( m_oFileResults.IsOpened( ) )
  {
    if( m_oFileResults.GetLineCount( ) > 0 )
    {
      std::cout << '\n';
      for( os1=m_oFileResults.GetFirstLine(); !m_oFileResults.Eof(); os1=m_oFileResults.GetNextLine() )
        std::cout << rosPrefix.mb_str( ) << "  " << os1.mb_str( ) << '\n';
    }
    else std::cout << " Is empty\n";
  }
  else std::cout << " Doesn't exist\n";

  if( ! bIsOpen )
    if( m_oFileResults.IsOpened( ) )
      m_oFileResults.Close( );
}

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