aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src/gdata.cc
blob: 5418db23abb8333d464fee1c8d94b00d89079810 (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
/***************************************************************************
                          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.9 2001/03/22 18:42:05 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 <assert.h>

#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

using namespace std;



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

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



// Data manipulators
template<int D>
void generic_data<D>::copy_from (const generic_data* src, const ibbox& box)
{
  assert (has_storage() && src->has_storage());
  assert (all(box.lower()>=extent().lower()
	      && box.lower()>=src->extent().lower()));
  assert (all(box.upper()<=extent().upper()
	      && box.upper()<=src->extent().upper()));
  assert (all(box.stride()==extent().stride()
	      && box.stride()==src->extent().stride()));
  assert (all((box.lower()-extent().lower())%box.stride() == 0
	      && (box.lower()-src->extent().lower())%box.stride() == 0));
  
  if (proc() == src->proc()) {
    // copy on same processor
    
    int rank;
    MPI_Comm_rank (dist::comm, &rank);
    if (rank == proc()) {
      copy_from_innerloop (src, box);
    }
    
  } else {
    
    // copy to different processor
    generic_data* const tmp = make_typed();
    tmp->allocate (box, src->proc());
    tmp->copy_from (src, box);
    tmp->change_processor (proc());
    copy_from (tmp, box);
    delete tmp;
    
  }
}



template<int D>
void generic_data<D>
::interpolate_from (const generic_data* src, const ibbox& box)
{
  assert (has_storage() && src->has_storage());
  assert (all(box.lower()>=extent().lower()
	      && box.upper()<=extent().upper()));
  assert (all(box.lower()>=extent().lower()
	      && box.lower()>=src->extent().lower()));
  assert (all(box.upper()<=extent().upper()
	      && box.upper()<=src->extent().upper()));
  assert (all(box.stride()==extent().stride()));
  assert (all((box.lower()-extent().lower())%box.stride() == 0));
  
  if (proc() == src->proc()) {
    // interpolate on same processor
    
    int rank;
    MPI_Comm_rank (dist::comm, &rank);
    if (rank == proc()) {
      interpolate_from_innerloop (src, box);
    }
    
  } else {
    // interpolate from other processor
    
    generic_data* const tmp = make_typed();
    tmp->allocate (box, src->proc());
    tmp->interpolate_from (src, box);
    tmp->change_processor (proc());
    copy_from (tmp, box);
    delete tmp;
    
  }
}



template<int D>
void generic_data<D>
::interpolate_from (const generic_data* src1, const int t1,
		    const generic_data* src2, const int t2,
		    const ibbox& box, const int t)
{
  assert (has_storage() && src1->has_storage() && src2->has_storage());
  assert (all(box.lower()>=extent().lower()
	      && box.upper()<=extent().upper()));
  assert (all(box.lower()>=extent().lower()
	      && box.lower()>=src1->extent().lower()
	      && box.lower()>=src2->extent().lower()));
  assert (all(box.upper()<=extent().upper()
	      && box.upper()<=src1->extent().upper()
	      && box.upper()<=src2->extent().upper()));
  assert (all(box.stride()==extent().stride()));
  assert (all((box.lower()-extent().lower())%box.stride() == 0
	      && (box.lower()-src1->extent().lower())%box.stride() == 0
	      && (box.lower()-src2->extent().lower())%box.stride() == 0));
  assert (src1->proc() == src2->proc());
  
  if (proc() == src1->proc()) {
    // interpolate on same processor
    
    int rank;
    MPI_Comm_rank (dist::comm, &rank);
    if (rank == proc()) {
      interpolate_from_innerloop (src1, t1, src2, t2, box, t);
    }
    
  } else {
    // interpolate from other processor
    
    generic_data* const tmp = make_typed();
    tmp->allocate (box, src1->proc());
    tmp->interpolate_from (src1, t1, src2, t2, box, t);
    tmp->change_processor (proc());
    copy_from (tmp, box);
    delete tmp;
    
  }
}



// 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);
      const char* const coords = "xyz";
      for (int d=0; d<D; ++d) file << " " << coords[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<3>;
template ostream& operator<< (ostream& os, const generic_data<3>& d);

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