cuikworldforces.c
Go to the documentation of this file.
1 #include "world.h"
2 #include "basic_algebra.h"
3 #include "error.h"
4 
5 #include <math.h>
6 
63 int main(int argc, char **arg)
64 {
65 
66  if (argc>2)
67  {
68  Tworld world;
69  Tparameters parameters;
70 
71  Tbox box;
72  int token;
73  unsigned int i,k,nf;
74  unsigned int *forceVars;
75 
76  Tfilename fforces;
77  Tfilename fsols;
78  Tfilename fparam;
79 
80  FILE *fin,*fout;
81 
82  boolean oneForce;
83  unsigned int fndx,nb;
84  double *force;
85  double mode;
86 
87  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
88  fprintf(stderr,"Reading parameter file : %s\n",GetFileFullName(&fparam));
89  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
90 
91  InitWorldFromFile(&parameters,TRUE,arg[1],&world);
92 
93  nf=WorldForceVarsIndices(&parameters,&forceVars,&world);
94  if (nf==0)
95  Error("The input world does not include any force variable");
96 
97  CreateFileName(NULL,arg[2],NULL,SOL_EXT,&fsols);
98  fprintf(stderr,"Reading solution file : %s\n",GetFileFullName(&fsols));
99  fin=fopen(GetFileFullName(&fsols),"r");
100  if (!fin)
101  Error("The file with the solution boxes can not be opened");
102 
103  mode=1.0;
104  if (argc>3)
105  {
106  oneForce=TRUE;
107  fndx=atoi(arg[3]);
108  if (fndx>=nf)
109  Error("Too large force index");
110  if (argc>4)
111  mode=atof(arg[4]);
112  }
113  else
114  oneForce=FALSE;
115 
116  if (oneForce)
117  {
118  /* Single force to output: cost file */
119  CreateFileName(NULL,arg[2],NULL,COST_EXT,&fforces);
120  fprintf(stderr,"Writing force to file : %s\n",GetFileFullName(&fforces));
121 
122  /* Count the number of boxes in the input file */
123  nb=0;
124  do {
125  token=ReadBox(fin,&box);
126  if (token!=EOF)
127  {
128  nb++;
129  DeleteBox(&box);
130  }
131  } while(token!=EOF);
132  /* re-open the input file for a second reading */
133  fclose(fin);
134  fin=fopen(GetFileFullName(&fsols),"r");
135 
136  /* allocate space for the selected force */
137  NEW(force,nb,double);
138  }
139  else
140  {
141  /* all forces to output: forces file */
142  CreateFileName(NULL,arg[2],NULL,FORCES_EXT,&fforces);
143  fprintf(stderr,"Writing forces file : %s\n",GetFileFullName(&fforces));
144  }
145  fout=fopen(GetFileFullName(&fforces),"w");
146  if (!fout)
147  Error("The file for the forces can not be opened");
148 
149  k=0;
150  do {
151  token=ReadBox(fin,&box);
152  if (token!=EOF)
153  {
154  /* the box must contain at lasat as many variables
155  as those in the force index array */
156  if (GetBoxNIntervals(&box)<forceVars[nf-1])
157  Error("Box size missmatch");
158 
159  if (oneForce)
160  /* if we only need one force, store it */
161  force[k]=fabs(IntervalCenter(GetBoxInterval(forceVars[fndx],&box)));
162  else
163  {
164  /* When we need all forces, we can store them on the fly */
165  for (i=0;i<nf;i++)
166  fprintf(fout,"%.12f ",IntervalCenter(GetBoxInterval(forceVars[i],&box)));
167  fprintf(fout,"\n");
168  }
169  DeleteBox(&box);
170  k++;
171  }
172  } while(token!=EOF);
173 
174  if (oneForce)
175  {
176  /* In the case of just one force -> scale it (we will use it for plotting) */
177  /* do not use <1e-4 as lower limit (charts with cost 0 are not plotted or
178  are plotted in black!) */
179  Vector2Range(1e-3,1.0,mode,nb,force);
180  SaveVector(fout,nb,force);
181  }
182 
183  fclose(fin);
184  fclose(fout);
185 
186  free(forceVars);
187 
188  DeleteFileName(&fforces);
189  DeleteFileName(&fsols);
190  DeleteFileName(&fparam);
191 
192  DeleteWorld(&world);
193  DeleteParameters(&parameters);
194  }
195  else
196  {
197  fprintf(stdout," Wrong number of parameters.\n");
198  fprintf(stdout," Use:\n");
199  fprintf(stdout," cuikworldforces <world>.world <solutions>.sol [num force [mode]]\n");
200  fprintf(stdout," Where:\n");
201  fprintf(stdout," <world>: File describing the problem\n");
202  fprintf(stdout," <solutions>: Solutions for which to extract the forces.\n");
203  fprintf(stdout," [num force]: Optional. The element in which we are intersted\n");
204  fprintf(stdout," In this case the output is a cost file and the force\n");
205  fprintf(stdout," is scaled to [0,1], as required for plotting.\n");
206  fprintf(stdout," [mode]: Optional and only if num_force is given. Exponential used when\n");
207  fprintf(stdout," scaling the values to the [0,1] range. This is used to exaggerate\n");
208  fprintf(stdout," the values around 0 or around 1 depending of whether this parameter\n");
209  fprintf(stdout," is negative of possitive. The larger the absolute value, the larger the\n");
210  fprintf(stdout," exageration.\n");
211  fprintf(stdout," File extensions are not required\n");
212  }
213 
214  return(EXIT_SUCCESS);
215 }
Tinterval * GetBoxInterval(unsigned int n, Tbox *b)
Returns a pointer to one of the intervals defining the box.
Definition: box.c:270
#define FALSE
FALSE.
Definition: boolean.h:30
unsigned int WorldForceVarsIndices(Tparameters *p, unsigned int **iv, Tworld *w)
Creates an array with the indices of the force variables.
Definition: world.c:2649
#define NEW(_var, _n, _type)
Allocates memory space.
Definition: defines.h:385
Data structure to hold the information about the name of a file.
Definition: filename.h:271
#define FORCES_EXT
File extension for files including forces.
Definition: filename.h:260
#define COST_EXT
File extension for arrays of costs.
Definition: filename.h:156
#define TRUE
TRUE.
Definition: boolean.h:21
void Error(const char *s)
General error function.
Definition: error.c:80
#define PARAM_EXT
File extension for parameter files.
Definition: filename.h:132
All the necessary information to generate equations for mechanisms.
Definition: world.h:229
Definition of the Tworld type and the associated functions.
void DeleteWorld(Tworld *w)
Destructor.
Definition: world.c:3952
unsigned int GetBoxNIntervals(Tbox *b)
Box dimensionality.
Definition: box.c:1016
void SaveVector(FILE *f, unsigned int n, double *v)
Saves a vector.
Error and warning functions.
void DeleteFileName(Tfilename *fn)
Destructor.
Definition: filename.c:205
int ReadBox(FILE *f, Tbox *b)
Reads a box from a file.
Definition: box.c:1196
A table of parameters.
void CreateFileName(char *path, char *name, char *suffix, char *ext, Tfilename *fn)
Constructor.
Definition: filename.c:22
void InitParametersFromFile(char *file, Tparameters *p)
Constructor from a file.
Definition: parameters.c:51
void Vector2Range(double l, double u, double mode, unsigned int m, double *v)
Scales a vector to a given range.
char * GetFileFullName(Tfilename *fn)
Gets the file full name (paht+name+extension).
Definition: filename.c:151
#define SOL_EXT
File extension for solution files.
Definition: filename.h:138
boolean InitWorldFromFile(Tparameters *p, boolean error, char *fn, Tworld *w)
Constructor.
A box.
Definition: box.h:83
double IntervalCenter(Tinterval *i)
Gets the interval center.
Definition: interval.c:129
void DeleteParameters(Tparameters *p)
Destructor.
Definition: parameters.c:294
void DeleteBox(void *b)
Destructor.
Definition: box.c:1283
int main(int argc, char **arg)
Main body of the cuikworldforces application.