MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
deriv.c File Reference
#include "mbconfig.h"
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
Include dependency graph for deriv.c:

Go to the source code of this file.

Macros

#define BUFSIZE   1024
 

Functions

int get_line (FILE *fin, char *buf, int buf_size)
 
char * get_word (char *buf, char **next)
 
int main (int argn, const char *const argv[])
 

Macro Definition Documentation

#define BUFSIZE   1024

Referenced by main().

Function Documentation

int get_line ( FILE *  fin,
char *  buf,
int  buf_size 
)

Definition at line 38 of file deriv.c.

References buf, and c.

Referenced by main().

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 }
static std::stack< cleanup * > c
Definition: cleanup.cc:59
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333
char* get_word ( char *  buf,
char **  next 
)

Definition at line 57 of file deriv.c.

References buf.

Referenced by main().

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 }
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333
int main ( int  argn,
const char *const  argv[] 
)

Definition at line 91 of file deriv.c.

References buf, BUFSIZE, get_line(), and get_word().

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 }
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 doublereal buf[BUFSIZE]
Definition: discctrl.cc:333

Here is the call graph for this function: