1 //**************************************************************
2 //  (C) written and directecd by Robert Kloefkorn
3 //**************************************************************
4 #ifndef GRAPE_READ_PARAMS_CC
5 #define GRAPE_READ_PARAMS_CC
6 
7 //- system includes
8 #include <string>
9 #include <sys/types.h>
10 #include <dirent.h>
11 #include <tuple>
12 
13 //- Dune includes
14 #include <dune/fem/io/file/iointerface.hh>
15 #include <dune/fem/io/file/datawriter.hh>
16 
readDataInfo(std::string path,DATAINFO * dinf,const int timestamp,const int dataSet)17 inline bool readDataInfo(std::string path, DATAINFO * dinf,
18                          const int timestamp, const int dataSet)
19 {
20   static const bool useRankPath = Dune::Fem::DataWriterParameters().separateRankPath();
21   std::cout << "Reading data base for " << dinf->name << "! \n";
22   std::string dataname;
23   if( useRankPath )
24   {
25     dataname = Fem::IOTupleBase::dataName(
26                   Fem::IOInterface::createRecoverPath(path,0, dinf->name, timestamp, useRankPath ),
27                   dinf->name);
28   }
29   else
30   {
31     return true ;
32   }
33 
34   {
35     std::stringstream dummy;
36     dummy << dataSet;
37     dataname += "_";
38     dataname += dummy.str();
39   }
40 
41   std::cerr << "reading dofs from: " << dataname << std::endl;
42 
43   std::ifstream check ( dataname.c_str() );
44   if( ! check )
45   {
46     std::cerr << "Removing non-valid data set `" << dataname << "'\n";
47     // comp = 0 marks non-valid data set
48     dinf->comp = 0;
49     return false;
50   }
51 
52   int fakedata = 1;
53   bool fake = Fem::readParameter(dataname,"Fake_data",fakedata);
54 
55   std::cerr << "FAKE: " << fake << " " << fakedata << std::endl;
56   if( (!fake) || (!fakedata) )
57   {
58     std::string dummy;
59     Fem::readParameter(dataname,"DataBase",dummy);
60     std::string * basename = new std::string (dummy);
61     std::cout << "Read Function: " << *basename << std::endl;
62     dinf->base_name = basename->c_str();
63     dinf->name = basename->c_str();
64     dinf->dimVal = 1;
65     if (!dinf->comp)
66     {
67        dinf->comp = new int [1];
68     }
69     dinf->comp[0] = 0;
70   }
71   else
72   {
73     std::string dummy;
74     Fem::readParameter(dataname,"DataBase",dummy);
75     std::string * basename = new std::string (dummy);
76     std::cout << "Read Function: " << *basename << std::endl;
77     dinf->base_name = basename->c_str();
78 
79     int dimrange;
80     Fem::readParameter(dataname,"Dim_Range",dimrange);
81     if(dimrange <= 0) dataDispErrorExit("wrong dimrange");
82 
83     int dimVal = 1;
84     Fem::readParameter(dataname,"Dim_Domain",dimVal);
85     if((dimVal <= 0) || (dimVal > dimrange)) dataDispErrorExit("wrong DimVal");
86     dinf->dimVal = dimVal;
87 
88     int * comp = new int [dimVal];
89     for(int k=0; k<dimVal; k++)
90     {
91       std::stringstream tmpDummy;
92       tmpDummy << k;
93 
94       std::string compkey ("comp_");
95       compkey += tmpDummy.str();
96 
97       bool couldread = Fem::readParameter(dataname,compkey.c_str(),comp[k]);
98       if(!couldread) dataDispErrorExit("wrong " + compkey);
99     }
100     dinf->comp = comp;
101   }
102   return true;
103 }
104 
105 // return number of procs of data set
scanProcsPaths(const std::string globalPath,const std::string dataPrefix,int step)106 inline int scanProcsPaths(const std::string globalPath,
107                    const std::string dataPrefix,
108                    int step)
109 {
110   int procs = 0;
111   while ( true )
112   {
113     std::string path( Fem::IOInterface::
114         createRecoverPath(globalPath,procs,dataPrefix,step) );
115 
116     // check for directory
117     if( ! Dune::Fem::directoryExists( path ) )
118     {
119       return procs;
120     }
121     ++procs;
122   }
123 
124   return procs;
125 }
126 
127 // return number of procs of data set
scanProcsFiles(const std::string globalPath,const std::string dataPrefix,int step)128 inline int scanProcsFiles(const std::string globalPath,
129                           const std::string dataPrefix,
130                           int step)
131 {
132   const bool singleBackupRestoreFile = Dune::Fem::PersistenceManager :: singleBackupRestoreFile ;
133 
134   int procs = 0;
135   std::string path( Fem::IOInterface::
136       createRecoverPath(globalPath,procs,dataPrefix,step, false ) );
137 
138   while ( true )
139   {
140     std::stringstream filename;
141     filename << path << "/" << dataPrefix << "." << procs;
142 
143     // check for file
144     const bool fileOk = Dune::Fem::fileExists( filename.str() ) ;
145 
146     if( (singleBackupRestoreFile && fileOk) || (! singleBackupRestoreFile && ! fileOk ) )
147     {
148       return procs;
149     }
150     ++procs;
151   }
152 
153   return procs;
154 }
155 
156 std::string path,solprefix;
157 
readParameterList(int argc,char ** argv,bool displayData=true)158 inline int readParameterList (int argc, char **argv, bool displayData = true )
159 {
160   int   i, i_start, i_end;
161   INFO * info = 0;
162   int    n = 0, n_info = 10;
163 
164   int    i_delta = 1;
165   double   timestep = 1.0e-3;
166   //bool fixedMesh = false;
167 #ifdef USE_GRAPE_DISPLAY
168   const  char *replay = 0;
169 #endif
170 
171   info = (INFO *) malloc(n_info*sizeof(INFO));
172   assert(info != 0);
173 
174   info[0].datinf = 0;
175   info[0].name = "grid";
176   info[0].fix_mesh = 0;
177 
178   if (argc < 3)
179   {
180     print_help("datadisp");
181     return(0);
182   }
183   if (argc == 3) {
184     path = Dune::Fem::Parameter::commonOutputPath();
185     std::string dummyfile;
186     Dune::Fem::Parameter::get("fem.io.datafileprefix",solprefix);
187     info[n].name = solprefix.c_str();
188     info[n].datinf = 0;
189     info[n].fix_mesh = 0;
190     const int tupleSize = std::tuple_size<GR_DiscFuncType>::value ;
191     for (int df = 0; df < tupleSize; ++df )
192     {
193       DATAINFO * dinf = (DATAINFO *) std::malloc(sizeof(DATAINFO));
194       assert(dinf);
195       dinf->name = solprefix.c_str();
196       dinf->base_name = 0;
197       dinf->comp = 0;
198       dinf->dimVal = 0;
199       dinf->next = info[n].datinf;
200       info[n].datinf = dinf;
201     }
202     n++;
203   }
204 
205   i_start = atoi(argv[1]);
206   i_end = atoi(argv[2]);
207 
208   std::cout << "Read paramaeterlist ... "<< std::endl;
209 
210   i = 3;
211   while (i < argc)
212   {
213     if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "-help"))
214     {
215       print_help("datadisp");
216     }
217     else if (!strcmp(argv[i], "-i"))
218     {
219       if (i+1 == argc)
220         dataDispErrorExit("usage: -i `increment'\n");
221       i_delta = atoi(argv[i+1]);
222       i += 2;
223     }
224     else if (!strcmp(argv[i], "-v"))
225     {
226       if (i+1 == argc)
227         dataDispErrorExit("usage: -v `vectorprefix'\n");
228 
229       DATAINFO * dinf = (DATAINFO *) std::malloc(sizeof(DATAINFO));
230       assert(dinf);
231       dinf->name = argv[i+1];
232       dinf->base_name = argv[i+1];
233 
234       dinf->comp = 0;
235       dinf->dimVal = 0;
236 
237       /* seems wrong order, but grape truns it arround, we can do nothing else here */
238       dinf->next = info[n].datinf;
239       info[n].datinf = dinf;
240 
241       i += 2;
242     }
243     else if (!strcmp(argv[i], "-t"))
244     {
245       if (i+1 == argc)
246         dataDispErrorExit("usage: -t `time step size'\n");
247       timestep = atof(argv[i+1]);
248       i += 2;
249     }
250     else if (!strcmp(argv[i], "-m"))
251     {
252       if (i+1 == argc)
253         dataDispErrorExit("usage: -m `gridprefix'\n");
254       assert(n < n_info);
255       info[n].name = argv[i+1];
256       info[n].datinf = 0;
257       info[n].fix_mesh = 0;
258       const int tupleSize = std::tuple_size<GR_DiscFuncType>::value ;
259       for (int df = 0; df < tupleSize; ++df )
260       {
261         DATAINFO * dinf = (DATAINFO *) std::malloc(sizeof(DATAINFO));
262         assert(dinf);
263         dinf->name = argv[i+1];
264         dinf->base_name = 0;
265         dinf->comp = 0;
266         dinf->dimVal = 0;
267         dinf->next = info[n].datinf;
268         info[n].datinf = dinf;
269       }
270       n++;
271       i += 2;
272     }
273     /*
274     else if (!strcmp(argv[i], "-f"))
275     {
276       fixedMesh = true;
277       i += 1;
278     }
279     */
280     else if (!strcmp(argv[i], "-p"))
281     {
282       if (i+1 == argc)
283         dataDispErrorExit("usage: -p `path'\n");
284       path = argv[i+1];
285       i += 2;
286     }
287 #ifdef USE_GRAPE_DISPLAY
288     else if (!strcmp(argv[i], "-replay"))
289     {
290       if (i+1 == argc)
291         dataDispErrorExit("usage: -replay `manager.replay file'\n");
292       replay = argv[i+1];
293       i += 2;
294     }
295 #endif
296     else
297     {
298       std::cerr << "unknown option " << argv[i] << std::endl;
299       exit(1);
300     }
301     printf("i = %d, argc = %d\n", i, argc);
302   }
303 
304   // defined in readiotupledata.cc
305 #ifdef USE_GRAPE_DISPLAY
306   if(replay)
307   {
308     std::string replayfile(replay);
309     // if strcmp > 0 then strins not equal
310     if(replayfile != "manager.replay")
311     {
312       std::string cmd("ln -s ");
313       cmd += replayfile;
314       cmd += " manager.replay";
315 
316       //std::cout << "call : " << cmd << "\n";
317       int result = system(cmd.c_str());
318 
319       if(result != 0) replay = 0;
320     }
321   }
322 /*
323   if( fixedMesh )
324   {
325     for(int j=0; j<n; ++j)
326     {
327       info[j].fix_mesh = 1;
328     }
329   }
330 */
331 #endif
332 
333   // scan for max number of processor paths
334   int numberProcessors = 0;
335   static const bool useRankPath = Dune::Fem::DataWriterParameters().separateRankPath();
336   for(int k=0; k<n; k++)
337   {
338     // scan for max number of processor paths
339     int para = ( useRankPath ) ?
340           scanProcsPaths(path,info[k].name,i_start) :
341           scanProcsFiles(path,info[k].name,i_start) ;
342 
343     std::cout << "*******************************************" << std::endl;
344     std::cout << "***   Start reading data for " << para << " procs." << std::endl;
345     std::cout << "*******************************************" << std::endl;
346 
347     // should be at least 1
348     if( para <= 0 )
349     {
350       std::cerr << "ERROR: not a valid data path! \n";
351       abort();
352     }
353     numberProcessors = std::max(numberProcessors,para);
354 
355     int df = 0;
356     DATAINFO * dinf = info[k].datinf;
357     while ( dinf )
358     {
359       if( path == "") path = "./";
360       readDataInfo(path, dinf, i_start, df);
361       dinf = dinf->next;
362       ++df;
363     }
364   }
365 
366 #ifdef USE_GRAPE_DISPLAY
367   // initialize time scenes
368   timeSceneInit(info, n , numberProcessors);
369 #endif
370 
371   // read all data
372   readData(info, path.c_str(),i_start,i_end,i_delta,n,timestep,numberProcessors);
373 
374 #ifdef USE_GRAPE_DISPLAY
375   if( displayData )
376   {
377     std::cout << "Displaying data of " << numberProcessors << " processors! \n";
378     // run grape
379     displayTimeScene(info,numberProcessors);
380   }
381 
382   if(replay)
383   {
384     std::string cmd("rm manager.replay");
385     int result = system(cmd.c_str());
386     if( result != 0 )
387       std::cerr << "System call possibly failed!" << std::endl;
388   }
389 #endif
390 
391   return (EXIT_SUCCESS);
392 }
393 #endif
394