MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
test_modalext_edge.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <signal.h>
#include <errno.h>
#include "mbsleep.h"
Include dependency graph for test_modalext_edge.c:

Go to the source code of this file.

Enumerations

enum  { RM, MR }
 

Functions

static void sh (int signum)
 
static const char * cmd2str (int cmd)
 
static int check_flag (const char *flag, int sleeptime)
 
static int put_flag (const char *flag, int cmd)
 
static int put_rdata (const char *rdata, double fm[6])
 
static int put_mdata (const char *mdata, int modes, double *fg)
 
static int do_rigid0 (const char *rflag, const char *rdata, double *fm)
 
static int do_modal0 (const char *mflag, const char *mdata, int modes, double **fgp)
 
int do_rigid (const char *rflag, const char *rdata, int niters, int *iterp, int cmd, double *fm)
 
int do_modal (const char *mflag, const char *mdata, int niters, int *iterp, int cmd, int modes, double *fg)
 
void usage (void)
 
int main (int argc, char *argv[])
 

Variables

volatile sig_atomic_t keep_going = 1
 
int do_rename
 
int sleeptime = 1
 
mbsleep_t mbt
 
int verbose
 
enum { ... }  order = RM
 

Enumeration Type Documentation

anonymous enum
Enumerator
RM 
MR 

Definition at line 48 of file test_modalext_edge.c.

48  {
49  RM,
50  MR
51 } order = RM;
enum @55 order

Function Documentation

static int check_flag ( const char *  flag,
int  sleeptime 
)
static

Definition at line 87 of file test_modalext_edge.c.

References buf, c, cmd2str(), mbsleep(), and mbt.

Referenced by do_modal(), and do_rigid().

88 {
89  int rc;
90 
91  while (1) {
92  char buf[BUFSIZ];
93  FILE *f;
94  char c = ' ';
95 
96  f = fopen(flag, "r");
97  if (f == NULL && errno == ENOENT) {
98  fprintf(stderr, "test_modalext_edge: file \"%s\" missing\n", flag);
99  return 1;
100  }
101 
102  fgets(buf, sizeof(buf), f);
103  if (strcmp(buf, "UPDATE,N,0,0,1\n") != 0) {
104  size_t len = strlen(buf);
105  buf[len - 1] = '\0';
106  fprintf(stderr, "test_modalext_edge: expecting \"UPDATE,N,0,0,1\", got \"%s\" from file \"%s\"\n", buf, flag);
107  fclose(f);
108  return -1;
109  }
110  fgets(buf, sizeof(buf), f);
111  if (strcmp(buf, "FLAG,I,1,1,0\n") != 0) {
112  size_t len = strlen(buf);
113  buf[len - 1] = '\0';
114  fprintf(stderr, "test_modalext_edge: expecting \"FLAG,I,1,1,0\", got \"%s\" from file \"%s\"\n", buf, flag);
115  fclose(f);
116  return -1;
117  }
118  rc = fread((void *)&c, 1, 1, f);
119  fclose(f);
120  if (rc == 1) {
121  fprintf(stderr, "test_modalext_edge: got %c (%s) from file \"%s\"\n", c, cmd2str(c - '0'), flag);
122 
123  switch (c) {
124  case '0':
125  case '1':
126  case '3':
127  return 0;
128 
129  case '5':
130  return 1;
131 
132  default:
133  break;
134  }
135  }
136 
137  if (sleeptime) {
138  fprintf(stderr, "test_modalext_edge: sleeping %d s\n", sleeptime);
139  mbsleep(&mbt);
140  }
141  }
142 
143  return 0;
144 }
mbsleep_t mbt
long int flag
Definition: mbdyn.h:43
int mbsleep(const mbsleep_t *t)
Definition: mbsleep.c:90
static const char * cmd2str(int cmd)
static std::stack< cleanup * > c
Definition: cleanup.cc:59
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333

Here is the call graph for this function:

static const char* cmd2str ( int  cmd)
static

Definition at line 60 of file test_modalext_edge.c.

Referenced by check_flag().

61 {
62  switch (cmd) {
63  case 0:
64  return "EDGE is initializing; MBDyn waits";
65 
66  case 1:
67  return "EDGE is busy; MBDyn waits";
68 
69  case 2:
70  return "EDGE waits (is ready to read kinematics); MBDyn iterates";
71 
72  case 3:
73  return "EDGE is computing; MBDyn waits before reading forces";
74 
75  case 4:
76  return "EDGE converged; MBDyn advances one step";
77 
78  case 5:
79  return "EDGE wants to end simulation";
80 
81  default:
82  return "unknown";
83  }
84 }
int do_modal ( const char *  mflag,
const char *  mdata,
int  niters,
int iterp,
int  cmd,
int  modes,
double *  fg 
)

