aboutsummaryrefslogtreecommitdiff
path: root/src/IsoSurfacerInit.c
blob: 5948426cf7e0cfeb27302db4f9be5abc6be29b4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#ifndef TCP /* JMS addition */
#define TCP
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef	_WIN32
#include <time.h>
#include <unistd.h>
#endif

#include <ctype.h>

#include <cctk.h>
#include <cctk_Parameters.h>

//#include <cctk_DefineThorn.h>

//#ifdef	CACTUSNET_HTTP
//#include "CactusNet/http/src/http_GHExtensions.hpp"
//#endif

#include "http_GHExtensions.hpp"


#include "IsoSurfacerGH.h"
#include "IsoSurfacerInit.h"


/* Some function forward declarations */
CCTK_INT4   ParseIsoString(char *s, param_map&parms);
CCTK_INT4   NumberOfItems(char *s, char *d);
char  *GetSubString(char *s, char *d, CCTK_INT4 n);

isotimes_st isotimes;
isotimes_st cdtimes;

CCTK_INT4   RunIsoSurfacer;


#ifdef	CACTUSNET_HTTP
CactusCommands::Client * IsosurfaceHttpClientCreator(CactusCommands::Reply*msgbuf, CactusCommands::CommandList&cmds );
#endif

/***************************************************************/
void *IsoSurfacer_SetupGH (tFleshConfig *config, 
			   int convergence_level, 
			   cGH *GH){
  isosurfacerGH *myGH=(isosurfacerGH*)malloc(sizeof(isosurfacerGH));
  return myGH;
}

int IsoSurfacer_InitGH (cGH *GH){
  DECLARE_CCTK_PARAMETERS
    int n;
  isosurfacerGH *myGH;
  myGH = (isosurfacerGH *) GH->extensions [CCTK_GHExtensionHandle ("IsoSurfacer")];
  
  printf("IsoInit\n");
  /* initialize values */
  myGH->funcName=0;
  myGH->formats=0;
  myGH->outfreq=1;
  myGH->firstIteration=1;
  myGH->resolution=1;
  myGH->uniq_verts=1;
  myGH->RunIsoSurfacer = 0;
  
  /* OK, this is ridiculous.
     Separate out the isostring components into 
     separate things and end this craziness */
  if( (n = ParseIsoString(isosurfacer, myGH->isoparms)) == 0)
    return 0;
  
  RunIsoSurfacer = 1; 
  /*
  doIso = 0;

  for (i=0; i<CCTK_NumVars (); i++) {
    char *fullname = CCTK_FullName (i);

    for(j=0; j<n; j++)
      if(CCTK_Equals (fullname, allparms[j].funcName) ) {
        doIso = 1;
        myGH->do_iso [i] = 1;
        myGH->isoparms [i] = allparms[j];
        if((allparms[j]).formats&SOCK){ 
          printf("usesockets**********\n");
        }
        break;
      }
    free (fullname);
  }
*/ 

  if (CCTK_MyProc (GH) == 0){
    char *cmd = (char *) malloc (strlen (outdir) + 80); 
    
    sprintf (cmd, "mkdir -p %s", outdir); 
    if (system (cmd) < 0)
      CCTK_WARN (1, "Problem creating IsoSurfacer output directory"); 
    free (cmd);
  }
  memset(&isotimes, 0, sizeof(isotimes)); 
  memset(&cdtimes, 0, sizeof(cdtimes));
  return true;
}

/************************************************************/

