aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src/gdata.cc
blob: 40a6338cf9494931d9b0c0cbba18f4edcb4774ad (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
/***************************************************************************
                          gdata.cc  -  description
                             -------------------
    begin                : Wed Jul 19 2000
    copyright            : (C) 2000 by Erik Schnetter
    email                : schnetter@astro.psu.edu

    $Header: /home/eschnett/C/carpet/Carpet/Carpet/CarpetLib/src/gdata.cc,v 1.7 2001/03/16 21:32:17 eschnett Exp $

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

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <cassert>
#include <fstream>
#include <iomanip>

#include "bbox.hh"
#include "defs.hh"
#include "dist.hh"
#include "vect.hh"

#if !defined(TMPL_IMPLICIT) || !defined(GDATA_HH)
#  include "gdata.hh"
#endif



// Constructors
template<int D>
generic_data<D>::generic_data ()
  : _has_storage(false)
{ }

// Destructors
template<int D>
generic_data<D>::~generic_data () { }



// Output
template<int D>
template<int DD>
void generic_data<D>::write_ascii (const string name, const int time,
				   const vect<int,D>& org,
				   const vect<int,DD>& dirs,
				   const int tl, const int rl,
				   const int c, const int ml)
  const
{
  assert (_has_storage);
  CHECKPOINT;
  
  if (proc()==0) {
    // output on processor 0
    
    int rank;
    MPI_Comm_rank (dist::comm, &rank);
    if (rank == 0) {
      
      ofstream file(name.c_str(), ios::app);
      assert (file.good());
      
      file << setprecision(15);
      
      file << "# iteration " << time << endl
	   << "# time level " << tl << "   refinement level " << rl
	   << "   component " << c << "   multigrid level " << ml << endl
	   << "# column format: it tl rl c ml";
      assert (D>=1 && D<=3);
      for (int d=0; d<D; ++d) file << " " << "xyz"[d];
      file << " data" << endl;
      
      const vect<int,DD> lo = extent().lower()[dirs];
      const vect<int,DD> up = extent().upper()[dirs];
      const vect<int,DD> str = extent().stride()[dirs];
      const bbox<int,DD> ext(lo,up,str);
      
      // Check whether the output origin is contained in the extent of
      // the data
      ivect org1(org);
      for (int d=0; d<DD; ++d) org1[dirs[d]] = ext.lower()[d];
      if (extent().contains(org1)) {
	
	for (bbox<int,DD>::iterator it=ext.begin(); it!=ext.end(); ++it) {
	  ivect index(org);
	  for (int d=0; d<DD; ++d) index[dirs[d]] = (*it)[d];
	  file << time << " " << tl << " " << rl << " " << c << " " << ml
	       << " ";
	  for (int d=0; d<D; ++d) file << index[d] << " ";
	  write_ascii_output_element (file, index);
	  file << endl;
	  for (int d=0; d<DD; ++d) {
	    if (index[dirs[d]]!=extent().upper()[dirs[d]]) break;
	    file << endl;
	  }
	}
	
      }	else {
	
	file << "#" << endl;
	
      }	// if ! ext contains org
      
      file.close();
      assert (file.good());
      
    }
    
  } else {
    // copy to processor 0 and output there
    
    generic_data* const tmp = make_typed();
    tmp->allocate(extent(), 0);
    tmp->copy_from (this, extent());
    tmp->write_ascii (name, time, org, dirs, tl, rl, c, ml);
    delete tmp;
    
  }
}



template<int D>
ostream& operator<< (ostream& os, const generic_data<D>& f) {
  return f.out(os);
}



#if defined(TMPL_EXPLICIT)
template class generic_data<1>;
template ostream& operator<< (ostream& os, const generic_data<1>& d);

template class generic_data<2>;
template ostream& operator<< (ostream& os, const generic_data<2>& d);

template class generic_data<3>;
template ostream& operator<< (ostream& os, const generic_data<3>& d);

template void generic_data<1>::write_ascii
(const string name, const int time,
 const vect<int,1>& org, const vect<int,1>& dirs,
 const int tl, const int rl, const int c, const int ml) const;

template void generic_data<2>::write_ascii
(const string name, const int time,
 const vect<int,2>& org, const vect<int,1>& dirs,
 const int tl, const int rl, const int c, const int ml) const;
template void generic_data<2>::write_ascii
(const string name, const int time,
 const vect<int,2>& org, const vect<int,2>& dirs,
 const int tl, const int rl, const int c, const int ml) const;

template void generic_data<3>::write_ascii
 (const string name, const int time,
  const vect<int,3>& org, const vect<int,1>& dirs,
  const int tl, const int rl, const int c, const int ml) const;
template void generic_data<3>::write_ascii
(const string name, const int time,
 const vect<int,3>& org, const vect<int,2>& dirs,
 const int tl, const int rl, const int c, const int ml) const;
template void generic_data<3>::write_ascii
(const string name, const int time,
 const vect<int,3>& org, const vect<int,3>& dirs,
 const int tl, const int rl, const int c, const int ml) const;
#endif