Definition at line 359 of file test_modalext_edge.c.

References buf, check_flag(), put_flag(), and put_mdata().

Referenced by main().

362 {
363  /* modal */
364  if (mflag != NULL) {
365  if (check_flag(mflag, sleeptime)) {
366  *iterp = niters;
367  keep_going = 0;
368  return 0;
369  }
370 
371  if (verbose) {
372  char buf[BUFSIZ];
373  FILE *f;
374 
375  f = fopen(mdata, "r");
376  if (f == NULL) {
377  int save_errno = errno;
378 
379  fprintf(stderr, "unable to open modal data file \"%s\" (%d: %s)\n",
380  mdata, save_errno, strerror(save_errno));
381  exit(EXIT_FAILURE);
382  }
383 
384  while (fgets(buf, sizeof(buf), f) != NULL) {
385  fprintf(stderr, ">> mdata:%s", buf);
386  }
387 
388  fclose(f);
389  }
390 
391  put_mdata(mdata, modes, fg);
392  put_flag(mflag, cmd);
393  }
394 
395  return 0;
396 }
int verbose
static int put_mdata(const char *mdata, int modes, double *fg)
static int put_flag(const char *flag, int cmd)
volatile sig_atomic_t keep_going
int sleeptime
static int check_flag(const char *flag, int sleeptime)
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333

Here is the call graph for this function:

static int do_modal0 ( const char *  mflag,
const char *  mdata,
int  modes,
double **  fgp 
)
static

Definition at line 285 of file test_modalext_edge.c.

References put_flag(), and put_mdata().

Referenced by main().

286 {
287  if (mflag != NULL) {
288  FILE *f = NULL;
289  int i;
290 
291  f = fopen(mflag, "r");
292  if (f == NULL) {
293  int save_errno = errno;
294  if (save_errno == ENOENT) {
295  put_flag(mflag, 0);
296 
297  } else {
298  fprintf(stderr, "unable to open modal flag file \"%s\" (%d: %s)\n",
299  mflag, save_errno, strerror(save_errno));
300  exit(EXIT_FAILURE);
301  }
302 
303  } else {
304  fclose(f);
305  }
306 
307  *fgp = (double *)malloc(sizeof(double)*modes);
308  for (i = 0; i < modes; i++) {
309  (*fgp)[i] = ((double)i)/10.0;
310  }
311 
312  put_mdata(mdata, modes, *fgp);
313  }
314 
315  return 0;
316 }
static int put_mdata(const char *mdata, int modes, double *fg)
static int put_flag(const char *flag, int cmd)

Here is the call graph for this function:

int do_rigid ( const char *  rflag,
const char *  rdata,
int  niters,
int iterp,
int  cmd,
double *  fm 
)

Definition at line 319 of file test_modalext_edge.c.

References buf, check_flag(), put_flag(), and put_rdata().

Referenced by main().

322 {
323  /* rigid */
324  if (rflag != NULL) {
325  if (check_flag(rflag, sleeptime)) {
326  *iterp = niters;
327  keep_going = 0;
328  return 0;
329  }
330 
331  if (verbose) {
332  char buf[BUFSIZ];
333  FILE *f;
334 
335  f = fopen(rdata, "r");
336  if (f == NULL) {
337  int save_errno = errno;
338 
339  fprintf(stderr, "unable to open rigid data file \"%s\" (%d: %s)\n",
340  rdata, save_errno, strerror(save_errno));
341  exit(EXIT_FAILURE);
342  }
343 
344  while (fgets(buf, sizeof(buf), f) != NULL) {
345  fprintf(stderr, ">> rdata:%s", buf);
346  }
347 
348  fclose(f);
349  }
350 
351  put_rdata(rdata, fm);
352  put_flag(rflag, cmd);
353  }
354 
355  return 0;
356 }
int verbose
static int put_flag(const char *flag, int cmd)
static int put_rdata(const char *rdata, double fm[6])
volatile sig_atomic_t keep_going
int sleeptime
static int check_flag(const char *flag, int sleeptime)
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333

Here is the call graph for this function:

static int do_rigid0 ( const char *  rflag,
const char *  rdata,
double *  fm 
)
static

Definition at line 252 of file test_modalext_edge.c.

