MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
deriv.c
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/utils/deriv.c,v 1.22 2017/01/12 15:10:27 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  *
10  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
11  * via La Masa, 34 - 20156 Milano, Italy
12  * http://www.aero.polimi.it
13  *
14  * Changing this copyright notice is forbidden.
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation (version 2 of the License).
19  *
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29  */
30 
31 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
32 
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <string.h>
37 
38 int get_line(FILE* fin, char* buf, int buf_size)
39 {
40  char* s = buf;
41  int c = 0;
42 
43  while ((c = fgetc(fin)) != '\n') {
44  if (c == EOF) {
45  return 1;
46  }
47  *s++ = c;
48  if (s >= buf+buf_size-1) {
49  return -1;
50  }
51  }
52  *s = '\0';
53 
54  return 0;
55 }
56 
57 char* get_word(char* buf, char** next)
58 {
59  char* s = buf;
60  char* beg = NULL;
61 
62  if (s == NULL) {
63  *next = NULL;
64  return NULL;
65  }
66 
67  while (isspace(*s)) {
68  s++;
69  }
70 
71  if (*s == '\0') {
72  *next = NULL;
73  return NULL;
74  }
75 
76  beg = s;
77  while (!isspace(*s)) {
78  s++;
79  }
80 
81  if (*s == '\0') {
82  *next = NULL;
83  } else {
84  *next = s;
85  }
86 
87  return beg;
88 }
89 
90 
91 int main(int argn, const char* const argv[])
92 {
93  FILE* fin = NULL;
94  int isf = 0;
95  double* pd = NULL;
96  double** pdv = NULL;
97  int* pi = NULL;
98  int ifirst = 0;
99  int icurr = 0;
100  int nrows = 0;
101  int ncols = 0;
102 #define BUFSIZE 1024
103  static char buf[BUFSIZE];
104  char* s = NULL;
105  char* next = NULL;
106  int i = 0;
107  int j = 0;
108 
109  int i1 = 0;
110  int i2 = i1+1;
111  int i3 = i2+1;
112 
113  double dt = 0.;
114  double dt2 = 0.;
115 
116  if (argn == 1 || argn > 3) {
117  fprintf(stderr, "usage: %s <file> <dt>\n", argv[0]);
118  exit(EXIT_FAILURE);
119  } else if (argn == 2) {
120  fin = stdin;
121  dt = atof(argv[1]);
122  } else {
123  if (!strcmp(argv[1], "-")) {
124  fin = stdin;
125  } else {
126  fin = fopen(argv[1], "r");
127  if (fin == NULL) {
128  fprintf(stderr, "%s: file <%s> doesn't exist\n", argv[0], argv[1]);
129  exit(EXIT_FAILURE);
130  }
131  isf = 1;
132  }
133  dt = atof(argv[2]);
134  }
135 
136  if (dt <= 0.) {
137  fprintf(stderr, "%s: illegal dt = %f\n", argv[0], dt);
138  exit(EXIT_FAILURE);
139  }
140  dt2 = 2.*dt;
141 
142  if (get_line(fin, buf, BUFSIZE) == -1) {
143  fprintf(stderr, "%s: line 1 is longer that %d in file <%s>\n",
144  argv[0], BUFSIZE, argv[1]);
145  exit(EXIT_FAILURE);
146  }
147 
148  s = get_word(buf, &next);
149  if (s == NULL) {
150  fprintf(stderr, "%s: unable to read first label in file <%s>\n",
151  argv[0], argv[1]);
152  exit(EXIT_FAILURE);
153  }
154  ifirst = atoi(s);
155 
156  while ((s = get_word(next, &next))) {
157  ncols++;
158  }
159 
160  nrows++;
161  while (1) {
162  if (get_line(fin, buf, BUFSIZE) == -1) {
163  fprintf(stderr, "%s: line %d is longer that %d in file <%s>\n",
164  argv[0], nrows+1, BUFSIZE, argv[1]);
165  exit(EXIT_FAILURE);
166  }
167 
168  if (sscanf(buf, "%d", &icurr) < 1) {
169  fprintf(stderr, "%s: unable to read label %d in file <%s>\n",
170  argv[0], nrows+1, argv[1]);
171  exit(EXIT_FAILURE);
172  }
173  if (icurr == ifirst) {
174  break;
175  }
176  nrows++;
177  }
178 
179  pd = (double*)malloc(sizeof(double)*(ncols*nrows*3));
180  pdv = (double**)malloc(sizeof(double*)*(nrows*3));
181  pi = (int*)malloc(sizeof(int)*nrows);
182 
183  if (pd == NULL || pdv == NULL) {
184  fprintf(stderr, "%s: out of memory?\n", argv[0]);
185  exit(EXIT_FAILURE);
186  }
187 
188  for (i = 3*nrows; i-- > 0; ) {
189  pdv[i] = pd+i*ncols;
190  }
191 
192 
193 
194  rewind(fin);
195 
196  for (i = 0; i < nrows; i++) {
197  get_line(fin, buf, BUFSIZE);
198  next = buf;
199  s = get_word(next, &next);
200  pi[i] = atoi(s);
201  for (j = 0; j < ncols; j++) {
202  s = get_word(next, &next);
203  pdv[i1*nrows+i][j] = atof(s);
204  }
205  }
206 
207  for (i = 0; i < nrows; i++) {
208  get_line(fin, buf, BUFSIZE);
209  next = buf;
210  s = get_word(next, &next);
211  printf("%8d", pi[i]);
212  for (j = 0; j < ncols; j++) {
213  s = get_word(next, &next);
214  pdv[i2*nrows+i][j] = atof(s);
215  printf("%16.8e", (pdv[i2*nrows+i][j]-pdv[i1*nrows+i][j])/dt);
216  }
217  printf("\n");
218  }
219 
220  while (1) {
221  for (i = 0; i < nrows; i++) {
222  if (get_line(fin, buf, BUFSIZE) == 1) {
223  goto last_line;
224  }
225  next = buf;
226  s = get_word(next, &next);
227  printf("%8d", pi[i]);
228  for (j = 0; j < ncols; j++) {
229  s = get_word(next, &next);
230  pdv[i3*nrows+i][j] = atof(s);
231  printf("%16.8e", (pdv[i3*nrows+i][j]-pdv[i1*nrows+i][j])/dt2);
232  }
233  printf("\n");
234  }
235  i1 = (i1+1)%3;
236  i2 = (i2+1)%3;
237  i3 = (i3+1)%3;
238  }
239 
240 last_line:
241 
242  for (i = 0; i < nrows; i++) {
243  printf("%8d", pi[i]);
244  for (j = 0; j < ncols; j++) {
245  printf("%16.8e", (pdv[i2*nrows+i][j]-pdv[i1*nrows+i][j])/dt);
246  }
247  printf("\n");
248  }
249 
250  if (isf) {
251  fclose(fin);
252  }
253 
254  return 0;
255 }
256 
char * get_word(char *buf, char **next)
Definition: deriv.c:57
int get_line(FILE *fin, char *buf, int buf_size)
Definition: deriv.c:38
#define BUFSIZE
static std::stack< cleanup * > c
Definition: cleanup.cc:59
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333
int main(int argn, const char *const argv[])
Definition: deriv.c:91