MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
test_modalext.cc File Reference
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <unistd.h>
#include <signal.h>
#include "mbsleep.h"
Include dependency graph for test_modalext.cc:

Go to the source code of this file.

Functions

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

Variables

static volatile sig_atomic_t keep_going = 1
 

Function Documentation

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

Definition at line 70 of file test_modalext.cc.

References data_and_next, done, getopt(), iters, iters_random, mbsleep(), mbsleep_init(), optarg, R, refnode, sh(), sleeptime, steps, usage(), and verbose.

71 {
72  int sleeptime = 1;
73  int iters = 1;
74  int iters_random = 0;
75  unsigned steps;
76 
77  std::string fname_in, fname_out, tmpfname_out;
78  unsigned modes = 0;
79  unsigned refnode = 0;
80  char verbose = 0;
81  char data_and_next = 0;
82 
83  double x[3], R[3][3], v[3], w[3], f[3], m[3];
84  double *q = 0, *qp = 0, *p = 0;
85 
86  while (1) {
87  int opt = getopt(argc, argv, "c:i:M:o:rs:vx");
88 
89  if (opt == EOF) {
90  break;
91  }
92 
93  switch (opt) {
94  case 'c':
95  if (strncasecmp(optarg, "random:", sizeof("random:") -1) == 0) {
96  iters_random = 1;
97  iters = atoi(&optarg[sizeof("random:") -1]);
98 
99  } else {
100  iters = atoi(optarg);
101  printf("iterations: %d\n", iters);
102  }
103  if (iters < 1) {
104  fprintf(stderr, "testedge: "
105  "invalid iterations %s\n",
106  optarg);
107  usage();
108  }
109  break;
110 
111  case 'i':
112  fname_in = optarg;
113  break;
114 
115  case 'M':
116  modes = atoi(optarg);
117  if (modes <= 0) {
118  fprintf(stderr, "testedge: "
119  "invalid mode number %s\n",
120  optarg);
121  usage();
122  }
123  break;
124 
125  case 'o':
126  fname_out = optarg;
127  break;
128 
129  case 'r':
130  refnode = 1;
131  break;
132 
133  case 's':
134  sleeptime = atoi(optarg);
135  if (sleeptime < 0) {
136  fprintf(stderr, "testedge: "
137  "invalid iters %s\n",
138  optarg);
139  usage();
140  }
141  break;
142 
143  case 'v':
144  verbose = 1;
145  break;
146 
147  case 'x':
148  data_and_next = 1;
149  break;
150 
151  default:
152  usage();
153  }
154  }
155 
156  if (fname_in.empty() || fname_out.empty()) {
157  usage();
158  }
159 
160  tmpfname_out = fname_out + ".tmp";
161 
162  if (modes > 0) {
163  q = new double[3*modes];
164  qp = &q[modes];
165  p = &qp[modes];
166  }
167 
168  signal(SIGTERM, sh);
169  signal(SIGINT, sh);
170 
171  for (steps = 0; keep_going > 0; steps++) {
172  int iter;
173  int niters;
174 
175  if (iters_random) {
176  niters = rand() % iters + 1;
177  printf(" iterations within this iter: %d\n", niters);
178 
179  } else {
180  niters = iters;
181  }
182 
183  for (iter = 0; iter < niters; iter++) {
184  std::ifstream fin;
185 
186 retry_1:;
187  fin.open(fname_in.c_str());
188  if (!fin) {
189  if (!keep_going) {
190  printf("interrupted\n");
191  goto done;
192  }
193 
194  mbsleep_t t = mbsleep_init(1);
195  mbsleep(&t);
196  goto retry_1;
197  }
198 
199  /* get motion */
200  if (refnode) {
201  fin
202  >> x[0] >> x[1] >> x[2]
203  >> R[0][0] >> R[0][1] >> R[0][2]
204  >> R[1][0] >> R[1][1] >> R[1][2]
205  >> R[2][0] >> R[2][1] >> R[2][2]
206  >> v[0] >> v[1] >> v[2]
207  >> w[0] >> w[1] >> w[2];
208  if (!fin) {
209  goto done;
210  }
211 
212  fprintf(stdout, "x={%+16.8e,%+16.8e,%+16.8e}\n", x[0], x[1], x[2]);
213  fprintf(stdout, "R={{%+16.8e,%+16.8e,%+16.8e};\n", R[0][0], R[0][1], R[0][2]);
214  fprintf(stdout, " {%+16.8e,%+16.8e,%+16.8e};\n", R[1][0], R[1][1], R[1][2]);
215  fprintf(stdout, " {%+16.8e,%+16.8e,%+16.8e}};\n", R[2][0], R[2][1], R[2][2]);
216  fprintf(stdout, "v={%+16.8e,%+16.8e,%+16.8e}\n", v[0], v[1], v[2]);
217  fprintf(stdout, "w={%+16.8e,%+16.8e,%+16.8e}\n", w[0], w[1], w[2]);
218  }
219 
220  if (modes) {
221  for (unsigned m = 0; m < modes; m++) {
222  fin >> q[m] >> qp[m];
223  if (!fin) {
224  goto done;
225  }
226  fprintf(stdout, "mode #%d: %+16.8e %+16.8e\n", m, q[m], qp[m]);
227  }
228  }
229 
230  fin.close();
231  unlink(fname_in.c_str());
232 
233  if (sleeptime) {
234  mbsleep_t t = mbsleep_init(sleeptime);
235  mbsleep(&t);
236  }
237 
238  /* set forces */
239  std::ofstream fout(tmpfname_out.c_str());
240 
241  if (refnode) {
242  f[0] = 1.;
243  f[1] = 2.;
244  f[2] = 3.;
245 
246  m[0] = 4.;
247  m[1] = 5.;
248  m[2] = 6.;
249 
250  fout
251  << f[0] << ' ' << f[1] << ' ' << f[2] << std::endl
252  << m[0] << ' ' << m[1] << ' ' << m[2] << std::endl;
253  }
254 
255  if (modes) {
256  for (unsigned m = 0; m < modes; m++) {
257  p[m] = (double)(m + 1);
258 
259  fout << p[m] << std::endl;
260  }
261  }
262 
263  rename(tmpfname_out.c_str(), fname_out.c_str());
264  }
265  }
266 
267 done:;
268 
269  if (modes && q) {
270  delete[] q;
271  }
272 
273  return 0;
274 }
static void sh(int signum)
static int iters
int mbsleep(const mbsleep_t *t)
Definition: mbsleep.c:90
unsigned long mbsleep_t
Definition: mbsleep.h:51
static int iters_random
mbsleep_t mbsleep_init(long t)
Definition: mbsleep.c:38
static int data_and_next
int getopt(int argc, char *const argv[], const char *opts)
Definition: getopt.c:93
static int verbose
Definition: ann_tr.c:51
int sleeptime
static unsigned done
Definition: gust.cc:209
static unsigned steps
char * optarg
Definition: getopt.c:74
static volatile sig_atomic_t keep_going
void usage(void)
static int refnode
Mat3x3 R

Here is the call graph for this function:

static void sh ( int  signum)
static

Definition at line 48 of file test_modalext.cc.

Referenced by main().

49 {
50  keep_going = 0;
51  signal(signum, SIG_DFL);
52 }
static volatile sig_atomic_t keep_going
void usage ( void  )

Definition at line 55 of file test_modalext.cc.

Referenced by main().

56 {
57  fprintf(stderr,
58  "usage: testsocket [options]\n"
59  "\t-c [random:]<c>\t\tnumber of iterations\n"
60  "\t-H <url>\tURL (file://path)\n"
61  "\t-M <modes>\tmodes number\n"
62  "\t-r\t\tuse reference node data\n"
63  "\t-s <sleeptime>\t\tsleep time between tries\n"
64  "\t-v\t\tverbose\n"
65  "\t-x\t\tdata_and_next\n");
66  exit(EXIT_FAILURE);
67 }

Variable Documentation

volatile sig_atomic_t keep_going = 1
static

Definition at line 45 of file test_modalext.cc.