References put_flag(), and put_rdata().

Referenced by main().

253 {
254  if (rflag != NULL) {
255  FILE *f = NULL;
256  int i;
257 
258  f = fopen(rflag, "r");
259  if (f == NULL) {
260  int save_errno = errno;
261  if (save_errno == ENOENT) {
262  put_flag(rflag, 0);
263 
264  } else {
265  fprintf(stderr, "unable to open rigid flag file \"%s\" (%d: %s)\n",
266  rflag, save_errno, strerror(save_errno));
267  exit(EXIT_FAILURE);
268  }
269 
270  } else {
271  fclose(f);
272  }
273 
274  for (i = 0; i < 6; i++) {
275  fm[i] = 0.1*(i + 1);
276  }
277 
278  put_rdata(rdata, fm);
279  }
280 
281  return 0;
282 }
static int put_flag(const char *flag, int cmd)
static int put_rdata(const char *rdata, double fm[6])

Here is the call graph for this function:

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

Definition at line 415 of file test_modalext_edge.c.

References do_modal(), do_modal0(), do_rigid(), do_rigid0(), getopt(), iters, iters_random, mbsleep_init(), mbt, MR, optarg, order, put_flag(), RM, sh(), steps, and usage().

416 {
417  char *rflag = NULL;
418  char *rdata = NULL;
419  char *mflag = NULL;
420  char *mdata = NULL;
421  int iters = 1;
422  int iters_random = 0;
423  unsigned steps;
424  int modes = 5;
425  double fm[6];
426  double *fg = NULL;
427 
428  while (1) {
429  int opt = getopt(argc, argv, "c:m:M:no:r:s:v");
430 
431  if (opt == EOF) {
432  break;
433  }
434 
435  switch (opt) {
436  case 'c':
437  if (strncasecmp(optarg, "random:", sizeof("random:") -1) == 0) {
438  iters_random = 1;
439  iters = atoi(&optarg[sizeof("random:") -1]);
440 
441  } else {
442  iters = atoi(optarg);
443  fprintf(stderr, "iterations: %d\n", iters);
444  }
445  if (iters < 1) {
446  fprintf(stderr, "test_modalext_edge: "
447  "invalid sleep time %s\n",
448  optarg);
449  usage();
450  }
451  break;
452 
453  case 'm':
454  if (strncasecmp(optarg, "flag=", sizeof("flag=") - 1) == 0) {
455  mflag = &optarg[sizeof("flag=") - 1];
456 
457  } else if (strncasecmp(optarg, "data=", sizeof("data=") - 1) == 0) {
458  mdata = &optarg[sizeof("data=") - 1];
459 
460  } else {
461  fprintf(stderr, "test_modalext_edge: "
462  "unknown modal file \"%s\"\n",
463  optarg);
464  usage();
465  }
466  break;
467 
468  case 'M':
469  modes = atoi(optarg);
470  if (modes <= 0) {
471  fprintf(stderr, "test_modalext_edge: "
472  "invalid mode number %s\n",
473  optarg);
474  usage();
475  }
476  break;
477 
478  case 'n':
479 #ifdef HAVE_MKSTEMP
480  do_rename++;
481 #else // ! HAVE_MKSTEMP
482  fprintf(stderr, "test_modalext_edge: "
483  "'-n' meaningless\n");
484 #endif // ! HAVE_MKSTEMP
485  break;
486 
487  case 'o':
488  if (strcmp(optarg, "rm") == 0) {
489  order = RM;
490 
491  } else if (strcmp(optarg, "mr") == 0) {
492  order = MR;
493 
494  } else {
495  fprintf(stderr, "test_modalext_edge: "
496  "invalid order \"%s\"\n",
497  optarg);
498  usage();
499  }
500  break;
501 
502  case 'r':
503  if (strncasecmp(optarg, "flag=", sizeof("flag=") - 1) == 0) {
504  rflag = &optarg[sizeof("flag=") - 1];
505 
506  } else if (strncasecmp(optarg, "data=", sizeof("data=") - 1) == 0) {
507  rdata = &optarg[sizeof("data=") - 1];
508 
509  } else {
510  fprintf(stderr, "test_modalext_edge: "
511  "unknown rigid file \"%s\"\n",
512  optarg);
513  usage();
514  }
515  break;
516 
517  case 's':
518  sleeptime = atoi(optarg);
519  if (sleeptime < 0) {
520  fprintf(stderr, "test_modalext_edge: "
521  "invalid iters %s\n",
522  optarg);
523  usage();
524  }
526  break;
527 
528  case 'v':
529  verbose++;
530  break;
531 
532  default:
533  usage();
534  }
535  }
536 
537  if (mflag == NULL && mdata != NULL) {
538  fprintf(stderr, "test_modalext_edge: "
539  "need modal flag file "
540  "along with modal data file \"%s\"\n",
541  mdata);
542  usage();
543  }
544 
545  if (mflag != NULL && mdata == NULL) {
546  fprintf(stderr, "test_modalext_edge: "
547  "need modal data file "
548  "along with modal flag file \"%s\"\n",
549  mflag);
550  usage();
551  }
552 
553  if (rflag == NULL && rdata != NULL) {
554  fprintf(stderr, "test_modalext_edge: "
555  "need rigid flag file "
556  "along with rigid data file \"%s\"\n",
557  rdata);
558  usage();
559  }
560 
561  if (rflag != NULL && rdata == NULL) {
562  fprintf(stderr, "test_modalext_edge: "
563  "need rigid data file "
564  "along with rigid flag file \"%s\"\n",
565  rflag);
566  usage();
567  }
568 
569  if (mflag == NULL && rflag == NULL) {
570  fprintf(stderr, "test_modalext_edge: "
571  "need at least rigid or modal files\n");
572  usage();
573  }
574 
575  signal(SIGTERM, sh);
576  signal(SIGINT, sh);
577 
578  switch (order) {
579  case RM:
580  do_rigid0(rflag, rdata, fm);
581  do_modal0(mflag, mdata, modes, &fg);
582  break;
583 
584  case MR:
585  do_modal0(mflag, mdata, modes, &fg);
586  do_rigid0(rflag, rdata, fm);
587  break;
588  }
589 
590  for (steps = 0; keep_going > 0; steps++) {
591  int iter;
592  int niters;
593 
594  if (iters_random) {
595  niters = rand() % iters + 1;
596  fprintf(stderr, " iterations within this iter: %d\n", niters);
597 
598  } else {
599  niters = iters;
600  }
601 
602  for (iter = 0; iter < niters; iter++) {
603  int cmd = 2;
604 
605  if (iter == niters - 1) {
606  cmd = 4;
607  }
608 
609  switch (order) {
610  case RM:
611  do_rigid(rflag, rdata, niters, &iter, cmd, fm);
612  do_modal(mflag, mdata, niters, &iter, cmd, modes, fg);
613  break;
614 
615  case MR:
616  do_modal(mflag, mdata, niters, &iter, cmd, modes, fg);
617  do_rigid(rflag, rdata, niters, &iter, cmd, fm);
618  break;
619  }
620 
621  }
622  }
623 
624  if (rflag != NULL) {
625  put_flag(rflag, 5);
626  }
627 
628  if (mflag != NULL) {
629  put_flag(mflag, 5);
630  }
631 
632  return 0;
633 }
mbsleep_t mbt
static int iters
int do_rigid(const char *rflag, const char *rdata, int niters, int *iterp, int cmd, double *fm)
int verbose
static int put_flag(const char *flag, int cmd)
static int iters_random
int do_modal(const char *mflag, const char *mdata, int niters, int *iterp, int cmd, int modes, double *fg)
void usage(void)
enum @55 order
int do_rename
static void sh(int signum)
static int do_modal0(const char *mflag, const char *mdata, int modes, double **fgp)
mbsleep_t mbsleep_init(long t)
Definition: mbsleep.c:38
volatile sig_atomic_t keep_going
int getopt(int argc, char *const argv[], const char *opts)
Definition: getopt.c:93
int sleeptime
static int do_rigid0(const char *rflag, const char *rdata, double *fm)
static unsigned steps
char * optarg
Definition: getopt.c:74

