MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
test_modalext.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/utils/test_modalext.cc,v 1.12 2017/01/12 15:10:28 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #include <iostream>
33 #include <fstream>
34 #include <cstdlib>
35 #include <cstdio>
36 #include <cstring>
37 #include <cerrno>
38 
39 #include <unistd.h>
40 #include <signal.h>
41 
42 #include "mbsleep.h"
43 
44 /* test tool specific stuff */
45 static volatile sig_atomic_t keep_going = 1;
46 
47 static void
48 sh(int signum)
49 {
50  keep_going = 0;
51  signal(signum, SIG_DFL);
52 }
53 
54 void
55 usage(void)
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 }
68 
69 int
70 main(int argc, char *argv[])
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 main(int argc, char *argv[])
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