void IsoSurfaceEnd(cGH *GH)
{


  if( RunIsoSurfacer == 0 )
    return;

#if 0
  isosurfacerGH *myGH;

  myGH = (isosurfacerGH *) GH->extensions [CCTK_GHExtensionHandle ("IsoSurfacer")];

  /* Close Data Connections (If open) */
  if(myGH->datasocket) {
         CCTK_INT4 i=0;
         SendData(myGH->datasocket,"done",1,INT32,&i);
         delete static_cast<DataSender*>(myGH->datasocket);
  }
  if(myGH->commandsocket) 
	delete static_cast<CommandReceiver*>(myGH->commandsocket);

#endif	// EXTERNAL_REMOTEIO

  printf("IsoSurfacer timing \n");
  printf("\n Total number of calls to the IsoSurfacer: %d\n", isotimes.ncalls);
  printf(" Time spent in IsoSurfacer:\n");
  printf("  Total   real   time          : %.4f s\n",
          isotimes.realtime_total * 1.0 / CLK_TCK);
#ifndef	_WIN32
  printf("  Total   user   time          : %.4f s\n",
          isotimes.tms_total.tms_utime * 1.0 / CLK_TCK);
  printf("  Total   system time          : %.4f s\n",
          isotimes.tms_total.tms_stime * 1.0 / CLK_TCK);
  printf("  Average real   time per call : %.4f s\n",
          isotimes.realtime_total * 1.0 / CLK_TCK / isotimes.ncalls);
  printf("  Average user   time per call : %.4f s\n",
          isotimes.tms_total.tms_utime * 1.0 / CLK_TCK / isotimes.ncalls);
  printf("  Average system time per call : %.4f s\n",
          isotimes.tms_total.tms_stime * 1.0 / CLK_TCK / isotimes.ncalls);
#endif
  printf("\n Total number of calls to CollectData: %d\n", cdtimes.ncalls);
  printf(" Time spent in CollectData:\n");
  printf("  Total   real   time          : %.4f s\n",
          cdtimes.realtime_total * 1.0 / CLK_TCK);
#ifndef _WIN32
  printf("  Total   user   time          : %.4f s\n",
          cdtimes.tms_total.tms_utime * 1.0 / CLK_TCK);
  printf("  Total   system time          : %.4f s\n",
          cdtimes.tms_total.tms_stime * 1.0 / CLK_TCK);
#endif
  printf("  Average real   time per call : %.4f s\n",
          cdtimes.realtime_total * 1.0 / CLK_TCK / cdtimes.ncalls);
#ifndef _WIN32
  printf("  Average user   time per call : %.4f s\n",
          cdtimes.tms_total.tms_utime * 1.0 / CLK_TCK / cdtimes.ncalls);
  printf("  Average system time per call : %.4f s\n",
          cdtimes.tms_total.tms_stime * 1.0 / CLK_TCK / cdtimes.ncalls);
  printf("--------------------------------------------------\n");
#endif
}

int IsoSurfacer_rfrTraverseGH (cGH *GH, int rfrpoint)
{
  return 0;
}

/*****************************************************************************/

CCTK_INT4 ParseIsoString(char *s, param_map&allparms)
{
  CCTK_INT4         i, j;
  CCTK_INT4         n, m;
  CCTK_INT4         nlevels, nformats;
  char        *sub0, *sub1, *sub2;



  /* First see how many functions we have      */
  /* by simply counting the number of {} pairs */
  n = NumberOfItems(s, "{}");

  if(n == 0)
  {
    printf("Isosurfacer: empty parameter string '%s'\n", s);
    return 0;
  }

  /* Allocate that many isoparms structs */ 
// *allparms = NEW(n, isoparms_st);

  /* Now fill in each isoparms struct with the info for each function */
  for(i=0; i<n; i++)
  {
    sub0 = GetSubString(s, "{}", i);

    /* Check that we have the right number of () pairs */
    m = NumberOfItems(sub0, "()");

    if(m != 4)
    {
      printf("Isosurfacer: improperly formed parameter string '%s'\n", s);
      printf("             there should be exactly 4 () pairs\n");
      free(sub0);
      return 0;
    }

    /*- 1 -----------------------------------------------------------------*/
    /* The first () pair contains the function name */ 
    char*gridfunc = GetSubString(sub0, "()", 0); 

    isoparms_st*gridparams = new isoparms_st();

	        allparms[gridfunc] = gridparams ;

    	gridparams->funcName = gridfunc;


    /*- 2 -----------------------------------------------------------------*/
    /* The second () pair contains the list of isosurface levels         */
    sub1 = GetSubString(sub0, "()", 1);

      /* First see how many there are by counting the number of commas + 1 */
      nlevels = NumberOfItems(sub1, ",");

      /* and get the values */
      for(j=0; j<nlevels; j++)
      {      
         sub2 = GetSubString(sub1, ",", j);
         gridparams->isolevels.push_back( Isolevel() );

         gridparams->isolevels.back().value = atof(sub2); 

	 gridparams->isolevels.back().Ncolorinfo = 0;

      char*N = strchr(sub2, ':');
	 if (N)
	 {
		N++; 
	 int	Nvalues = NumberOfItems(N, "/");
		if (Nvalues > 7 )
			Nvalues = 7; 

		for(int k=0; k<Nvalues; k++)
		{
		char*sub3 = GetSubString(N, "/", k); 
		char*number = sub3;
			while(*number && !isdigit(*number)) number++;
			gridparams->isolevels.back().colorinfo[k] = atof(number);
			free(sub3);
		} 

		gridparams->isolevels.back().Ncolorinfo = Nvalues;
	 }

         free(sub2);
      }

      free(sub1);


    /*- 3 -----------------------------------------------------------------*/
    /* The third () pair contains the list of output formats */
    sub1 = GetSubString(sub0, "()", 2);

      /* See how many there are by counting the number of commas + 1 */
      nformats = NumberOfItems(sub1, ",");

      /* and get the values */
      gridparams->formats = 0;
      for(j=0; j<nformats; j++)
      {
         sub2 = GetSubString(sub1, ",", j);
         if( !strcmp(sub2, "BIN") )
           gridparams->formats |= BIN;
         else if (!strcmp(sub2, "VRML"))
           gridparams->formats |= VRML;
         else if (!strcmp(sub2, "ASCII"))
           gridparams->formats |= ASCII;
         else if (!strcmp(sub2, "UCD")){
#ifdef VERBOSE
           printf("UCD found in formats string\n");
#endif
           gridparams->formats |= UCD;
        }
         else if (!strcmp(sub2,"SOCK")){         /* JMS addition */
#ifdef VERBOSE
           printf("SOCK found in formats string for %d\n",i);
#endif
           gridparams->formats |= SOCK;
         }
         else if (!strcmp(sub2,"ISOHDF5")){         /* TR addition */
#ifdef VERBOSE
           printf("HDF5 found in formats string for %d\n",i);
#endif
           gridparams->formats |= ISOHDF5;
         }
         else if(!strcmp(sub2,"NONE")){
#ifdef VERBOSE
           printf("NONE string found in formats string\n");
#endif
           gridparams->formats |= NONE;
         }
         else
         {
           printf("Isosurfacer: invalid output format '%s'\n", sub2);
           printf("             supported formats are: BIN, VRML, ASCII, UCD, SOCK\n");
           free(sub2);
           free(sub1);
           free(sub0);
           return 0;
         }
         free(sub2);
      }

      free(sub1);


    /*- 4 -----------------------------------------------------------------*/
    /* The fourth () pair contains the other various parameters */
    sub1 = GetSubString(sub0, "()", 3);

      /* Check that the amount of them is correct (=4) */
      if( NumberOfItems(sub1, ",") != 4)
      {
         printf("Isosurfacer: improperly formed parameter string '%s'\n", s);
         printf("             there should be exactly 4 parameters in the last () pair\n");
         free(sub1);
         free(sub0);
         return 0;
      }

      /* The first one is the frequency of output */
      sub2 = GetSubString(sub1, ",", 0);
      gridparams->outfreq = (CCTK_INT4)atoi(sub2);
      free(sub2);

      /* Then the first iteration */
      sub2 = GetSubString(sub1, ",", 1);
      gridparams->firstIteration = (CCTK_INT4)atoi(sub2);
      free(sub2);

      /* Then the resolution */
      sub2 = GetSubString(sub1, ",", 2);
      gridparams->resolution = (CCTK_INT4)atoi(sub2);
      free(sub2);

      /* And finally the "uniq_vertices" flag */
      sub2 = GetSubString(sub1, ",", 3);
      gridparams->uniq_verts = (CCTK_INT4)atoi(sub2);
      free(sub2);

      free(sub1);

    free(sub0);
  }

  return n;
}