Here is the call graph for this function:

static int put_flag ( const char *  flag,
int  cmd 
)
static

Definition at line 147 of file test_modalext_edge.c.

References mbsleep(), and mbt.

Referenced by do_modal(), do_modal0(), do_rigid(), do_rigid0(), and main().

148 {
149  FILE *f;
150  char ftmpname[] = "mbedgeXXXXXX";
151 
152 #ifdef HAVE_MKSTEMP
153  if (do_rename) {
154  int filedes = mkstemp(ftmpname);
155  f = fdopen(filedes, "w");
156 
157  } else
158 #endif // HAVE_MKSTEMP
159  {
160  f = fopen(flag, "w");
161  }
162 
163  if (f == NULL) {
164  int save_errno = errno;
165  fprintf(stderr, "unable to open flag file \"%s\" for writing (%d: %s)\n",
166  flag, save_errno, strerror(save_errno));
167  exit(EXIT_FAILURE);
168  }
169 
170  fprintf(f, "UPDATE,N,0,0,1\n");
171  fprintf(f, "FLAG,I,1,1,0\n");
172  fprintf(f, "%d", cmd);
173  fclose(f);
174 
175  if (do_rename) {
176 retry:;
177  if (rename(ftmpname, flag) == -1) {
178  switch (errno) {
179  case EBUSY:
180  mbsleep(&mbt);
181  goto retry;
182 
183  default: {
184  int save_errno = errno;
185  fprintf(stderr, "unable to rename flag file \"%s\" (errno=%d: %s)\n",
186  flag, save_errno, strerror(save_errno));
187  exit(EXIT_FAILURE);
188  }
189  }
190  }
191  }
192 
193  return 0;
194 }
mbsleep_t mbt
long int flag
Definition: mbdyn.h:43
int mbsleep(const mbsleep_t *t)
Definition: mbsleep.c:90
int do_rename

