parameters.c
Go to the documentation of this file.
1 #include "parameters.h"
2 
3 #include "error.h"
4 #include "filename.h"
5 #include "defines.h"
6 
7 #include <stdlib.h>
8 #include <strings.h>
9 
31 boolean ReadParameters(char *file,Tparameters *p);
32 
33 /*
34  * Inits a parameter structure and defines all parameters equal to 0
35  */
37 {
38  unsigned int i;
39 
40  for(i=0;i<NPARAMETERS;i++)
41  {
42  (*p)[i].name=NULL;
43  (*p)[i].value=0.0;
44  (*p)[i].timesSet=0;
45  }
46 }
47 
48 /*
49  * Inits a parameter structure from the given file
50  */
52 {
53  Tfilename dp;
54  unsigned int i;
55  boolean ok;
56 
57  InitParameters(p);
58 
59 
60  CreateFileName(_CUIK_SUITE_MAIN_DIR,DEFAULT_PARAMS,NULL,PARAM_EXT,&dp);
62  DeleteFileName(&dp);
63  if (!ok)
64  {
65  /* try to read from a system directory (at a user given folder) */
66  CreateFileName(_PARAM_PREFIX,DEFAULT_PARAMS,NULL,PARAM_EXT,&dp);
68  DeleteFileName(&dp);
69  if (!ok)
70  {
71  /* try to read from a system directory (at a user general folder) */
72  CreateFileName("/usr",DEFAULT_PARAMS,NULL,PARAM_EXT,&dp);
73  if (!ReadParameters(GetFileFullName(&dp),p))
74  Error("Can not find the default parameter file");
75  DeleteFileName(&dp);
76  }
77  }
78 
79  /* The default initialization does not count. Reset the 'timesSet' */
80  for(i=0;i<NPARAMETERS;i++)
81  (*p)[i].timesSet=0;
82 
83  /* The existence of the parameter file associated with the
84  problem is compulsatory */
85  if (!ReadParameters(file,p))
86  {
87  char ErrorText[500];
88 
89  sprintf(ErrorText,"File %s does not exists. Using the default parameters",file);
90  Warning(ErrorText);
91 
92  /* fix the 'timeSet' */
93  for(i=0;i<NPARAMETERS;i++)
94  (*p)[i].timesSet=1;
95  }
96 }
97 
98 /*
99  * Returns the value of parameter number 'n'
100  */
101 double GetParameter(unsigned int n,Tparameters *p)
102 {
103  if (n<NPARAMETERS)
104  {
105  if ((*p)[n].name==NULL)
106  Error("Undefined parameter");
107  else
108  return((*p)[n].value);
109  }
110  else
111  Error("Number of parameter out of range");
112  return(0.0);
113 }
114 
115 /*
116  * Returns the parameter identifier given the parameter name.
117  */
118 unsigned int GetParameterID(char *name,Tparameters *p)
119 {
120  boolean found;
121  unsigned int i;
122 
123  found=FALSE;
124  i=0;
125  while((!found)&&(i<NPARAMETERS))
126  {
127  found=(((*p)[i].name!=NULL)&&(strcasecmp((*p)[i].name,name)==0));
128  if (!found) i++;
129  }
130  if (found)
131  return(i);
132  else
133  return(NO_UINT);
134 }
135 
136 /*
137  * Check is a parameter is already set.
138  */
139 boolean ParameterSet(unsigned int n,Tparameters *p)
140 {
141  return(((*p)[n].name!=NULL)&&((*p)[n].timesSet>0));
142 }
143 
144 /*
145  * Sets a new parameter with identifier 'n', with name 'name', and with value 'v'
146  */
147 void SetParameter(unsigned int n,char *name,double v,Tparameters *p)
148 {
149  if (n<NPARAMETERS)
150  {
151  if ((*p)[n].name!=NULL)
152  {
153  if ((*p)[n].timesSet>0)
154  Warning("Redefined parameter");
155  ChangeParameter(n,v,p);
156  (*p)[n].timesSet++;
157  }
158  else
159  {
160  (*p)[n].name=name;
161  (*p)[n].value=v;
162  (*p)[n].timesSet=1;
163  }
164  }
165  else
166  Error("Number of parameter out of range");
167 }
168 
169 /*
170  * Changes the value of parameter 'n'
171  */
172 void ChangeParameter(unsigned int n,double v,Tparameters *p)
173 {
174  if (n<NPARAMETERS)
175  {
176  if ((*p)[n].name==NULL)
177  Error("Changing a non-defined parameter");
178 
179  (*p)[n].value=v;
180  }
181  else
182  Error("Number of parameter out of range");
183 }
184 
185 /*
186  * Prints a set of parameters.
187  * Only those explicitly used are shown.
188  */
189 void PrintParameters(FILE *f,Tparameters *p)
190 {
191  unsigned int i;
192  unsigned int v;
193 
194  fprintf(f,"%% **************************************************\n");
195  fprintf(f,"%% COMPILATION FLAGS:\n");
196  fprintf(f,"%% DEBUG: %u\n",_DEBUG);
197  fprintf(f,"%% SIMPLEX ENGINE: ");
198  switch(_SIMPLEX_ENGINE)
199  {
200  case _GLPK:
201  fprintf(f,"glpk\n");
202  break;
203  case _CLP:
204  fprintf(f,"clp\n");
205  break;
206  case _LP_SOLVE:
207  fprintf(f,"lp_solve\n");
208  break;
209  case _GUROBI:
210  fprintf(f,"gurobi\n");
211  break;
212  default:
213  fprintf(f,"unknown\n");
214  }
215  fprintf(f,"%% MPI: %u\n",_USE_MPI);
216  #ifdef _OPENMP
217  fprintf(f,"%% OpenMP: 1\n");
218  #else
219  fprintf(f,"%% OpenMP: 0\n");
220  #endif
221  #ifdef _LAPACK
222  fprintf(f,"%% Lapack: 1\n");
223  #else
224  fprintf(f,"%% Lapack: 0\n");
225  #endif
226  #ifdef _CBLAS
227  fprintf(f,"%% Cblas: 1\n");
228  #else
229  fprintf(f,"%% Cblas: 0\n");
230  #endif
231  fprintf(f,"%% KDTree: %u\n",_KDTREE);
232  fprintf(f,"%%\n");
233  fprintf(f,"%% PARAMETERS:\n");
234  for(i=0;i<NPARAMETERS;i++)
235  {
236  if ((*p)[i].name!=NULL)
237  {
238  fprintf(f," %s = ",(*p)[i].name);
239  switch(i)
240  {
241  case CT_REPRESENTATION:
242  v=(unsigned int)(*p)[i].value;
243  switch (v)
244  {
245  case REP_LINKS:
246  fprintf(f,"LINKS");
247  break;
248  case REP_FLINKS:
249  fprintf(f,"FLINKS");
250  break;
251  case REP_QLINKS:
252  fprintf(f,"QLINKS");
253  break;
254  case REP_JOINTS:
255  fprintf(f,"JOINTS");
256  break;
257  default:
258  Error("Wrong value for parameter REPRESENTATION");
259  }
260  break;
261  case CT_SPLIT_TYPE:
262  v=(unsigned int)(*p)[i].value;
263  fprintf(f,"%u",v);
264  break;
265  case CT_SAMPLING:
266  v=(unsigned int)(*p)[i].value;
267  switch (v)
268  {
269  case AMBIENT_SAMPLING:
270  fprintf(f,"AMBIENT_SAMPLING");
271  break;
272  case KDTREE_SAMPLING:
273  fprintf(f,"KDTREE_SAMPLING");
274  break;
275  case TANGENT_SAMPLING:
276  fprintf(f,"TANGENT_SAMPLING");
277  break;
278  default:
279  Error("Wrong value for parameter SAMPLING");
280  }
281  break;
282  case CT_CD_ENGINE:
283  v=(unsigned int)(*p)[i].value;
284  switch (v)
285  {
286  case SOLID:
287  fprintf(f,"SOLID");
288  break;
289  case VCOLLIDE:
290  fprintf(f,"VCOLLIDE");
291  break;
292  case PQP:
293  fprintf(f,"PQP");
294  break;
295  case FCL:
296  fprintf(f,"FCL");
297  break;
298  case C_FCL:
299  fprintf(f,"C_FCL");
300  break;
301  case RIGIDCLL:
302  fprintf(f,"RIGIDCLL");
303  break;
304  case NOCD:
305  fprintf(f,"NONE");
306  break;
307  default:
308  Error("Wrong value for parameter CD_ENGINE");
309  }
310  break;
311  case CT_CUT_X:
312  case CT_CUT_Y:
313  case CT_CUT_Z:
314  if ((*p)[i].value==INF)
315  fprintf(f,"+INF");
316  else
317  {
318  if ((*p)[i].value==-INF)
319  fprintf(f,"-INF");
320  else
321  fprintf(f,"%g",(*p)[i].value);
322  }
323  break;
324  case CT_G_AXIS:
325  v=(unsigned int)(*p)[i].value;
326  switch (v)
327  {
328  case 0:
329  fprintf(f,"0"); // no gravity
330  break;
331  case 1:
332  fprintf(f,"X");
333  break;
334  case 2:
335  fprintf(f,"Y");
336  break;
337  case 3:
338  fprintf(f,"Z");
339  break;
340  case 4:
341  fprintf(f,"-X");
342  break;
343  case 5:
344  fprintf(f,"-Y");
345  break;
346  case 6:
347  fprintf(f,"-Z");
348  break;
349  default:
350  Error("Wrong value for parameter CD_G_AXIS");
351  }
352  break;
353  default:
354  fprintf(f,"%g",(*p)[i].value);
355  }
356  fprintf(f,"\n");
357  }
358  }
359 }
360 
362 {
363 }
364