aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetIOBasic/src/iobasic.cc
blob: e15b6d9d3b5287910261c428f4730907234ca504 (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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <sstream>
#include <string>
#include <vector>

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

#include "CactusBase/IOUtil/src/ioGH.h"
#include "CactusBase/IOUtil/src/ioutil_Utils.h"

#include <Timer.hh>

#include "carpet.hh"

#include "typeprops.hh"

using namespace CarpetLib;



// That's a hack
namespace Carpet {
  void UnsupportedVarType (const int vindex);
}



namespace CarpetIOBasic {

  using namespace std;
  using namespace Carpet;



  // Definition of local types
  struct info {
    string reduction;
    int handle;
  };



  // Registered functions
  void* SetupGH (tFleshConfig* fc, int convLevel, cGH* cctkGH);
  int OutputGH (const cGH* cctkGH);

  // Internal functions
  bool TimeToOutput (const cGH* cctkGH);
  void OutputHeader (const cGH* cctkGH);
  void OutputVar (const cGH* cctkGH, int vindex);
  void OutputVar (const cGH* cctkGH, int vindex, const char* out_reductions);
  void CheckSteerableParameters (const cGH * cctkGH);

  void
  ExamineVariable (int vindex, bool & isint, int & numcomps, bool & isscalar);
  vector<string> ParseReductions (char const * credlist);
  
  
  
  template <typename T>
  bool
  UseScientificNotation (T const & x)
  {
    return false;               // default
  }
  
  template <>
  bool
  UseScientificNotation (CCTK_REAL const & x)
  {
    DECLARE_CCTK_PARAMETERS;
    
    CCTK_REAL const xa = fabs (x);
    return xa != 0 and (xa < real_min or xa >= real_max);
  }
  
  template <>
  bool
  UseScientificNotation (CCTK_COMPLEX const & x)
  {
    return UseScientificNotation(x.real()) or UseScientificNotation(x.imag());
  }



  // Definition of members
  int output_count;
  int last_output;

  /* CarpetBasic GH extension structure */
  struct
  {
    /* list of variables to output */
    char *out_vars;

    /* stop on I/O parameter parsing errors ? */
    bool stop_on_parse_errors;

    /* I/O request description list (for all variables) */
    ioRequest **requests;
  } IOparameters;



  extern "C" int
  CarpetIOBasicStartup ()
  {
    CCTK_RegisterBanner ("AMR info I/O provided by CarpetIOBasic");

    int GHExtension = CCTK_RegisterGHExtension("CarpetIOBasic");
    CCTK_RegisterGHExtensionSetupGH (GHExtension, SetupGH);

    int IOMethod = CCTK_RegisterIOMethod ("CarpetIOBasic");
    CCTK_RegisterIOMethodOutputGH (IOMethod, OutputGH);

    return 0;
  }



  extern "C" void
  CarpetIOBasicInit (CCTK_ARGUMENTS)
  {
    DECLARE_CCTK_ARGUMENTS;

    *this_iteration = 0;
    *last_output_iteration = 0;
    *last_output_time = cctkGH->cctk_time;
  }



  void*
  SetupGH (tFleshConfig* const fc, int const convLevel, cGH* const cctkGH)
  {
    DECLARE_CCTK_PARAMETERS;
    const void *dummy;

    dummy = &fc;
    dummy = &convLevel;
    dummy = &cctkGH;
    dummy = &dummy;

    // Truncate all files if this is not a restart
    const int numvars = CCTK_NumVars ();
    output_count = 0;

    // No iterations have yet been output
    last_output = -1;

    IOparameters.requests = (ioRequest **) calloc (numvars, sizeof(ioRequest*));
    IOparameters.out_vars = strdup ("");

    // initial I/O parameter check
    IOparameters.stop_on_parse_errors = strict_io_parameter_check;
    CheckSteerableParameters (cctkGH);
    IOparameters.stop_on_parse_errors = false;

    // We register only once, ergo we get only one handle.  We store
    // that statically, so there is no need to pass anything to
    // Cactus.
    return NULL;
  }



  int
  OutputGH (const cGH * const cctkGH)
  {
    DECLARE_CCTK_ARGUMENTS;
    DECLARE_CCTK_PARAMETERS;
    
    static Timers::Timer timer ("OutputGH");
    timer.start();
    
    if (TimeToOutput (cctkGH)) {
      
      int const oldprec = cout.precision();
      ios_base::fmtflags const oldflags = cout.flags();
      
      if (output_count ++ % outHeader_every == 0 && outHeader_every != -1) {
        // Print the header
        OutputHeader (cctkGH);
      }
      
      if (CCTK_MyProc(cctkGH) == 0) {
        cout << setw(iter_width) << setfill(' ') << cctk_iteration
             << " "
             << setw(time_width) << setfill(' ')
             << fixed << setprecision(time_prec) << cctk_time;
      }
      
      int const numvars = CCTK_NumVars();
      for (int vindex=0; vindex<numvars; ++vindex) {
        if (IOparameters.requests[vindex]) {
          // use individual reductions list for this variable when given
          // otherwise IOScalar::outInfo_reductions
          const char* out_reductions = IOparameters.requests[vindex]->reductions;
          if (not out_reductions) out_reductions = outInfo_reductions;
 
          OutputVar (cctkGH, vindex, out_reductions);
        }
      }
      
      if (CCTK_MyProc(cctkGH) == 0) {
        cout << endl << flush;
      }
      
      last_output = cctk_iteration;
      
      cout.precision (oldprec);
      cout.setf (oldflags);
      
    } // if time to output
    
    timer.stop();
    
    return 0;
  }
  
  
  
  void
  OutputHeader (const cGH * const cctkGH)
  {
    DECLARE_CCTK_PARAMETERS;
    
    if (CCTK_MyProc(cctkGH) == 0) {
      
      int const numvars = CCTK_NumVars();
      
      // The header consists of four lines:
      //    pass 0: print separator
      //    pass 1: print variable name
      //    pass 2: print reduction operation
      //    pass 3: print separator
      for (int pass=0; pass<4; ++pass) {
        
        // Print iteration and time
        switch (pass) {
        case 0:
        case 3:
          cout << "-------------------";
          break;
        case 1:
          cout << "Iteration      Time";
          break;
        case 2:
          cout << "                   ";
          break;
        default:
          assert (0);
        }
        
        // Loop over all variables that should be output
        for (int vindex=0; vindex<numvars; ++vindex) {
          if (IOparameters.requests[vindex]) {
            
            bool isint;
            int numcomps;
            bool isscalar;
            ExamineVariable (vindex, isint, numcomps, isscalar);
            
            char * cfullname = CCTK_FullName (vindex);
            string const fullname (cfullname);
            free (cfullname);
            
            // Print a vertical separator
            switch (pass) {
            case 0:
            case 3:
              cout << "--";
              break;
            case 1:
            case 2:
              cout << " |";
              break;
            default:
              assert (0);
            }
            
            // use individual reductions list for this variable when given
            // otherwise IOScalar::outInfo_reductions
            const char* out_reductions = IOparameters.requests[vindex]->reductions;
            if (not out_reductions) out_reductions = outInfo_reductions;
            
            // Find the set of desired reductions
            vector<string> const reductions = ParseReductions (out_reductions);
            int const numreds = reductions.size();
      
            int const width = isint ? int_width : real_width;
            
            int const mynumreds = isscalar ? 1 : numreds;
            
            // Print the entry
            switch (pass) {
            case 0:
            case 3:
              {
                size_t const numchars = (width+1)*numcomps*mynumreds;
                cout << setw(numchars) << setfill('-') << "";
              }
              break;
            case 1:
              {
                size_t const numchars = (width+1)*numcomps*mynumreds-1;
                if (fullname.length() > numchars) {
                  int begin = fullname.length() - (numchars-1);
                  cout << " *" << fullname.substr(begin);
                } else {
                  cout << " " << setw(numchars) << setfill(' ') << fullname;
                }
              }
              break;
            case 2:
              if (isscalar) {
                int const numchars = (width+1)*numcomps;
                cout << setw(numchars) << setfill(' ') << "";
              } else {
                for (int red=0; red<mynumreds; ++red) {
                  int const numchars = (width+1)*numcomps-1;
                  cout << " " << setw(numchars) << setfill(' ')
                       << reductions.at(red).substr(0,numchars);
                }
              }
              break;
            default:
              assert (0);
            }
            
          } // if want output
        }   // for vindex
        
        cout << endl;
        
      } // pass
      
    } // if on root processor
  }
  
  
  
  void
  OutputVar (const cGH * const cctkGH,
             int const n)
  {
    DECLARE_CCTK_PARAMETERS;

    OutputVar (cctkGH, n, outInfo_reductions);
  }

  void
  OutputVar (const cGH * const cctkGH,
               int n, 
               const char* out_reductions)
  {
    DECLARE_CCTK_ARGUMENTS;
    DECLARE_CCTK_PARAMETERS;
    
    assert (is_level_mode());
    
    const int group = CCTK_GroupIndexFromVarI (n);
    assert (group>=0 and group<(int)Carpet::arrdata.size());
    const int n0 = CCTK_FirstVarIndexI(group);
    assert (n0>=0 and n0<CCTK_NumVars());
    const int var = n - n0;
    assert (var>=0 and var<CCTK_NumVarsInGroupI(group));
    const int num_tl = CCTK_NumTimeLevelsFromVarI(n);
    assert (num_tl>=1);
    
    // Check for storage
    if (not CCTK_QueryGroupStorageI(cctkGH, group)) {
      char * fullname = CCTK_FullName (n);
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "Cannot output variable \"%s\" because it has no storage",
                  fullname);
      free (fullname);
      return;
    }
    
    assert (do_global_mode);
    
    const int vartype = CCTK_VarTypeI(n);
    assert (vartype >= 0);
    
    // Get grid hierarchy extentsion from IOUtil
    const ioGH * const iogh = (const ioGH *)CCTK_GHExtension (cctkGH, "IO");
    assert (iogh);
    
    // Find the set of desired reductions
    vector<string> const reductionstrings
      = ParseReductions (out_reductions);
    list<info> reductions;
    for (vector<string>::const_iterator ireduction = reductionstrings.begin();
         ireduction != reductionstrings.end();
         ++ireduction)
    {
      int const handle = CCTK_ReductionHandle ((*ireduction).c_str());
      if (handle < 0) {
        CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "Reduction operator \"%s\" does not exist (maybe there is no reduction thorn active?)",
                    (*ireduction).c_str());
      } else {
        info i;
        i.reduction = * ireduction;
        i.handle = handle;
        reductions.push_back (i);
      }
    }
    
    bool isint;
    int numcomps;
    bool isscalar;
    ExamineVariable (n, isint, numcomps, isscalar);
    
    // Output in global mode
    BEGIN_GLOBAL_MODE(cctkGH) {
      
      // Remember cout state
      int const oldprec = cout.precision();
      ios_base::fmtflags const oldflags = cout.flags();
      
      // Print vertical separator
      if (CCTK_MyProc(cctkGH) == 0) {
        cout << " |";
      }
      
      int const width = isint ? int_width : real_width;
      
      if (isscalar) {
        
        if (CCTK_MyProc(cctkGH) == 0) {
          
          cout << " " << setw(width);
          
          void const * const vardataptr = CCTK_VarDataPtrI (cctkGH, 0, n);
          assert (vardataptr);

          switch (specific_cactus_type(vartype)) {
#define CARPET_NO_COMPLEX
#define TYPECASE(N,T)                                                   \
            case N:                                                     \
              {                                                         \
                T const val = * static_cast <T const *> (vardataptr);   \
                if (not isint) {                                        \
                  if (UseScientificNotation (val)) {                    \
                    cout << scientific << setprecision(real_prec_sci);  \
                  } else {                                              \
                    cout << fixed << setprecision(real_prec);           \
                  }                                                     \
                }                                                       \
                cout << val;                                            \
              }                                                         \
            break;
#include "typecase.hh"
#undef TYPECASE
#undef CARPET_NO_COMPLEX
#define CARPET_COMPLEX
#define TYPECASE(N,T)                                                   \
            case N:                                                     \
              {                                                         \
                T const val = * static_cast <T const *> (vardataptr);   \
                if (not isint) {                                        \
                  if (UseScientificNotation (val)) {                    \
                    cout << scientific << setprecision(real_prec_sci);  \
                  } else {                                              \
                    cout << fixed << setprecision(real_prec);           \
                  }                                                     \
                }                                                       \
                cout << real(val) << " " << imag(val);                  \
              }                                                         \
            break;
#include "typecase.hh"
#undef TYPECASE
#undef CARPET_COMPLEX
          default:
            UnsupportedVarType (n);
          }

        } // if on root processor

      } else {

        for (list<info>::const_iterator ireduction = reductions.begin();
             ireduction != reductions.end();
             ++ireduction)
        {
          string const reduction = ireduction->reduction;
        
          int const handle = ireduction->handle;
        
          char result[100];     // assuming no type is larger than this
        
          int const ierr
            = CCTK_Reduce (cctkGH, 0, handle, 1, vartype, result, 1, n);
          assert (not ierr);
        
          if (CCTK_MyProc(cctkGH) == 0) {

            cout << " " << setw(width);
          
            switch (specific_cactus_type(vartype)) {
#define TYPECASE(N,T)                                                   \
              case N:                                                   \
                {                                                       \
                  T const& val = *(T const*)result;                     \
                  if (not isint) {                                      \
                    if (UseScientificNotation (val)) {                  \
                      cout << scientific << setprecision(real_prec_sci); \
                    } else {                                            \
                      cout << fixed << setprecision(real_prec);         \
                    }                                                   \
                  }                                                     \
                  cout << val;                                          \
                }                                                       \
              break;
#include "typecase.hh"
#undef TYPECASE
            default:
              UnsupportedVarType (n);
            }

          } // if on root processor
        
        } // for reductions
      
      } // not isscalar
      
      // Restore cout state
      cout.precision (oldprec);
      cout.setf (oldflags);
      
    } END_GLOBAL_MODE;
  }



  bool
  TimeToOutput (const cGH * const cctkGH)
  {
    DECLARE_CCTK_ARGUMENTS;
    DECLARE_CCTK_PARAMETERS;

    if (not do_global_mode) return false;

    CheckSteerableParameters (cctkGH);
    
    // check whether to output at this iteration
    bool output_this_iteration;

    const char* myoutcriterion = outInfo_criterion;
    if (CCTK_EQUALS(myoutcriterion, "default")) {
      myoutcriterion = out_criterion;
    }

    if (CCTK_EQUALS (myoutcriterion, "never")) {

      // Never output
      output_this_iteration = false;

    } else if (CCTK_EQUALS (myoutcriterion, "iteration")) {

      int myoutevery = outInfo_every;
      if (myoutevery == -2) {
        myoutevery = out_every;
      }
      if (myoutevery <= 0) {
        // output is disabled
        output_this_iteration = false;
      } else if (cctk_iteration == *this_iteration) {
        // we already decided to output this iteration
        output_this_iteration = true;
      } else if (cctk_iteration >= *last_output_iteration + myoutevery) {
        // it is time for the next output
        output_this_iteration = true;
        *last_output_iteration = cctk_iteration;
        *this_iteration = cctk_iteration;
      } else {
        // we want no output at this iteration
        output_this_iteration = false;
      }

    } else if (CCTK_EQUALS (myoutcriterion, "divisor")) {

      int myoutevery = outInfo_every;
      if (myoutevery == -2) {
        myoutevery = out_every;
      }
      if (myoutevery <= 0) {
        // output is disabled
        output_this_iteration = false;
      } else if ((cctk_iteration % myoutevery) == 0 ) {
        // we already decided to output this iteration
        output_this_iteration = true;
      } else {
        // we want no output at this iteration
        output_this_iteration = false;
      }

    } else if (CCTK_EQUALS (myoutcriterion, "time")) {

      CCTK_REAL myoutdt = outInfo_dt;
      if (myoutdt == -2) {
        myoutdt = out_dt;
      }
      if (myoutdt < 0) {
        // output is disabled
        output_this_iteration = false;
      } else if (myoutdt == 0) {
        // output all iterations
        output_this_iteration = true;
      } else if (cctk_iteration == *this_iteration) {
        // we already decided to output this iteration
        output_this_iteration = true;
      } else {
        int do_output =
          (cctk_time / cctk_delta_time >=
           (*last_output_time + myoutdt) / cctk_delta_time - 1.0e-12);
        MPI_Bcast (&do_output, 1, MPI_INT, 0, dist::comm());
        if (do_output) {
          // it is time for the next output
          output_this_iteration = true;
          *last_output_time = cctk_time;
          *this_iteration = cctk_iteration;
        } else {
          // we want no output at this iteration
          output_this_iteration = false;
        }
      }

    } else {

      assert (0);

    } // select output criterion

    if (not output_this_iteration) return false;



    // These should be true in general, but may be false if
    // CCTK_OutputGH is called explicitly:
    // assert (last_output != cctk_iteration);
    // assert (last_output < cctk_iteration);

    // Should be output during this iteration
    return true;
  }



  void
  CheckSteerableParameters (const cGH *const cctkGH)
  {
    DECLARE_CCTK_PARAMETERS;

    // re-parse the 'IOBasic::outInfo_vars' parameter if it has changed
    if (strcmp (outInfo_vars, IOparameters.out_vars)) {
#ifdef IOUTIL_PARSER_HAS_OUT_DT
      IOUtil_ParseVarsForOutput (cctkGH, CCTK_THORNSTRING,
                                 "IOBasic::outInfo_vars",
                                 IOparameters.stop_on_parse_errors,
                                 outInfo_vars, -1, -1.0, IOparameters.requests);
#else
      IOUtil_ParseVarsForOutput (cctkGH, CCTK_THORNSTRING,
                                 "IOBasic::outInfo_vars",
                                 IOparameters.stop_on_parse_errors,
                                 outInfo_vars, -1, IOparameters.requests);
#endif

      // save the last setting of 'IOBasic::outInfo_vars' parameter
      free (IOparameters.out_vars);
      IOparameters.out_vars = strdup (outInfo_vars);
    }
  }
  
  
  
  // Parse the set of reductions
  vector<string>
  ParseReductions (char const * const credlist)
  {
    string const redlist (credlist);
    vector<string> reductions;
    string::const_iterator p = redlist.begin();
    while (p!=redlist.end()) {
      // Skip white space
      while (p!=redlist.end() and isspace(*p)) ++p;
      // Exit if end of reductions is reached
      if (p==redlist.end()) break;
      // Mark beginning of reduction entry
      string::const_iterator const start = p;
      // Walk over reduction entry
      while (p!=redlist.end() and not isspace(*p)) ++p;
      // Mark end of reduction entry
      string::const_iterator const end = p;
      // Remember reduction
      string const reduction (start, end);
      reductions.push_back (reduction);
    }
    return reductions;
  }
  
  
  
  void
  ExamineVariable (int const vindex,
                   bool & isint, int & numcomps, bool & isscalar)
  {
    switch (CCTK_VarTypeI (vindex)) {
    case CCTK_VARIABLE_BYTE:
    case CCTK_VARIABLE_INT:
    case CCTK_VARIABLE_INT1:
    case CCTK_VARIABLE_INT2:
    case CCTK_VARIABLE_INT4:
    case CCTK_VARIABLE_INT8:
      isint = true;
      numcomps = 1;
      break;
    case CCTK_VARIABLE_REAL:
    case CCTK_VARIABLE_REAL4:
    case CCTK_VARIABLE_REAL8:
    case CCTK_VARIABLE_REAL16:
      isint = false;
      numcomps = 1;
      break;
    case CCTK_VARIABLE_COMPLEX:
    case CCTK_VARIABLE_COMPLEX8:
    case CCTK_VARIABLE_COMPLEX16:
    case CCTK_VARIABLE_COMPLEX32:
      isint = false;
      numcomps = 2;
      break;
    default:
      assert (0);
    }
    
    switch (CCTK_GroupTypeFromVarI (vindex)) {
    case CCTK_SCALAR:
      isscalar = true;
      break;
    case CCTK_ARRAY:
    case CCTK_GF:
      isscalar = false;
      break;
    default:
      assert (0);
    }
  }
  
} // namespace CarpetIOBasic