/*****************************************************************************/

CCTK_INT4
NumberOfItems(char *s, char *d)
{
  int   ld, i;
  int   result;
  char *p;

  result = 0;
  ld = strlen(d);

  switch(ld)
  {
    case 1:
      p = s;
      while( *p != '\0' )
      {
         if( *p++ == d[0] )
           result++;
      }
      if( p != s )
         result++;
      return result;

    case 2:
      p = s;
      i = 0;
      while( *p != '\0' )
      {
         if ( *p++ == d[i] )
         {
           result++;
           i = (i==0)?1:0;
         }
      }
      if( (result % 2) != 0 )
         return 0;
      else
         return result/2;

    default:
      return 0;
  }
}

/*****************************************************************************/

char *
GetSubString(char *s, char *d, CCTK_INT4 n)
{
  int   ld, count, i;
  char *result;
  char *p, *q = NULL;

  result = NULL;
  ld = strlen(d);

  p = s;
  count = 0;
  switch(ld)
  {
    case 1:
      q = p;
      while( *p != '\0' )
      {
         if( *p++ == d[0] )
         {
           if(count == n)
             break;
           else
           {
             count++;
             q = p;
           }
         }
      }
      if(*p == '\0')
         p++;
      break;

    case 2:
      i = 0;
      while( *p != '\0' )
      {
         if ( *p++ == d[i] )
         {
           if(i==0)
           {
             q = p;
           }
           else
           {
             if(count == n)
               break;
             else
               count++;
           }
           i = (i==0)?1:0;
         }
      }
      break;

    default:
      return NULL;
  }

  if(count == n)
  {
    while( *q == ' ' )
      q++;
    p-=2;
    while( *p == ' ' )
      p--;
    p++;
    result = NEW((p-q+1), char);
    strncpy(result, q, p-q);
    result[p-q] = '\0';
  }
  return result;
}

/**********************************************************/