Here is the call graph for this function:

static int put_mdata ( const char *  mdata,
int  modes,
double *  fg 
)
static

Definition at line 224 of file test_modalext_edge.c.

Referenced by do_modal(), and do_modal0().

225 {
226  FILE *f;
227  int i;
228 
229  f = fopen(mdata, "w");
230  if (f == NULL) {
231  int save_errno = errno;
232 
233  fprintf(stderr, "unable to open modal data file \"%s\" (%d: %s)\n",
234  mdata, save_errno, strerror(save_errno));
235  exit(EXIT_FAILURE);
236  }
237 
238  fprintf(f,
239  "* modal forces\n"
240  "modal_force_flow,R,%d,1,0\n",
241  modes);
242  for (i = 0; i < modes; i++) {
243  fprintf(f, "%e ", fg[i]);
244  }
245  fputc('\n', f);
246  fclose(f);
247 
248  return 0;
249 }
static int put_rdata ( const char *  rdata,
double  fm[6] 
)
static

Definition at line 197 of file test_modalext_edge.c.

Referenced by do_rigid(), and do_rigid0().

198 {
199  FILE *f;
200  int i;
201 
202  f = fopen(rdata, "w");
203  if (f == NULL) {
204  int save_errno = errno;
205 
206  fprintf(stderr, "unable to open rigid data file \"%s\" (%d: %s)\n",
207  rdata, save_errno, strerror(save_errno));
208  exit(EXIT_FAILURE);
209  }
210 
211  fprintf(f,
212  "* rigid-body forces and moments\n"
213  "body_forces,R,1,6,0\n");
214  for (i = 0; i < 6; i++) {
215  fprintf(f, "%e ", fm[i]);
216  }
217  fputc('\n', f);
218  fclose(f);
219 
220  return 0;
221 }
static void sh ( int  signum)
static

Definition at line 54 of file test_modalext_edge.c.

Referenced by main().

55 {
56  keep_going = 0;
57  signal(signum, SIG_DFL);
58 }
volatile sig_atomic_t keep_going
void usage ( void  )

Definition at line 399 of file test_modalext_edge.c.

Referenced by main().

400 {
401  fprintf(stderr,
402  "usage: test_modalext_edge [options]\n"
403  "\t-c [random:]<c>\t\tnumber of iterations\n"
404  "\t-m [flag|data]=<file>\tmodal file names (set both)\n"
405  "\t-M <modes>\t\tmodes number\n"
406  "\t-n\t\t\tuse \"rename\" when writing flag files\n"
407  "\t-o {rm|mr}\tprocess rigid,modal (rm) or modal,rigid (mr)\n"
408  "\t-r [flag|data]=<file>\trigid-body file names (set both)\n"
409  "\t-s <sleeptime>\t\tsleep time between tries\n"
410  "\t-v\t\t\tverbose\n" );
411  exit(EXIT_FAILURE);
412 }

Variable Documentation

int do_rename

Definition at line 44 of file test_modalext_edge.c.

volatile sig_atomic_t keep_going = 1

Definition at line 42 of file test_modalext_edge.c.

mbsleep_t mbt

Definition at line 46 of file test_modalext_edge.c.

Referenced by check_flag(), main(), and put_flag().

int sleeptime = 1

Definition at line 45 of file test_modalext_edge.c.

Referenced by main().

int verbose

Definition at line 47 of file test_modalext_edge.c.