cuikcost.c
Go to the documentation of this file.
1 #include "filename.h"
2 #include "box_list.h"
3 #include "error.h"
4 #include "defines.h"
5 #include "parameters.h"
6 #include "world.h"
7 #include "chart.h"
8 
9 
10 #include <stdio.h>
11 #include <math.h>
12 
79 int main(int argc, char **arg)
80 {
81  Tparameters parameters;
82  TAtlasBase world;
83  Tfilename fparam,fsols,fcost;
84  Tbox *box;
85  FILE *fout;
86  unsigned int n,k,nBoxes;
87  double *point;
88  Tlist boxList;
89  Titerator i;
90  double *cost,minCost,maxCost,c;
91 
92  if (argc>2)
93  {
94  /*Init parameters*/
95  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
96  fprintf(stderr,"Reading parameters : %s\n",GetFileFullName(&fparam));
97  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
98 
99  /*Read the world from file*/
100  CS_WD_INIT(&parameters,arg[1],&world);
101 
102  CreateFileName(NULL,arg[2],NULL,SOL_EXT,&fsols);
103  fprintf(stderr,"Reading solution file : %s\n",GetFileFullName(&fsols));
104  ReadListOfBoxes(GetFileFullName(&fsols),&boxList);
105  nBoxes=ListSize(&boxList);
106  if (nBoxes==0)
107  Error("Empty solution file in cuikcost");
108 
109  /* Compute the energy for each conformation */
110  InitIterator(&i,&boxList);
111  First(&i);
112  k=0;
113  NEW(cost,nBoxes,double);
114  while(!EndOfList(&i))
115  {
116  box=(Tbox *)GetCurrent(&i);
117  if (k==0)
118  {
119  n=GetBoxNIntervals(box);
120  NEW(point,n,double);
121  }
122  GetBoxCenter(NULL,point,box);
123  cost[k]=CS_WD_COST(&parameters,FALSE,point,&world);
124  if (k==0)
125  {
126  minCost=cost[k];
127  maxCost=cost[k];
128  }
129  else
130  {
131  if (cost[k]>maxCost)
132  maxCost=cost[k];
133  else
134  {
135  if (cost[k]<minCost)
136  minCost=cost[k];
137  }
138  }
139  k++;
140  Advance(&i);
141  }
142  fprintf(stderr,"Extreme costs : %g %g\n",minCost,maxCost);
143 
144  /* Normalize and write */
145  CreateFileName(NULL,arg[2],NULL,COST_EXT,&fcost);
146  fprintf(stderr,"Writing cost file : %s\n",GetFileFullName(&fcost));
147 
148  fout=fopen(GetFileFullName(&fcost),"w");
149  if (!fout)
150  Error("Output file for costs can not be opened");
151 
152  for(k=0;k<nBoxes;k++)
153  {
154  c=(cost[k]-minCost)/(maxCost-minCost);
155  if (c<1e-4) c=1e-4;
156  fprintf(fout,"%g\n",c);
157  }
158 
159  fclose(fout);
160 
161  DeleteFileName(&fsols);
162  DeleteFileName(&fcost);
163  DeleteFileName(&fparam);
164 
165  free(point);
166  free(cost);
167  DeleteListOfBoxes(&boxList);
168  DeleteParameters(&parameters);
169  CS_WD_DELETE(&world);
170  }
171  else
172  {
173  fprintf(stderr,"Use:\n");
174  fprintf(stderr," cuikcost <problem_name> <sol_name> \n");
175  fprintf(stderr," <problem_name> the input problem.\n");
176  fprintf(stderr," <sol_name> Configurations to evaluate.\n");
177  fprintf(stderr," The ouput file is <sol_name>.cost\n");
178  }
179 
180  return(EXIT_SUCCESS);
181 }
void First(Titerator *i)
Moves an iterator to the first position of its associated list.
Definition: list.c:356
#define FALSE
FALSE.
Definition: boolean.h:30
#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:248
int main(int argc, char **arg)
Main body of the cuikcost application.
Definition: cuikcost.c:79
Definition of the Tfilename type and the associated functions.
#define COST_EXT
File extension for arrays of costs.
Definition: filename.h:155
void Error(const char *s)
General error function.
Definition: error.c:80
#define PARAM_EXT
File extension for parameter files.
Definition: filename.h:131
boolean ReadListOfBoxes(char *filename, Tlist *l)
Reads a list of boxes from a file.
Definition: box_list.c:286
#define CS_WD_DELETE(wcs)
Destructor of the equation structure.
Definition: wcs.h:500
Collection of methods to work on Tlist of boxes.
Definition of the Tworld type and the associated functions.
unsigned int GetBoxNIntervals(Tbox *b)
Box dimensionality.
Definition: box.c:992
Error and warning functions.
void DeleteFileName(Tfilename *fn)
Destructor.
Definition: filename.c:205
void DeleteListOfBoxes(Tlist *l)
Destructor.
Definition: box_list.c:353
boolean EndOfList(Titerator *i)
Checks if an iterator is pointing at the end of the list.
Definition: list.c:445
Definitions of constants and macros used in several parts of the cuik library.
A generic list.
Definition: list.h:46
void InitIterator(Titerator *i, Tlist *list)
Constructor.
Definition: list.c:284
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
Type defining the equations on which the atlas is defined.
Definition: wcs.h:30
Definition of a local chart on a manifold.
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:137
A box.
Definition: box.h:83
void * GetCurrent(Titerator *i)
Gets the element pointed by the iterator.
Definition: list.c:299
void DeleteParameters(Tparameters *p)
Destructor.
Definition: parameters.c:295
#define CS_WD_INIT(pr, name, wcs)
Initializes the equations from a file.
Definition: wcs.h:89
#define CS_WD_COST(pr, simp, p, wcs)
Evaluates the cost of a given configution.
Definition: wcs.h:387
List iterator.
Definition: list.h:61
void GetBoxCenter(boolean *used, double *c, Tbox *b)
Returns the box center along the selected dimensions.
Definition: box.c:697
unsigned int ListSize(Tlist *list)
Gets the number of elements in the list.
Definition: list.c:180
Definition of the Tparameters type and the associated functions.
boolean Advance(Titerator *i)
Moves an iterator to the next position of its associated list.
Definition: list.c:373