MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
c81test.cc File Reference
#include "mbconfig.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include "ac/getopt.h"
#include "myassert.h"
#include "mynewmem.h"
#include "aerodata.h"
#include "aerodc81.h"
#include "c81data.h"
Include dependency graph for c81test.cc:

Go to the source code of this file.

Functions

static void usage (int rc)
 
int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 72 of file c81test.cc.

References a, c81_data::ad, c81_data::al, c81_data::am, c81_data_destroy(), c81_data_fc511_read(), c81_data_flip(), c81_data_get_coef(), c81_data_nrel_read(), c81_data_read(), c81_data_read_free_format(), c81_data_write_free_format(), getopt(), c81_data::md, c81_data::ml, c81_data::mm, c81_data::NAD, c81_data::NAL, c81_data::NAM, c81_data::NMD, c81_data::NML, c81_data::NMM, optarg, optind, and usage().

73 {
74  char mode = 'x';
75  char *dump_fname = NULL;
76  bool dump(false);
77  bool flip(false);
78  doublereal alpha;
79  doublereal mach;
80  enum {
81  GOT_NONE = 0x0000U,
82  GOT_ALPHA = 0x0001U,
83  GOT_MACH = 0x0002U,
84  GOT_ALPHAMACH = (GOT_ALPHA|GOT_MACH)
85  };
86  unsigned got = GOT_NONE;
87  char coef = '\0';
88  doublereal tol = 1e-6;
89 
90  while (true) {
91  int opt = getopt(argc, argv, "a:cC:d:fFm:not:");
92 
93  if (opt == EOF) {
94  break;
95  }
96 
97  switch (opt) {
98  case 'a':
99  alpha = atof(optarg);
100  got |= GOT_ALPHA;
101  break;
102 
103  case 'c':
104  mode = 'c';
105  break;
106 
107  case 'C':
108  if (strcasecmp(optarg, "cl") == 0) {
109  coef = 'l';
110 
111  } else if (strcasecmp(optarg, "cd") == 0) {
112  coef = 'd';
113 
114  } else if (strcasecmp(optarg, "cm") == 0) {
115  coef = 'm';
116 
117  } else {
118  std::cerr << "unknown coefficient "
119  "\"" << optarg << "\"" << std::endl;
120  usage(EXIT_FAILURE);
121  }
122  break;
123 
124  case 'd':
125  dump = true;
126  if (optarg && strcmp(optarg, "-") != 0) {
127  dump_fname = optarg;
128  }
129  break;
130 
131  case 'f':
132  mode = 'f';
133  break;
134 
135  case 'F':
136  flip = true;
137  break;
138 
139  case 'm':
140  mach = atof(optarg);
141  got |= GOT_MACH;
142  break;
143 
144  case 'n':
145  case 'o':
146  mode = char(opt);
147  break;
148 
149  case 't':
150  tol = atof(optarg);
151  break;
152 
153  default:
154  usage(EXIT_FAILURE);
155  }
156  }
157 
158  argc -= optind;
159  argv += optind;
160 
161  /* C81 file name is mandatory */
162  if (argc != 1) {
163  usage(EXIT_FAILURE);
164  }
165 
166  /* consistency checks... */
167  if (dump_fname || dump) {
168  if (got) {
169  std::cerr << "\"dump\" incompatible "
170  "with alpha/mach selection"
171  << std::endl;
172  usage(EXIT_FAILURE);
173  }
174 
175  } else {
176  switch (got) {
177  default:
178  case GOT_NONE:
179  case GOT_ALPHA:
180  case GOT_MACH:
181  if (coef == '\0') {
182  std::cerr << "need to select a coefficient "
183  "when alpha and mach "
184  "are not selected altogether"
185  << std::endl;
186  usage(EXIT_FAILURE);
187  }
188  break;
189 
190  case GOT_ALPHAMACH:
191  break;
192  }
193  }
194 
195  /* read C81 database */
196  std::ifstream in(argv[0]);
197  if (!in) {
198  std::cerr << "unable to open file '" << argv[0] << "'"
199  << std::endl;
200  exit(EXIT_FAILURE);
201  }
202 
203  c81_data databuf = {{'\0'}};
204  c81_data* data = &databuf;
205 
206  int rc;
207 
208  int ff = 0;
209  switch (mode) {
210  case 'x':
211  rc = c81_data_read(in, data, tol, &ff);
212  break;
213 
214  case 'c':
215  rc = c81_data_read(in, data, tol, 0);
216  break;
217 
218  case 'f':
219  rc = c81_data_read_free_format(in, data, tol);
220  break;
221 
222  case 'n':
223  rc = c81_data_nrel_read(in, data, tol);
224  break;
225 
226  case 'o':
227  rc = c81_data_fc511_read(in, data, tol);
228  break;
229 
230  default:
231  exit(EXIT_FAILURE);
232  }
233 
234  in.close();
235 
236  if (rc) {
237  std::cerr << "unable to read c81 data from file "
238  "\"" << argv[0] << "\"" << std::endl;
239  exit(EXIT_FAILURE);
240  }
241 
242  if (flip) {
243  c81_data_flip(data);
244  }
245 
246  if (dump_fname) {
247  std::ofstream out(dump_fname);
248  c81_data_write_free_format(out, data);
249  out.close();
250  c81_data_destroy(data);
251  exit(EXIT_SUCCESS);
252 
253  } else if (dump) {
254  c81_data_write_free_format(std::cout, data);
255  c81_data_destroy(data);
256  exit(EXIT_SUCCESS);
257  }
258 
259  if ((got & GOT_ALPHAMACH) == GOT_ALPHAMACH) {
260  switch (coef) {
261  case 'l':
262  std::cout << c81_data_get_coef(data->NML, data->ml,
263  data->NAL, data->al, alpha, mach)
264  << std::endl;
265  break;
266 
267  case 'd':
268  std::cout << c81_data_get_coef(data->NMD, data->md,
269  data->NAD, data->ad, alpha, mach)
270  << std::endl;
271  break;
272 
273  case 'm':
274  std::cout << c81_data_get_coef(data->NMM, data->mm,
275  data->NAM, data->am, alpha, mach)
276  << std::endl;
277  break;
278 
279  case '\0':
280  std::cout
281  << "alpha: " << alpha << std::endl
282  << "mach: " << mach << std::endl
283  << "cl: " << c81_data_get_coef(data->NML, data->ml,
284  data->NAL, data->al, alpha, mach)
285  << std::endl
286  << "cd: " << c81_data_get_coef(data->NMD, data->md,
287  data->NAD, data->ad, alpha, mach)
288  << std::endl
289  << "cm: " << c81_data_get_coef(data->NMM, data->mm,
290  data->NAM, data->am, alpha, mach)
291  << std::endl;
292  break;
293  }
294 
295  } else if (got & GOT_ALPHAMACH) {
296  int NM = 0, NA = 0;
297  double *m = 0, *a = 0;
298 
299  switch (coef) {
300  case 'l':
301  NM = data->NML;
302  NA = data->NAL;
303  m = data->ml;
304  a = data->al;
305  break;
306 
307  case 'd':
308  NM = data->NMD;
309  NA = data->NAD;
310  m = data->md;
311  a = data->ad;
312  break;
313 
314  case 'm':
315  NM = data->NMM;
316  NA = data->NAM;
317  m = data->mm;
318  a = data->am;
319  break;
320 
321  case '\0':
322  /* TODO */
323  break;
324  }
325 
326  if (m) {
327  switch (got & GOT_ALPHAMACH) {
328  case GOT_ALPHA:
329  for (int i = 0; i < NM; i++) {
330  std::cout
331  << m[i] << " "
332  << c81_data_get_coef(NM, m,
333  NA, a, alpha, m[i])
334  << std::endl;
335  }
336  break;
337 
338  case GOT_MACH:
339  for (int i = 0; i < NA; i++) {
340  std::cout
341  << a[i] << " "
342  << c81_data_get_coef(NM, m,
343  NA, a, a[i], mach)
344  << std::endl;
345  }
346  break;
347  }
348  }
349 
350  } else {
351  std::cerr << "don't know what to do..." << std::endl;
352  c81_data_destroy(data);
353  usage(EXIT_FAILURE);
354  }
355 
356  c81_data_destroy(data);
357 
358  return 0;
359 }
int NMD
Definition: aerodc81.h:160
int NAM
Definition: aerodc81.h:166
int optind
Definition: getopt.c:72
void c81_data_destroy(c81_data *data)
Definition: c81data.cc:276
int NMM
Definition: aerodc81.h:165
int c81_data_write_free_format(std::ostream &out, c81_data *data)
Definition: c81data.cc:1065
int c81_data_flip(c81_data *data)
Definition: c81data.cc:1286
int NML
Definition: aerodc81.h:146
doublereal * mm
Definition: aerodc81.h:167
int c81_data_read_free_format(std::istream &in, c81_data *data, const doublereal dcltol)
Definition: c81data.cc:961
int c81_data_read(std::istream &in, c81_data *data, const doublereal dcltol, int *ff)
Definition: c81data.cc:292
int NAL
Definition: aerodc81.h:147
static void usage(int rc)
Definition: c81test.cc:47
doublereal * ad
Definition: aerodc81.h:163
doublereal c81_data_get_coef(int nm, doublereal *m, int na, doublereal *a, doublereal alpha, doublereal mach)
Definition: aerodc81.c:90
int c81_data_fc511_read(std::istream &in, c81_data *data, const doublereal dcltol)
Definition: c81data.cc:810
int getopt(int argc, char *const argv[], const char *opts)
Definition: getopt.c:93
doublereal * am
Definition: aerodc81.h:168
int c81_data_nrel_read(std::istream &in, c81_data *data, const doublereal dcltol)
Definition: c81data.cc:874
doublereal * al
Definition: aerodc81.h:149
static const doublereal a
Definition: hfluid_.h:289
char * optarg
Definition: getopt.c:74
double doublereal
Definition: colamd.c:52
doublereal * md
Definition: aerodc81.h:162
doublereal * ml
Definition: aerodc81.h:148
int NAD
Definition: aerodc81.h:161

Here is the call graph for this function:

static void usage ( int  rc)
static

Definition at line 47 of file c81test.cc.

Referenced by main().

48 {
49  std::cerr <<
50 "usage: c81test [<options>] <file>\n"
51 "\n"
52 "options:\n"
53 "\t-a <alpha>\t" "dump coefficients for angle of attack <alpha>\n"
54 "\t-c\t\t" "interpret <file> as traditional c81 format (default)\n"
55 "\t-C <coef>\t" "only dump coefficient <coef> (must be cl, cd or cm)\n"
56 "\t-d {<dump>|-}\t" "dump contents (to optional file <dump>, if given)\n"
57 "\t-f\t\t" "interpret <file> as free format\n"
58 "\t-F\t\t" "\"flip\" coefficients\n"
59 "\t-m <mach>\t" "dump coefficients for Mach number <mach>\n"
60 "\t-n\t\t" "interpret <file> as NREL format\n"
61 "\t-o\t\t" "interpret <file> as fc511 format\n"
62 "\t-t <tolerance>\t" "use <tolerance> to determine end of linearity\n"
63  << std::endl;
64 
65  exit(rc);
66 }