/[escript]/branches/diaplayground/ripley/src/Brick.cpp
ViewVC logotype

Annotation of /branches/diaplayground/ripley/src/Brick.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4949 - (hide annotations)
Mon May 19 05:54:58 2014 UTC (4 years, 11 months ago) by caltinay
File size: 164569 byte(s)
Fast forward to current trunk rev 4947.
All tests that don't require a direct solver pass without MPI.

1 caltinay 3691
2 jfenwick 3981 /*****************************************************************************
3 caltinay 3691 *
4 jfenwick 4657 * Copyright (c) 2003-2014 by University of Queensland
5 jfenwick 3981 * http://www.uq.edu.au
6 caltinay 3691 *
7     * Primary Business: Queensland, Australia
8     * Licensed under the Open Software License version 3.0
9     * http://www.opensource.org/licenses/osl-3.0.php
10     *
11 jfenwick 3981 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 jfenwick 4657 * Development 2012-2013 by School of Earth Sciences
13     * Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 jfenwick 3981 *
15     *****************************************************************************/
16 caltinay 3691
17 sshaw 4861 #include <limits>
18    
19 caltinay 3691 #include <ripley/Brick.h>
20 caltinay 4334 #include <esysUtils/esysFileWriter.h>
21 sshaw 4622 #include <ripley/DefaultAssembler3D.h>
22 sshaw 4629 #include <ripley/WaveAssembler3D.h>
23 sshaw 4712 #include <ripley/LameAssembler3D.h>
24     #include <ripley/domainhelpers.h>
25 caltinay 4477 #include <boost/scoped_array.hpp>
26    
27 caltinay 4013 #ifdef USE_NETCDF
28     #include <netcdfcpp.h>
29     #endif
30    
31 caltinay 3691 #if USE_SILO
32     #include <silo.h>
33     #ifdef ESYS_MPI
34     #include <pmpio.h>
35     #endif
36     #endif
37    
38     #include <iomanip>
39    
40 jfenwick 4650 #include "esysUtils/EsysRandom.h"
41     #include "blocktools.h"
42    
43    
44 caltinay 3691 using namespace std;
45 caltinay 4334 using esysUtils::FileWriter;
46 caltinay 3691
47     namespace ripley {
48    
49 sshaw 4712 int indexOfMax(int a, int b, int c) {
50     if (a > b) {
51     if (c > a) {
52     return 2;
53     }
54     return 0;
55     } else if (b > c) {
56     return 1;
57     }
58     return 2;
59     }
60    
61 caltinay 3781 Brick::Brick(int n0, int n1, int n2, double x0, double y0, double z0,
62 sshaw 4622 double x1, double y1, double z1, int d0, int d1, int d2,
63     const std::vector<double>& points, const std::vector<int>& tags,
64 jfenwick 4934 const simap_t& tagnamestonums,
65     escript::SubWorld_ptr w) :
66     RipleyDomain(3, w)
67 caltinay 3691 {
68 sshaw 4861 if (static_cast<long>(n0 + 1) * static_cast<long>(n1 + 1)
69     * static_cast<long>(n2 + 1) > std::numeric_limits<int>::max())
70     throw RipleyException("The number of elements has overflowed, this "
71     "limit may be raised in future releases.");
72    
73 sshaw 4851 if (n0 <= 0 || n1 <= 0 || n2 <= 0)
74     throw RipleyException("Number of elements in each spatial dimension "
75     "must be positive");
76    
77 caltinay 3943 // ignore subdivision parameters for serial run
78     if (m_mpiInfo->size == 1) {
79     d0=1;
80     d1=1;
81     d2=1;
82     }
83     bool warn=false;
84 sshaw 4712
85     std::vector<int> factors;
86     int ranks = m_mpiInfo->size;
87     int epr[3] = {n0,n1,n2};
88     int d[3] = {d0,d1,d2};
89     if (d0<=0 || d1<=0 || d2<=0) {
90     for (int i = 0; i < 3; i++) {
91     if (d[i] < 1) {
92     d[i] = 1;
93     continue;
94 caltinay 3943 }
95 sshaw 4712 epr[i] = -1; // can no longer be max
96     if (ranks % d[i] != 0) {
97     throw RipleyException("Invalid number of spatial subdivisions");
98 caltinay 3943 }
99 sshaw 4712 //remove
100     ranks /= d[i];
101 caltinay 3943 }
102 sshaw 4712 factorise(factors, ranks);
103     if (factors.size() != 0) {
104     warn = true;
105 caltinay 3943 }
106 sshaw 4712 }
107     while (factors.size() > 0) {
108     int i = indexOfMax(epr[0],epr[1],epr[2]);
109     int f = factors.back();
110     factors.pop_back();
111     d[i] *= f;
112     epr[i] /= f;
113 caltinay 3943 }
114 sshaw 4712 d0 = d[0]; d1 = d[1]; d2 = d[2];
115 caltinay 3943
116 caltinay 3691 // ensure number of subdivisions is valid and nodes can be distributed
117     // among number of ranks
118 sshaw 4712 if (d0*d1*d2 != m_mpiInfo->size){
119 caltinay 3691 throw RipleyException("Invalid number of spatial subdivisions");
120 sshaw 4712 }
121 caltinay 3943 if (warn) {
122     cout << "Warning: Automatic domain subdivision (d0=" << d0 << ", d1="
123     << d1 << ", d2=" << d2 << "). This may not be optimal!" << endl;
124     }
125 caltinay 3691
126 caltinay 4334 double l0 = x1-x0;
127     double l1 = y1-y0;
128     double l2 = z1-z0;
129     m_dx[0] = l0/n0;
130     m_dx[1] = l1/n1;
131     m_dx[2] = l2/n2;
132    
133     if ((n0+1)%d0 > 0) {
134 caltinay 3943 n0=(int)round((float)(n0+1)/d0+0.5)*d0-1;
135 caltinay 4334 l0=m_dx[0]*n0;
136 caltinay 3943 cout << "Warning: Adjusted number of elements and length. N0="
137 caltinay 4334 << n0 << ", l0=" << l0 << endl;
138 caltinay 3943 }
139 caltinay 4334 if ((n1+1)%d1 > 0) {
140 caltinay 3943 n1=(int)round((float)(n1+1)/d1+0.5)*d1-1;
141 caltinay 4334 l1=m_dx[1]*n1;
142 caltinay 3943 cout << "Warning: Adjusted number of elements and length. N1="
143 caltinay 4334 << n1 << ", l1=" << l1 << endl;
144 caltinay 3943 }
145 caltinay 4334 if ((n2+1)%d2 > 0) {
146 caltinay 3943 n2=(int)round((float)(n2+1)/d2+0.5)*d2-1;
147 caltinay 4334 l2=m_dx[2]*n2;
148 caltinay 3943 cout << "Warning: Adjusted number of elements and length. N2="
149 caltinay 4334 << n2 << ", l2=" << l2 << endl;
150 caltinay 3943 }
151    
152 caltinay 4334 if ((d0 > 1 && (n0+1)/d0<2) || (d1 > 1 && (n1+1)/d1<2) || (d2 > 1 && (n2+1)/d2<2))
153 caltinay 3753 throw RipleyException("Too few elements for the number of ranks");
154    
155 caltinay 4334 m_gNE[0] = n0;
156     m_gNE[1] = n1;
157     m_gNE[2] = n2;
158     m_origin[0] = x0;
159     m_origin[1] = y0;
160     m_origin[2] = z0;
161     m_length[0] = l0;
162     m_length[1] = l1;
163     m_length[2] = l2;
164     m_NX[0] = d0;
165     m_NX[1] = d1;
166     m_NX[2] = d2;
167    
168 caltinay 3753 // local number of elements (including overlap)
169 caltinay 4334 m_NE[0] = m_ownNE[0] = (d0>1 ? (n0+1)/d0 : n0);
170     if (m_mpiInfo->rank%d0>0 && m_mpiInfo->rank%d0<d0-1)
171     m_NE[0]++;
172     else if (d0>1 && m_mpiInfo->rank%d0==d0-1)
173     m_ownNE[0]--;
174 caltinay 3764
175 caltinay 4334 m_NE[1] = m_ownNE[1] = (d1>1 ? (n1+1)/d1 : n1);
176     if (m_mpiInfo->rank%(d0*d1)/d0>0 && m_mpiInfo->rank%(d0*d1)/d0<d1-1)
177     m_NE[1]++;
178     else if (d1>1 && m_mpiInfo->rank%(d0*d1)/d0==d1-1)
179     m_ownNE[1]--;
180 caltinay 3764
181 caltinay 4334 m_NE[2] = m_ownNE[2] = (d2>1 ? (n2+1)/d2 : n2);
182     if (m_mpiInfo->rank/(d0*d1)>0 && m_mpiInfo->rank/(d0*d1)<d2-1)
183     m_NE[2]++;
184     else if (d2>1 && m_mpiInfo->rank/(d0*d1)==d2-1)
185     m_ownNE[2]--;
186 caltinay 3753
187     // local number of nodes
188 caltinay 4334 m_NN[0] = m_NE[0]+1;
189     m_NN[1] = m_NE[1]+1;
190     m_NN[2] = m_NE[2]+1;
191 caltinay 3753
192 caltinay 3691 // bottom-left-front node is at (offset0,offset1,offset2) in global mesh
193 caltinay 4334 m_offset[0] = (n0+1)/d0*(m_mpiInfo->rank%d0);
194     if (m_offset[0] > 0)
195     m_offset[0]--;
196     m_offset[1] = (n1+1)/d1*(m_mpiInfo->rank%(d0*d1)/d0);
197     if (m_offset[1] > 0)
198     m_offset[1]--;
199     m_offset[2] = (n2+1)/d2*(m_mpiInfo->rank/(d0*d1));
200     if (m_offset[2] > 0)
201     m_offset[2]--;
202 caltinay 3753
203 caltinay 3691 populateSampleIds();
204 caltinay 3756 createPattern();
205 sshaw 4622
206 sshaw 4629 for (map<string, int>::const_iterator i = tagnamestonums.begin();
207     i != tagnamestonums.end(); i++) {
208     setTagMap(i->first, i->second);
209     }
210 sshaw 4622 addPoints(tags.size(), &points[0], &tags[0]);
211 caltinay 3691 }
212    
213    
214     Brick::~Brick()
215     {
216     }
217    
218     string Brick::getDescription() const
219     {
220     return "ripley::Brick";
221     }
222    
223     bool Brick::operator==(const AbstractDomain& other) const
224     {
225 caltinay 3744 const Brick* o=dynamic_cast<const Brick*>(&other);
226     if (o) {
227     return (RipleyDomain::operator==(other) &&
228 caltinay 4334 m_gNE[0]==o->m_gNE[0] && m_gNE[1]==o->m_gNE[1] && m_gNE[2]==o->m_gNE[2]
229     && m_origin[0]==o->m_origin[0] && m_origin[1]==o->m_origin[1] && m_origin[2]==o->m_origin[2]
230     && m_length[0]==o->m_length[0] && m_length[1]==o->m_length[1] && m_length[2]==o->m_length[2]
231     && m_NX[0]==o->m_NX[0] && m_NX[1]==o->m_NX[1] && m_NX[2]==o->m_NX[2]);
232 caltinay 3744 }
233 caltinay 3691
234     return false;
235     }
236    
237 caltinay 4013 void Brick::readNcGrid(escript::Data& out, string filename, string varname,
238 caltinay 4618 const ReaderParameters& params) const
239 caltinay 4013 {
240     #ifdef USE_NETCDF
241     // check destination function space
242     int myN0, myN1, myN2;
243     if (out.getFunctionSpace().getTypeCode() == Nodes) {
244 caltinay 4334 myN0 = m_NN[0];
245     myN1 = m_NN[1];
246     myN2 = m_NN[2];
247 caltinay 4013 } else if (out.getFunctionSpace().getTypeCode() == Elements ||
248     out.getFunctionSpace().getTypeCode() == ReducedElements) {
249 caltinay 4334 myN0 = m_NE[0];
250     myN1 = m_NE[1];
251     myN2 = m_NE[2];
252 caltinay 4013 } else
253     throw RipleyException("readNcGrid(): invalid function space for output data object");
254    
255 caltinay 4615 if (params.first.size() != 3)
256 caltinay 4013 throw RipleyException("readNcGrid(): argument 'first' must have 3 entries");
257    
258 caltinay 4615 if (params.numValues.size() != 3)
259 caltinay 4013 throw RipleyException("readNcGrid(): argument 'numValues' must have 3 entries");
260    
261 caltinay 4615 if (params.multiplier.size() != 3)
262 caltinay 4277 throw RipleyException("readNcGrid(): argument 'multiplier' must have 3 entries");
263 caltinay 4615 for (size_t i=0; i<params.multiplier.size(); i++)
264     if (params.multiplier[i]<1)
265 caltinay 4277 throw RipleyException("readNcGrid(): all multipliers must be positive");
266    
267 caltinay 4013 // check file existence and size
268     NcFile f(filename.c_str(), NcFile::ReadOnly);
269     if (!f.is_valid())
270     throw RipleyException("readNcGrid(): cannot open file");
271    
272     NcVar* var = f.get_var(varname.c_str());
273     if (!var)
274     throw RipleyException("readNcGrid(): invalid variable name");
275    
276     // TODO: rank>0 data support
277     const int numComp = out.getDataPointSize();
278     if (numComp > 1)
279     throw RipleyException("readNcGrid(): only scalar data supported");
280    
281     const int dims = var->num_dims();
282 caltinay 4477 boost::scoped_array<long> edges(var->edges());
283 caltinay 4013
284     // is this a slice of the data object (dims!=3)?
285     // note the expected ordering of edges (as in numpy: z,y,x)
286 caltinay 4615 if ( (dims==3 && (params.numValues[2] > edges[0] ||
287     params.numValues[1] > edges[1] ||
288     params.numValues[0] > edges[2]))
289     || (dims==2 && params.numValues[2]>1)
290     || (dims==1 && (params.numValues[2]>1 || params.numValues[1]>1)) ) {
291 caltinay 4013 throw RipleyException("readNcGrid(): not enough data in file");
292     }
293    
294     // check if this rank contributes anything
295 caltinay 4615 if (params.first[0] >= m_offset[0]+myN0 ||
296     params.first[0]+params.numValues[0]*params.multiplier[0] <= m_offset[0] ||
297     params.first[1] >= m_offset[1]+myN1 ||
298     params.first[1]+params.numValues[1]*params.multiplier[1] <= m_offset[1] ||
299     params.first[2] >= m_offset[2]+myN2 ||
300     params.first[2]+params.numValues[2]*params.multiplier[2] <= m_offset[2]) {
301 caltinay 4013 return;
302     }
303    
304     // now determine how much this rank has to write
305    
306     // first coordinates in data object to write to
307 caltinay 4615 const int first0 = max(0, params.first[0]-m_offset[0]);
308     const int first1 = max(0, params.first[1]-m_offset[1]);
309     const int first2 = max(0, params.first[2]-m_offset[2]);
310 caltinay 4618 // indices to first value in file (not accounting for reverse yet)
311     int idx0 = max(0, m_offset[0]-params.first[0]);
312     int idx1 = max(0, m_offset[1]-params.first[1]);
313     int idx2 = max(0, m_offset[2]-params.first[2]);
314 caltinay 4277 // number of values to read
315 caltinay 4615 const int num0 = min(params.numValues[0]-idx0, myN0-first0);
316     const int num1 = min(params.numValues[1]-idx1, myN1-first1);
317     const int num2 = min(params.numValues[2]-idx2, myN2-first2);
318 caltinay 4013
319 caltinay 4618 // make sure we read the right block if going backwards through file
320     if (params.reverse[0])
321     idx0 = edges[dims-1]-num0-idx0;
322     if (dims>1 && params.reverse[1])
323     idx1 = edges[dims-2]-num1-idx1;
324     if (dims>2 && params.reverse[2])
325     idx2 = edges[dims-3]-num2-idx2;
326    
327    
328 caltinay 4013 vector<double> values(num0*num1*num2);
329     if (dims==3) {
330     var->set_cur(idx2, idx1, idx0);
331     var->get(&values[0], num2, num1, num0);
332     } else if (dims==2) {
333     var->set_cur(idx1, idx0);
334     var->get(&values[0], num1, num0);
335     } else {
336     var->set_cur(idx0);
337     var->get(&values[0], num0);
338     }
339    
340     const int dpp = out.getNumDataPointsPerSample();
341     out.requireWrite();
342    
343 caltinay 4618 // helpers for reversing
344     const int x0 = (params.reverse[0] ? num0-1 : 0);
345     const int x_mult = (params.reverse[0] ? -1 : 1);
346     const int y0 = (params.reverse[1] ? num1-1 : 0);
347     const int y_mult = (params.reverse[1] ? -1 : 1);
348     const int z0 = (params.reverse[2] ? num2-1 : 0);
349     const int z_mult = (params.reverse[2] ? -1 : 1);
350    
351 caltinay 4013 for (index_t z=0; z<num2; z++) {
352     for (index_t y=0; y<num1; y++) {
353     #pragma omp parallel for
354     for (index_t x=0; x<num0; x++) {
355 caltinay 4615 const int baseIndex = first0+x*params.multiplier[0]
356     +(first1+y*params.multiplier[1])*myN0
357     +(first2+z*params.multiplier[2])*myN0*myN1;
358 caltinay 4618 const int srcIndex=(z0+z_mult*z)*num1*num0
359     +(y0+y_mult*y)*num0
360     +(x0+x_mult*x);
361 caltinay 4174 if (!isnan(values[srcIndex])) {
362 caltinay 4615 for (index_t m2=0; m2<params.multiplier[2]; m2++) {
363     for (index_t m1=0; m1<params.multiplier[1]; m1++) {
364     for (index_t m0=0; m0<params.multiplier[0]; m0++) {
365 caltinay 4277 const int dataIndex = baseIndex+m0
366     +m1*myN0
367     +m2*myN0*myN1;
368     double* dest = out.getSampleDataRW(dataIndex);
369     for (index_t q=0; q<dpp; q++) {
370     *dest++ = values[srcIndex];
371     }
372     }
373     }
374 caltinay 4174 }
375 caltinay 4013 }
376     }
377     }
378     }
379     #else
380     throw RipleyException("readNcGrid(): not compiled with netCDF support");
381     #endif
382     }
383    
384 sshaw 4738 #ifdef USE_BOOSTIO
385     void Brick::readBinaryGridFromZipped(escript::Data& out, string filename,
386     const ReaderParameters& params) const
387     {
388     // the mapping is not universally correct but should work on our
389     // supported platforms
390     switch (params.dataType) {
391     case DATATYPE_INT32:
392     readBinaryGridZippedImpl<int>(out, filename, params);
393     break;
394     case DATATYPE_FLOAT32:
395     readBinaryGridZippedImpl<float>(out, filename, params);
396     break;
397     case DATATYPE_FLOAT64:
398     readBinaryGridZippedImpl<double>(out, filename, params);
399     break;
400     default:
401     throw RipleyException("readBinaryGrid(): invalid or unsupported datatype");
402     }
403     }
404     #endif
405    
406 caltinay 4334 void Brick::readBinaryGrid(escript::Data& out, string filename,
407 caltinay 4618 const ReaderParameters& params) const
408 caltinay 4334 {
409 caltinay 4495 // the mapping is not universally correct but should work on our
410     // supported platforms
411 caltinay 4615 switch (params.dataType) {
412 caltinay 4495 case DATATYPE_INT32:
413 caltinay 4615 readBinaryGridImpl<int>(out, filename, params);
414 caltinay 4495 break;
415     case DATATYPE_FLOAT32:
416 caltinay 4615 readBinaryGridImpl<float>(out, filename, params);
417 caltinay 4495 break;
418     case DATATYPE_FLOAT64:
419 caltinay 4615 readBinaryGridImpl<double>(out, filename, params);
420 caltinay 4495 break;
421     default:
422     throw RipleyException("readBinaryGrid(): invalid or unsupported datatype");
423     }
424     }
425    
426     template<typename ValueType>
427     void Brick::readBinaryGridImpl(escript::Data& out, const string& filename,
428 caltinay 4618 const ReaderParameters& params) const
429 caltinay 4495 {
430 caltinay 4334 // check destination function space
431     int myN0, myN1, myN2;
432     if (out.getFunctionSpace().getTypeCode() == Nodes) {
433     myN0 = m_NN[0];
434     myN1 = m_NN[1];
435     myN2 = m_NN[2];
436     } else if (out.getFunctionSpace().getTypeCode() == Elements ||
437     out.getFunctionSpace().getTypeCode() == ReducedElements) {
438     myN0 = m_NE[0];
439     myN1 = m_NE[1];
440     myN2 = m_NE[2];
441     } else
442     throw RipleyException("readBinaryGrid(): invalid function space for output data object");
443    
444 caltinay 4615 if (params.first.size() != 3)
445 caltinay 4334 throw RipleyException("readBinaryGrid(): argument 'first' must have 3 entries");
446    
447 caltinay 4615 if (params.numValues.size() != 3)
448 caltinay 4334 throw RipleyException("readBinaryGrid(): argument 'numValues' must have 3 entries");
449    
450 caltinay 4615 if (params.multiplier.size() != 3)
451 caltinay 4334 throw RipleyException("readBinaryGrid(): argument 'multiplier' must have 3 entries");
452 caltinay 4615 for (size_t i=0; i<params.multiplier.size(); i++)
453     if (params.multiplier[i]<1)
454 caltinay 4334 throw RipleyException("readBinaryGrid(): all multipliers must be positive");
455    
456     // check file existence and size
457     ifstream f(filename.c_str(), ifstream::binary);
458     if (f.fail()) {
459     throw RipleyException("readBinaryGrid(): cannot open file");
460     }
461     f.seekg(0, ios::end);
462     const int numComp = out.getDataPointSize();
463     const int filesize = f.tellg();
464 caltinay 4615 const int reqsize = params.numValues[0]*params.numValues[1]*params.numValues[2]*numComp*sizeof(ValueType);
465 caltinay 4334 if (filesize < reqsize) {
466     f.close();
467     throw RipleyException("readBinaryGrid(): not enough data in file");
468     }
469    
470     // check if this rank contributes anything
471 caltinay 4615 if (params.first[0] >= m_offset[0]+myN0 ||
472     params.first[0]+params.numValues[0]*params.multiplier[0] <= m_offset[0] ||
473     params.first[1] >= m_offset[1]+myN1 ||
474     params.first[1]+params.numValues[1]*params.multiplier[1] <= m_offset[1] ||
475     params.first[2] >= m_offset[2]+myN2 ||
476     params.first[2]+params.numValues[2]*params.multiplier[2] <= m_offset[2]) {
477 caltinay 4334 f.close();
478     return;
479     }
480    
481     // now determine how much this rank has to write
482    
483     // first coordinates in data object to write to
484 caltinay 4615 const int first0 = max(0, params.first[0]-m_offset[0]);
485     const int first1 = max(0, params.first[1]-m_offset[1]);
486     const int first2 = max(0, params.first[2]-m_offset[2]);
487 caltinay 4334 // indices to first value in file
488 caltinay 4615 const int idx0 = max(0, m_offset[0]-params.first[0]);
489     const int idx1 = max(0, m_offset[1]-params.first[1]);
490     const int idx2 = max(0, m_offset[2]-params.first[2]);
491 caltinay 4334 // number of values to read
492 caltinay 4615 const int num0 = min(params.numValues[0]-idx0, myN0-first0);
493     const int num1 = min(params.numValues[1]-idx1, myN1-first1);
494     const int num2 = min(params.numValues[2]-idx2, myN2-first2);
495 caltinay 4334
496     out.requireWrite();
497 caltinay 4495 vector<ValueType> values(num0*numComp);
498 caltinay 4334 const int dpp = out.getNumDataPointsPerSample();
499    
500 caltinay 4529 for (int z=0; z<num2; z++) {
501     for (int y=0; y<num1; y++) {
502 caltinay 4615 const int fileofs = numComp*(idx0+(idx1+y)*params.numValues[0]
503     +(idx2+z)*params.numValues[0]*params.numValues[1]);
504 caltinay 4495 f.seekg(fileofs*sizeof(ValueType));
505     f.read((char*)&values[0], num0*numComp*sizeof(ValueType));
506 caltinay 4334
507 caltinay 4529 for (int x=0; x<num0; x++) {
508 caltinay 4615 const int baseIndex = first0+x*params.multiplier[0]
509     +(first1+y*params.multiplier[1])*myN0
510     +(first2+z*params.multiplier[2])*myN0*myN1;
511     for (int m2=0; m2<params.multiplier[2]; m2++) {
512     for (int m1=0; m1<params.multiplier[1]; m1++) {
513     for (int m0=0; m0<params.multiplier[0]; m0++) {
514 caltinay 4334 const int dataIndex = baseIndex+m0
515     +m1*myN0
516     +m2*myN0*myN1;
517     double* dest = out.getSampleDataRW(dataIndex);
518 caltinay 4529 for (int c=0; c<numComp; c++) {
519     ValueType val = values[x*numComp+c];
520    
521 caltinay 4615 if (params.byteOrder != BYTEORDER_NATIVE) {
522 caltinay 4529 char* cval = reinterpret_cast<char*>(&val);
523     // this will alter val!!
524     byte_swap32(cval);
525     }
526     if (!std::isnan(val)) {
527     for (int q=0; q<dpp; q++) {
528     *dest++ = static_cast<double>(val);
529 caltinay 4334 }
530     }
531     }
532     }
533     }
534     }
535     }
536     }
537     }
538    
539     f.close();
540     }
541    
542 sshaw 4738 #ifdef USE_BOOSTIO
543     template<typename ValueType>
544     void Brick::readBinaryGridZippedImpl(escript::Data& out, const string& filename,
545     const ReaderParameters& params) const
546     {
547     // check destination function space
548     int myN0, myN1, myN2;
549     if (out.getFunctionSpace().getTypeCode() == Nodes) {
550     myN0 = m_NN[0];
551     myN1 = m_NN[1];
552     myN2 = m_NN[2];
553     } else if (out.getFunctionSpace().getTypeCode() == Elements ||
554     out.getFunctionSpace().getTypeCode() == ReducedElements) {
555     myN0 = m_NE[0];
556     myN1 = m_NE[1];
557     myN2 = m_NE[2];
558     } else
559     throw RipleyException("readBinaryGridFromZipped(): invalid function space for output data object");
560    
561     if (params.first.size() != 3)
562     throw RipleyException("readBinaryGridFromZipped(): argument 'first' must have 3 entries");
563    
564     if (params.numValues.size() != 3)
565     throw RipleyException("readBinaryGridFromZipped(): argument 'numValues' must have 3 entries");
566    
567     if (params.multiplier.size() != 3)
568     throw RipleyException("readBinaryGridFromZipped(): argument 'multiplier' must have 3 entries");
569     for (size_t i=0; i<params.multiplier.size(); i++)
570     if (params.multiplier[i]<1)
571     throw RipleyException("readBinaryGridFromZipped(): all multipliers must be positive");
572    
573     // check file existence and size
574     ifstream f(filename.c_str(), ifstream::binary);
575     if (f.fail()) {
576     throw RipleyException("readBinaryGridFromZipped(): cannot open file");
577     }
578     f.seekg(0, ios::end);
579     const int numComp = out.getDataPointSize();
580     int filesize = f.tellg();
581     f.seekg(0, ios::beg);
582     std::vector<char> compressed(filesize);
583     f.read((char*)&compressed[0], filesize);
584     f.close();
585     std::vector<char> decompressed = unzip(compressed);
586     filesize = decompressed.size();
587     const int reqsize = params.numValues[0]*params.numValues[1]*params.numValues[2]*numComp*sizeof(ValueType);
588     if (filesize < reqsize) {
589     throw RipleyException("readBinaryGridFromZipped(): not enough data in file");
590     }
591    
592     // check if this rank contributes anything
593     if (params.first[0] >= m_offset[0]+myN0 ||
594     params.first[0]+params.numValues[0]*params.multiplier[0] <= m_offset[0] ||
595     params.first[1] >= m_offset[1]+myN1 ||
596     params.first[1]+params.numValues[1]*params.multiplier[1] <= m_offset[1] ||
597     params.first[2] >= m_offset[2]+myN2 ||
598     params.first[2]+params.numValues[2]*params.multiplier[2] <= m_offset[2]) {
599     return;
600     }
601    
602     // now determine how much this rank has to write
603    
604     // first coordinates in data object to write to
605     const int first0 = max(0, params.first[0]-m_offset[0]);
606     const int first1 = max(0, params.first[1]-m_offset[1]);
607     const int first2 = max(0, params.first[2]-m_offset[2]);
608     // indices to first value in file
609     const int idx0 = max(0, m_offset[0]-params.first[0]);
610     const int idx1 = max(0, m_offset[1]-params.first[1]);
611     const int idx2 = max(0, m_offset[2]-params.first[2]);
612     // number of values to read
613     const int num0 = min(params.numValues[0]-idx0, myN0-first0);
614     const int num1 = min(params.numValues[1]-idx1, myN1-first1);
615     const int num2 = min(params.numValues[2]-idx2, myN2-first2);
616    
617     out.requireWrite();
618     vector<ValueType> values(num0*numComp);
619     const int dpp = out.getNumDataPointsPerSample();
620    
621     for (int z=0; z<num2; z++) {
622     for (int y=0; y<num1; y++) {
623     const int fileofs = numComp*(idx0+(idx1+y)*params.numValues[0]
624     +(idx2+z)*params.numValues[0]*params.numValues[1]);
625     memcpy((char*)&values[0], (char*)&decompressed[fileofs*sizeof(ValueType)], num0*numComp*sizeof(ValueType));
626    
627     for (int x=0; x<num0; x++) {
628     const int baseIndex = first0+x*params.multiplier[0]
629     +(first1+y*params.multiplier[1])*myN0
630     +(first2+z*params.multiplier[2])*myN0*myN1;
631     for (int m2=0; m2<params.multiplier[2]; m2++) {
632     for (int m1=0; m1<params.multiplier[1]; m1++) {
633     for (int m0=0; m0<params.multiplier[0]; m0++) {
634     const int dataIndex = baseIndex+m0
635     +m1*myN0
636     +m2*myN0*myN1;
637     double* dest = out.getSampleDataRW(dataIndex);
638     for (int c=0; c<numComp; c++) {
639     ValueType val = values[x*numComp+c];
640    
641     if (params.byteOrder != BYTEORDER_NATIVE) {
642     char* cval = reinterpret_cast<char*>(&val);
643     // this will alter val!!
644     byte_swap32(cval);
645     }
646     if (!std::isnan(val)) {
647     for (int q=0; q<dpp; q++) {
648     *dest++ = static_cast<double>(val);
649     }
650     }
651     }
652     }
653     }
654     }
655     }
656     }
657     }
658     }
659     #endif
660    
661 caltinay 4357 void Brick::writeBinaryGrid(const escript::Data& in, string filename,
662     int byteOrder, int dataType) const
663 caltinay 4334 {
664 caltinay 4357 // the mapping is not universally correct but should work on our
665     // supported platforms
666     switch (dataType) {
667     case DATATYPE_INT32:
668     writeBinaryGridImpl<int>(in, filename, byteOrder);
669     break;
670     case DATATYPE_FLOAT32:
671     writeBinaryGridImpl<float>(in, filename, byteOrder);
672     break;
673     case DATATYPE_FLOAT64:
674     writeBinaryGridImpl<double>(in, filename, byteOrder);
675     break;
676     default:
677     throw RipleyException("writeBinaryGrid(): invalid or unsupported datatype");
678     }
679     }
680    
681     template<typename ValueType>
682     void Brick::writeBinaryGridImpl(const escript::Data& in,
683     const string& filename, int byteOrder) const
684     {
685 caltinay 4334 // check function space and determine number of points
686     int myN0, myN1, myN2;
687     int totalN0, totalN1, totalN2;
688     if (in.getFunctionSpace().getTypeCode() == Nodes) {
689     myN0 = m_NN[0];
690     myN1 = m_NN[1];
691     myN2 = m_NN[2];
692     totalN0 = m_gNE[0]+1;
693     totalN1 = m_gNE[1]+1;
694     totalN2 = m_gNE[2]+1;
695     } else if (in.getFunctionSpace().getTypeCode() == Elements ||
696     in.getFunctionSpace().getTypeCode() == ReducedElements) {
697     myN0 = m_NE[0];
698     myN1 = m_NE[1];
699     myN2 = m_NE[2];
700     totalN0 = m_gNE[0];
701     totalN1 = m_gNE[1];
702     totalN2 = m_gNE[2];
703     } else
704     throw RipleyException("writeBinaryGrid(): invalid function space of data object");
705    
706     const int numComp = in.getDataPointSize();
707     const int dpp = in.getNumDataPointsPerSample();
708 caltinay 4357 const int fileSize = sizeof(ValueType)*numComp*dpp*totalN0*totalN1*totalN2;
709 caltinay 4334
710     if (numComp > 1 || dpp > 1)
711     throw RipleyException("writeBinaryGrid(): only scalar, single-value data supported");
712    
713     // from here on we know that each sample consists of one value
714 caltinay 4482 FileWriter fw;
715     fw.openFile(filename, fileSize);
716 caltinay 4334 MPIBarrier();
717    
718     for (index_t z=0; z<myN2; z++) {
719     for (index_t y=0; y<myN1; y++) {
720     const int fileofs = (m_offset[0]+(m_offset[1]+y)*totalN0
721 caltinay 4357 +(m_offset[2]+z)*totalN0*totalN1)*sizeof(ValueType);
722 caltinay 4334 ostringstream oss;
723    
724     for (index_t x=0; x<myN0; x++) {
725 caltinay 4626 const double* sample = in.getSampleDataRO(z*myN0*myN1+y*myN0+x);
726 caltinay 4357 ValueType fvalue = static_cast<ValueType>(*sample);
727     if (byteOrder == BYTEORDER_NATIVE) {
728 caltinay 4334 oss.write((char*)&fvalue, sizeof(fvalue));
729     } else {
730     char* value = reinterpret_cast<char*>(&fvalue);
731 caltinay 4357 oss.write(byte_swap32(value), sizeof(fvalue));
732 caltinay 4334 }
733     }
734 caltinay 4482 fw.writeAt(oss, fileofs);
735 caltinay 4334 }
736     }
737 caltinay 4482 fw.close();
738 caltinay 4334 }
739    
740 caltinay 3691 void Brick::dump(const string& fileName) const
741     {
742     #if USE_SILO
743     string fn(fileName);
744     if (fileName.length() < 6 || fileName.compare(fileName.length()-5, 5, ".silo") != 0) {
745     fn+=".silo";
746     }
747    
748     int driver=DB_HDF5;
749     string siloPath;
750     DBfile* dbfile = NULL;
751    
752     #ifdef ESYS_MPI
753     PMPIO_baton_t* baton = NULL;
754 gross 3793 const int NUM_SILO_FILES = 1;
755     const char* blockDirFmt = "/block%04d";
756 caltinay 3691 #endif
757    
758     if (m_mpiInfo->size > 1) {
759     #ifdef ESYS_MPI
760     baton = PMPIO_Init(NUM_SILO_FILES, PMPIO_WRITE, m_mpiInfo->comm,
761     0x1337, PMPIO_DefaultCreate, PMPIO_DefaultOpen,
762     PMPIO_DefaultClose, (void*)&driver);
763     // try the fallback driver in case of error
764     if (!baton && driver != DB_PDB) {
765     driver = DB_PDB;
766     baton = PMPIO_Init(NUM_SILO_FILES, PMPIO_WRITE, m_mpiInfo->comm,
767     0x1338, PMPIO_DefaultCreate, PMPIO_DefaultOpen,
768     PMPIO_DefaultClose, (void*)&driver);
769     }
770     if (baton) {
771     char str[64];
772     snprintf(str, 64, blockDirFmt, PMPIO_RankInGroup(baton, m_mpiInfo->rank));
773     siloPath = str;
774     dbfile = (DBfile*) PMPIO_WaitForBaton(baton, fn.c_str(), siloPath.c_str());
775     }
776     #endif
777     } else {
778     dbfile = DBCreate(fn.c_str(), DB_CLOBBER, DB_LOCAL,
779     getDescription().c_str(), driver);
780     // try the fallback driver in case of error
781     if (!dbfile && driver != DB_PDB) {
782     driver = DB_PDB;
783     dbfile = DBCreate(fn.c_str(), DB_CLOBBER, DB_LOCAL,
784     getDescription().c_str(), driver);
785     }
786     }
787    
788     if (!dbfile)
789     throw RipleyException("dump: Could not create Silo file");
790    
791     /*
792     if (driver==DB_HDF5) {
793     // gzip level 1 already provides good compression with minimal
794     // performance penalty. Some tests showed that gzip levels >3 performed
795     // rather badly on escript data both in terms of time and space
796     DBSetCompression("ERRMODE=FALLBACK METHOD=GZIP LEVEL=1");
797     }
798     */
799    
800 caltinay 4334 boost::scoped_ptr<double> x(new double[m_NN[0]]);
801     boost::scoped_ptr<double> y(new double[m_NN[1]]);
802     boost::scoped_ptr<double> z(new double[m_NN[2]]);
803 caltinay 3691 double* coords[3] = { x.get(), y.get(), z.get() };
804     #pragma omp parallel
805     {
806     #pragma omp for
807 caltinay 4334 for (dim_t i0 = 0; i0 < m_NN[0]; i0++) {
808     coords[0][i0]=getLocalCoordinate(i0, 0);
809 caltinay 3691 }
810     #pragma omp for
811 caltinay 4334 for (dim_t i1 = 0; i1 < m_NN[1]; i1++) {
812     coords[1][i1]=getLocalCoordinate(i1, 1);
813 caltinay 3691 }
814     #pragma omp for
815 caltinay 4334 for (dim_t i2 = 0; i2 < m_NN[2]; i2++) {
816     coords[2][i2]=getLocalCoordinate(i2, 2);
817 caltinay 3691 }
818     }
819 caltinay 4334 int* dims = const_cast<int*>(getNumNodesPerDim());
820    
821     // write mesh
822     DBPutQuadmesh(dbfile, "mesh", NULL, coords, dims, 3, DB_DOUBLE,
823 caltinay 3691 DB_COLLINEAR, NULL);
824    
825 caltinay 4334 // write node ids
826     DBPutQuadvar1(dbfile, "nodeId", "mesh", (void*)&m_nodeId[0], dims, 3,
827 caltinay 3698 NULL, 0, DB_INT, DB_NODECENT, NULL);
828 caltinay 3691
829 caltinay 3698 // write element ids
830 caltinay 4334 dims = const_cast<int*>(getNumElementsPerDim());
831 caltinay 3698 DBPutQuadvar1(dbfile, "elementId", "mesh", (void*)&m_elementId[0],
832 caltinay 4334 dims, 3, NULL, 0, DB_INT, DB_ZONECENT, NULL);
833 caltinay 3698
834     // rank 0 writes multimesh and multivar
835 caltinay 3691 if (m_mpiInfo->rank == 0) {
836     vector<string> tempstrings;
837     vector<char*> names;
838     for (dim_t i=0; i<m_mpiInfo->size; i++) {
839     stringstream path;
840     path << "/block" << setw(4) << setfill('0') << right << i << "/mesh";
841     tempstrings.push_back(path.str());
842     names.push_back((char*)tempstrings.back().c_str());
843     }
844     vector<int> types(m_mpiInfo->size, DB_QUAD_RECT);
845     DBSetDir(dbfile, "/");
846     DBPutMultimesh(dbfile, "multimesh", m_mpiInfo->size, &names[0],
847     &types[0], NULL);
848     tempstrings.clear();
849     names.clear();
850     for (dim_t i=0; i<m_mpiInfo->size; i++) {
851     stringstream path;
852     path << "/block" << setw(4) << setfill('0') << right << i << "/nodeId";
853     tempstrings.push_back(path.str());
854     names.push_back((char*)tempstrings.back().c_str());
855     }
856     types.assign(m_mpiInfo->size, DB_QUADVAR);
857     DBPutMultivar(dbfile, "nodeId", m_mpiInfo->size, &names[0],
858     &types[0], NULL);
859 caltinay 3698 tempstrings.clear();
860     names.clear();
861     for (dim_t i=0; i<m_mpiInfo->size; i++) {
862     stringstream path;
863     path << "/block" << setw(4) << setfill('0') << right << i << "/elementId";
864     tempstrings.push_back(path.str());
865     names.push_back((char*)tempstrings.back().c_str());
866     }
867     DBPutMultivar(dbfile, "elementId", m_mpiInfo->size, &names[0],
868     &types[0], NULL);
869 caltinay 3691 }
870    
871     if (m_mpiInfo->size > 1) {
872     #ifdef ESYS_MPI
873     PMPIO_HandOffBaton(baton, dbfile);
874     PMPIO_Finish(baton);
875     #endif
876     } else {
877     DBClose(dbfile);
878     }
879    
880     #else // USE_SILO
881 caltinay 3791 throw RipleyException("dump: no Silo support");
882 caltinay 3691 #endif
883     }
884    
885     const int* Brick::borrowSampleReferenceIDs(int fsType) const
886     {
887 caltinay 3697 switch (fsType) {
888     case Nodes:
889 caltinay 3748 case ReducedNodes: //FIXME: reduced
890 caltinay 3753 return &m_nodeId[0];
891 caltinay 3757 case DegreesOfFreedom:
892     case ReducedDegreesOfFreedom: //FIXME: reduced
893 caltinay 3753 return &m_dofId[0];
894 caltinay 3697 case Elements:
895 caltinay 3733 case ReducedElements:
896 caltinay 3697 return &m_elementId[0];
897 caltinay 3757 case FaceElements:
898 caltinay 3733 case ReducedFaceElements:
899 caltinay 3697 return &m_faceId[0];
900 sshaw 4660 case Points:
901     return &m_diracPointNodeIDs[0];
902 caltinay 3697 default:
903     break;
904     }
905 caltinay 3691
906 caltinay 3697 stringstream msg;
907 caltinay 3791 msg << "borrowSampleReferenceIDs: invalid function space type "<<fsType;
908 caltinay 3697 throw RipleyException(msg.str());
909 caltinay 3691 }
910    
911 caltinay 3757 bool Brick::ownSample(int fsType, index_t id) const
912 caltinay 3691 {
913 caltinay 3759 if (getMPISize()==1)
914     return true;
915    
916 caltinay 3757 switch (fsType) {
917     case Nodes:
918     case ReducedNodes: //FIXME: reduced
919     return (m_dofMap[id] < getNumDOF());
920     case DegreesOfFreedom:
921     case ReducedDegreesOfFreedom:
922     return true;
923     case Elements:
924     case ReducedElements:
925     {
926     // check ownership of element's _last_ node
927 caltinay 4334 const index_t x=id%m_NE[0] + 1;
928     const index_t y=id%(m_NE[0]*m_NE[1])/m_NE[0] + 1;
929     const index_t z=id/(m_NE[0]*m_NE[1]) + 1;
930     return (m_dofMap[x + m_NN[0]*y + m_NN[0]*m_NN[1]*z] < getNumDOF());
931 caltinay 3757 }
932     case FaceElements:
933     case ReducedFaceElements:
934 caltinay 3759 {
935     // check ownership of face element's last node
936     dim_t n=0;
937 caltinay 4334 for (size_t i=0; i<6; i++) {
938     n+=m_faceCount[i];
939 caltinay 3759 if (id<n) {
940 caltinay 4334 const index_t j=id-n+m_faceCount[i];
941 caltinay 3759 if (i>=4) { // front or back
942 caltinay 4334 const index_t first=(i==4 ? 0 : m_NN[0]*m_NN[1]*(m_NN[2]-1));
943     return (m_dofMap[first+j%m_NE[0]+1+(j/m_NE[0]+1)*m_NN[0]] < getNumDOF());
944 caltinay 3759 } else if (i>=2) { // bottom or top
945 caltinay 4334 const index_t first=(i==2 ? 0 : m_NN[0]*(m_NN[1]-1));
946     return (m_dofMap[first+j%m_NE[0]+1+(j/m_NE[0]+1)*m_NN[0]*m_NN[1]] < getNumDOF());
947 caltinay 3759 } else { // left or right
948 caltinay 4334 const index_t first=(i==0 ? 0 : m_NN[0]-1);
949     return (m_dofMap[first+(j%m_NE[1]+1)*m_NN[0]+(j/m_NE[1]+1)*m_NN[0]*m_NN[1]] < getNumDOF());
950 caltinay 3759 }
951     }
952     }
953     return false;
954     }
955 caltinay 3757 default:
956     break;
957 caltinay 3753 }
958 caltinay 3757
959     stringstream msg;
960 caltinay 3791 msg << "ownSample: invalid function space type " << fsType;
961 caltinay 3757 throw RipleyException(msg.str());
962 caltinay 3691 }
963    
964 caltinay 3764 void Brick::setToNormal(escript::Data& out) const
965 caltinay 3703 {
966 caltinay 3764 if (out.getFunctionSpace().getTypeCode() == FaceElements) {
967     out.requireWrite();
968     #pragma omp parallel
969     {
970     if (m_faceOffset[0] > -1) {
971     #pragma omp for nowait
972 caltinay 4334 for (index_t k2 = 0; k2 < m_NE[2]; ++k2) {
973     for (index_t k1 = 0; k1 < m_NE[1]; ++k1) {
974     double* o = out.getSampleDataRW(m_faceOffset[0]+INDEX2(k1,k2,m_NE[1]));
975 caltinay 3764 // set vector at four quadrature points
976     *o++ = -1.; *o++ = 0.; *o++ = 0.;
977     *o++ = -1.; *o++ = 0.; *o++ = 0.;
978     *o++ = -1.; *o++ = 0.; *o++ = 0.;
979     *o++ = -1.; *o++ = 0.; *o = 0.;
980     }
981     }
982     }
983    
984     if (m_faceOffset[1] > -1) {
985     #pragma omp for nowait
986 caltinay 4334 for (index_t k2 = 0; k2 < m_NE[2]; ++k2) {
987     for (index_t k1 = 0; k1 < m_NE[1]; ++k1) {
988     double* o = out.getSampleDataRW(m_faceOffset[1]+INDEX2(k1,k2,m_NE[1]));
989 caltinay 3764 // set vector at four quadrature points
990     *o++ = 1.; *o++ = 0.; *o++ = 0.;
991     *o++ = 1.; *o++ = 0.; *o++ = 0.;
992     *o++ = 1.; *o++ = 0.; *o++ = 0.;
993     *o++ = 1.; *o++ = 0.; *o = 0.;
994     }
995     }
996     }
997    
998     if (m_faceOffset[2] > -1) {
999     #pragma omp for nowait
1000 caltinay 4334 for (index_t k2 = 0; k2 < m_NE[2]; ++k2) {
1001     for (index_t k0 = 0; k0 < m_NE[0]; ++k0) {
1002     double* o = out.getSampleDataRW(m_faceOffset[2]+INDEX2(k0,k2,m_NE[0]));
1003 caltinay 3764 // set vector at four quadrature points
1004     *o++ = 0.; *o++ = -1.; *o++ = 0.;
1005     *o++ = 0.; *o++ = -1.; *o++ = 0.;
1006     *o++ = 0.; *o++ = -1.; *o++ = 0.;
1007     *o++ = 0.; *o++ = -1.; *o = 0.;
1008     }
1009     }
1010     }
1011    
1012     if (m_faceOffset[3] > -1) {
1013     #pragma omp for nowait
1014 caltinay 4334 for (index_t k2 = 0; k2 < m_NE[2]; ++k2) {
1015     for (index_t k0 = 0; k0 < m_NE[0]; ++k0) {
1016     double* o = out.getSampleDataRW(m_faceOffset[3]+INDEX2(k0,k2,m_NE[0]));
1017 caltinay 3764 // set vector at four quadrature points
1018     *o++ = 0.; *o++ = 1.; *o++ = 0.;
1019     *o++ = 0.; *o++ = 1.; *o++ = 0.;
1020     *o++ = 0.; *o++ = 1.; *o++ = 0.;
1021     *o++ = 0.; *o++ = 1.; *o = 0.;
1022     }
1023     }
1024     }
1025    
1026     if (m_faceOffset[4] > -1) {
1027     #pragma omp for nowait
1028 caltinay 4334 for (index_t k1 = 0; k1 < m_NE[1]; ++k1) {
1029     for (index_t k0 = 0; k0 < m_NE[0]; ++k0) {
1030     double* o = out.getSampleDataRW(m_faceOffset[4]+INDEX2(k0,k1,m_NE[0]));
1031 caltinay 3764 // set vector at four quadrature points
1032     *o++ = 0.; *o++ = 0.; *o++ = -1.;
1033     *o++ = 0.; *o++ = 0.; *o++ = -1.;
1034     *o++ = 0.; *o++ = 0.; *o++ = -1.;
1035     *o++ = 0.; *o++ = 0.; *o = -1.;
1036     }
1037     }
1038     }
1039    
1040     if (m_faceOffset[5] > -1) {
1041     #pragma omp for nowait
1042 caltinay 4334 for (index_t k1 = 0; k1 < m_NE[1]; ++k1) {
1043     for (index_t k0 = 0; k0 < m_NE[0]; ++k0) {
1044     double* o = out.getSampleDataRW(m_faceOffset[5]+INDEX2(k0,k1,m_NE[0]));
1045 caltinay 3764 // set vector at four quadrature points
1046     *o++ = 0.; *o++ = 0.; *o++ = 1.;
1047     *o++ = 0.; *o++ = 0.; *o++ = 1.;
1048     *o++ = 0.; *o++ = 0.; *o++ = 1.;
1049     *o++ = 0.; *o++ = 0.; *o = 1.;
1050     }
1051     }
1052     }
1053     } // end of parallel section
1054     } else if (out.getFunctionSpace().getTypeCode() == ReducedFaceElements) {
1055     out.requireWrite();
1056     #pragma omp parallel
1057     {
1058     if (m_faceOffset[0] > -1) {
1059     #pragma omp for nowait
1060 caltinay 4334 for (index_t k2 = 0; k2 < m_NE[2]; ++k2) {
1061     for (index_t k1 = 0; k1 < m_NE[1]; ++k1) {
1062     double* o = out.getSampleDataRW(m_faceOffset[0]+INDEX2(k1,k2,m_NE[1]));
1063 caltinay 3764 *o++ = -1.;
1064     *o++ = 0.;
1065     *o = 0.;
1066     }
1067     }
1068     }
1069    
1070     if (m_faceOffset[1] > -1) {
1071     #pragma omp for nowait
1072 caltinay 4334 for (index_t k2 = 0; k2 < m_NE[2]; ++k2) {
1073     for (index_t k1 = 0; k1 < m_NE[1]; ++k1) {
1074     double* o = out.getSampleDataRW(m_faceOffset[1]+INDEX2(k1,k2,m_NE[1]));
1075 caltinay 3764 *o++ = 1.;
1076     *o++ = 0.;
1077     *o = 0.;
1078     }
1079     }
1080     }
1081    
1082     if (m_faceOffset[2] > -1) {
1083     #pragma omp for nowait
1084 caltinay 4334 for (index_t k2 = 0; k2 < m_NE[2]; ++k2) {
1085     for (index_t k0 = 0; k0 < m_NE[0]; ++k0) {
1086     double* o = out.getSampleDataRW(m_faceOffset[2]+INDEX2(k0,k2,m_NE[0]));
1087 caltinay 3764 *o++ = 0.;
1088     *o++ = -1.;
1089     *o = 0.;
1090     }
1091     }
1092     }
1093    
1094     if (m_faceOffset[3] > -1) {
1095     #pragma omp for nowait
1096 caltinay 4334 for (index_t k2 = 0; k2 < m_NE[2]; ++k2) {
1097     for (index_t k0 = 0; k0 < m_NE[0]; ++k0) {
1098     double* o = out.getSampleDataRW(m_faceOffset[3]+INDEX2(k0,k2,m_NE[0]));
1099 caltinay 3764 *o++ = 0.;
1100     *o++ = 1.;
1101     *o = 0.;
1102     }
1103     }
1104     }
1105    
1106     if (m_faceOffset[4] > -1) {
1107     #pragma omp for nowait
1108 caltinay 4334 for (index_t k1 = 0; k1 < m_NE[1]; ++k1) {
1109     for (index_t k0 = 0; k0 < m_NE[0]; ++k0) {
1110     double* o = out.getSampleDataRW(m_faceOffset[4]+INDEX2(k0,k1,m_NE[0]));
1111 caltinay 3764 *o++ = 0.;
1112     *o++ = 0.;
1113     *o = -1.;
1114     }
1115     }
1116     }
1117    
1118     if (m_faceOffset[5] > -1) {
1119     #pragma omp for nowait
1120 caltinay 4334 for (index_t k1 = 0; k1 < m_NE[1]; ++k1) {
1121     for (index_t k0 = 0; k0 < m_NE[0]; ++k0) {
1122     double* o = out.getSampleDataRW(m_faceOffset[5]+INDEX2(k0,k1,m_NE[0]));
1123 caltinay 3764 *o++ = 0.;
1124     *o++ = 0.;
1125     *o = 1.;
1126     }
1127     }
1128     }
1129     } // end of parallel section
1130    
1131     } else {
1132     stringstream msg;
1133 caltinay 3791 msg << "setToNormal: invalid function space type "
1134     << out.getFunctionSpace().getTypeCode();
1135 caltinay 3764 throw RipleyException(msg.str());
1136     }
1137     }
1138    
1139     void Brick::setToSize(escript::Data& out) const
1140     {
1141     if (out.getFunctionSpace().getTypeCode() == Elements
1142     || out.getFunctionSpace().getTypeCode() == ReducedElements) {
1143     out.requireWrite();
1144     const dim_t numQuad=out.getNumDataPointsPerSample();
1145 caltinay 4334 const double size=sqrt(m_dx[0]*m_dx[0]+m_dx[1]*m_dx[1]+m_dx[2]*m_dx[2]);
1146 caltinay 3764 #pragma omp parallel for
1147     for (index_t k = 0; k < getNumElements(); ++k) {
1148     double* o = out.getSampleDataRW(k);
1149     fill(o, o+numQuad, size);
1150     }
1151     } else if (out.getFunctionSpace().getTypeCode() == FaceElements
1152     || out.getFunctionSpace().getTypeCode() == ReducedFaceElements) {
1153     out.requireWrite();
1154     const dim_t numQuad=out.getNumDataPointsPerSample();
1155     #pragma omp parallel
1156     {
1157     if (m_faceOffset[0] > -1) {
1158 caltinay 4334 const double size=min(m_dx[1],m_dx[2]);
1159 caltinay 3764 #pragma omp for nowait
1160 caltinay 4334 for (index_t k2 = 0; k2 < m_NE[2]; ++k2) {
1161     for (index_t k1 = 0; k1 < m_NE[1]; ++k1) {
1162     double* o = out.getSampleDataRW(m_faceOffset[0]+INDEX2(k1,k2,m_NE[1]));
1163 caltinay 3764 fill(o, o+numQuad, size);
1164     }
1165     }
1166     }
1167    
1168     if (m_faceOffset[1] > -1) {
1169 caltinay 4334 const double size=min(m_dx[1],m_dx[2]);
1170 caltinay 3764 #pragma omp for nowait
1171 caltinay 4334 for (index_t k2 = 0; k2 < m_NE[2]; ++k2) {
1172     for (index_t k1 = 0; k1 < m_NE[1]; ++k1) {
1173     double* o = out.getSampleDataRW(m_faceOffset[1]+INDEX2(k1,k2,m_NE[1]));
1174 caltinay 3764 fill(o, o+numQuad, size);
1175     }
1176     }
1177     }
1178    
1179     if (m_faceOffset[2] > -1) {
1180 caltinay 4334 const double size=min(m_dx[0],m_dx[2]);
1181 caltinay 3764 #pragma omp for nowait
1182 caltinay 4334 for (index_t k2 = 0; k2 < m_NE[2]; ++k2) {
1183     for (index_t k0 = 0; k0 < m_NE[0]; ++k0) {
1184     double* o = out.getSampleDataRW(m_faceOffset[2]+INDEX2(k0,k2,m_NE[0]));
1185 caltinay 3764 fill(o, o+numQuad, size);
1186     }
1187     }
1188     }
1189    
1190     if (m_faceOffset[3] > -1) {
1191 caltinay 4334 const double size=min(m_dx[0],m_dx[2]);
1192 caltinay 3764 #pragma omp for nowait
1193 caltinay 4334 for (index_t k2 = 0; k2 < m_NE[2]; ++k2) {
1194     for (index_t k0 = 0; k0 < m_NE[0]; ++k0) {
1195     double* o = out.getSampleDataRW(m_faceOffset[3]+INDEX2(k0,k2,m_NE[0]));
1196 caltinay 3764 fill(o, o+numQuad, size);
1197     }
1198     }
1199     }
1200    
1201     if (m_faceOffset[4] > -1) {
1202 caltinay 4334 const double size=min(m_dx[0],m_dx[1]);
1203 caltinay 3764 #pragma omp for nowait
1204 caltinay 4334 for (index_t k1 = 0; k1 < m_NE[1]; ++k1) {
1205     for (index_t k0 = 0; k0 < m_NE[0]; ++k0) {
1206     double* o = out.getSampleDataRW(m_faceOffset[4]+INDEX2(k0,k1,m_NE[0]));
1207 caltinay 3764 fill(o, o+numQuad, size);
1208     }
1209     }
1210     }
1211    
1212     if (m_faceOffset[5] > -1) {
1213 caltinay 4334 const double size=min(m_dx[0],m_dx[1]);
1214 caltinay 3764 #pragma omp for nowait
1215 caltinay 4334 for (index_t k1 = 0; k1 < m_NE[1]; ++k1) {
1216     for (index_t k0 = 0; k0 < m_NE[0]; ++k0) {
1217     double* o = out.getSampleDataRW(m_faceOffset[5]+INDEX2(k0,k1,m_NE[0]));
1218 caltinay 3764 fill(o, o+numQuad, size);
1219     }
1220     }
1221     }
1222     } // end of parallel section
1223    
1224     } else {
1225     stringstream msg;
1226 caltinay 3791 msg << "setToSize: invalid function space type "
1227     << out.getFunctionSpace().getTypeCode();
1228 caltinay 3764 throw RipleyException(msg.str());
1229     }
1230     }
1231    
1232     void Brick::Print_Mesh_Info(const bool full) const
1233     {
1234     RipleyDomain::Print_Mesh_Info(full);
1235     if (full) {
1236     cout << " Id Coordinates" << endl;
1237     cout.precision(15);
1238     cout.setf(ios::scientific, ios::floatfield);
1239     for (index_t i=0; i < getNumNodes(); i++) {
1240     cout << " " << setw(5) << m_nodeId[i]
1241 caltinay 4334 << " " << getLocalCoordinate(i%m_NN[0], 0)
1242     << " " << getLocalCoordinate(i%(m_NN[0]*m_NN[1])/m_NN[0], 1)
1243     << " " << getLocalCoordinate(i/(m_NN[0]*m_NN[1]), 2) << endl;
1244 caltinay 3764 }
1245     }
1246     }
1247    
1248    
1249     //protected
1250     void Brick::assembleCoordinates(escript::Data& arg) const
1251     {
1252     escriptDataC x = arg.getDataC();
1253     int numDim = m_numDim;
1254     if (!isDataPointShapeEqual(&x, 1, &numDim))
1255     throw RipleyException("setToX: Invalid Data object shape");
1256     if (!numSamplesEqual(&x, 1, getNumNodes()))
1257     throw RipleyException("setToX: Illegal number of samples in Data object");
1258    
1259     arg.requireWrite();
1260     #pragma omp parallel for
1261 caltinay 4334 for (dim_t i2 = 0; i2 < m_NN[2]; i2++) {
1262     for (dim_t i1 = 0; i1 < m_NN[1]; i1++) {
1263     for (dim_t i0 = 0; i0 < m_NN[0]; i0++) {
1264     double* point = arg.getSampleDataRW(i0+m_NN[0]*i1+m_NN[0]*m_NN[1]*i2);
1265     point[0] = getLocalCoordinate(i0, 0);
1266     point[1] = getLocalCoordinate(i1, 1);
1267     point[2] = getLocalCoordinate(i2, 2);
1268 caltinay 3764 }
1269     }
1270     }
1271     }
1272    
1273     //protected
1274 caltinay 4626 void Brick::assembleGradient(escript::Data& out, const escript::Data& in) const
1275 caltinay 3764 {
1276 caltinay 3703 const dim_t numComp = in.getDataPointSize();
1277 caltinay 3731 const double C0 = .044658198738520451079;
1278     const double C1 = .16666666666666666667;
1279     const double C2 = .21132486540518711775;
1280     const double C3 = .25;
1281     const double C4 = .5;
1282     const double C5 = .62200846792814621559;
1283     const double C6 = .78867513459481288225;
1284    
1285 caltinay 3703 if (out.getFunctionSpace().getTypeCode() == Elements) {
1286 caltinay 3760 out.requireWrite();
1287 caltinay 3913 #pragma omp parallel
1288     {
1289     vector<double> f_000(numComp);
1290     vector<double> f_001(numComp);
1291     vector<double> f_010(numComp);
1292     vector<double> f_011(numComp);
1293     vector<double> f_100(numComp);
1294     vector<double> f_101(numComp);
1295     vector<double> f_110(numComp);
1296     vector<double> f_111(numComp);
1297     #pragma omp for
1298 caltinay 4334 for (index_t k2=0; k2 < m_NE[2]; ++k2) {
1299     for (index_t k1=0; k1 < m_NE[1]; ++k1) {
1300     for (index_t k0=0; k0 < m_NE[0]; ++k0) {
1301     memcpy(&f_000[0], in.getSampleDataRO(INDEX3(k0,k1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1302     memcpy(&f_001[0], in.getSampleDataRO(INDEX3(k0,k1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1303     memcpy(&f_010[0], in.getSampleDataRO(INDEX3(k0,k1+1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1304     memcpy(&f_011[0], in.getSampleDataRO(INDEX3(k0,k1+1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1305     memcpy(&f_100[0], in.getSampleDataRO(INDEX3(k0+1,k1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1306     memcpy(&f_101[0], in.getSampleDataRO(INDEX3(k0+1,k1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1307     memcpy(&f_110[0], in.getSampleDataRO(INDEX3(k0+1,k1+1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1308     memcpy(&f_111[0], in.getSampleDataRO(INDEX3(k0+1,k1+1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1309     double* o = out.getSampleDataRW(INDEX3(k0,k1,k2,m_NE[0],m_NE[1]));
1310 caltinay 3913 for (index_t i=0; i < numComp; ++i) {
1311 caltinay 4375 const double V0=((f_100[i]-f_000[i])*C5 + (f_111[i]-f_011[i])*C0 + (f_101[i]+f_110[i]-f_001[i]-f_010[i])*C1) / m_dx[0];
1312     const double V1=((f_110[i]-f_010[i])*C5 + (f_101[i]-f_001[i])*C0 + (f_100[i]+f_111[i]-f_000[i]-f_011[i])*C1) / m_dx[0];
1313     const double V2=((f_101[i]-f_001[i])*C5 + (f_110[i]-f_010[i])*C0 + (f_100[i]+f_111[i]-f_000[i]-f_011[i])*C1) / m_dx[0];
1314     const double V3=((f_111[i]-f_011[i])*C5 + (f_100[i]-f_000[i])*C0 + (f_101[i]+f_110[i]-f_001[i]-f_010[i])*C1) / m_dx[0];
1315     const double V4=((f_010[i]-f_000[i])*C5 + (f_111[i]-f_101[i])*C0 + (f_011[i]+f_110[i]-f_001[i]-f_100[i])*C1) / m_dx[1];
1316     const double V5=((f_110[i]-f_100[i])*C5 + (f_011[i]-f_001[i])*C0 + (f_010[i]+f_111[i]-f_000[i]-f_101[i])*C1) / m_dx[1];
1317     const double V6=((f_011[i]-f_001[i])*C5 + (f_110[i]-f_100[i])*C0 + (f_010[i]+f_111[i]-f_000[i]-f_101[i])*C1) / m_dx[1];
1318     const double V7=((f_111[i]-f_101[i])*C5 + (f_010[i]-f_000[i])*C0 + (f_011[i]+f_110[i]-f_001[i]-f_100[i])*C1) / m_dx[1];
1319     const double V8=((f_001[i]-f_000[i])*C5 + (f_111[i]-f_110[i])*C0 + (f_011[i]+f_101[i]-f_010[i]-f_100[i])*C1) / m_dx[2];
1320     const double V9=((f_101[i]-f_100[i])*C5 + (f_011[i]-f_010[i])*C0 + (f_001[i]+f_111[i]-f_000[i]-f_110[i])*C1) / m_dx[2];
1321     const double V10=((f_011[i]-f_010[i])*C5 + (f_101[i]-f_100[i])*C0 + (f_001[i]+f_111[i]-f_000[i]-f_110[i])*C1) / m_dx[2];
1322     const double V11=((f_111[i]-f_110[i])*C5 + (f_001[i]-f_000[i])*C0 + (f_011[i]+f_101[i]-f_010[i]-f_100[i])*C1) / m_dx[2];
1323 caltinay 3913 o[INDEX3(i,0,0,numComp,3)] = V0;
1324     o[INDEX3(i,1,0,numComp,3)] = V4;
1325     o[INDEX3(i,2,0,numComp,3)] = V8;
1326     o[INDEX3(i,0,1,numComp,3)] = V0;
1327     o[INDEX3(i,1,1,numComp,3)] = V5;
1328     o[INDEX3(i,2,1,numComp,3)] = V9;
1329     o[INDEX3(i,0,2,numComp,3)] = V1;
1330     o[INDEX3(i,1,2,numComp,3)] = V4;
1331     o[INDEX3(i,2,2,numComp,3)] = V10;
1332     o[INDEX3(i,0,3,numComp,3)] = V1;
1333     o[INDEX3(i,1,3,numComp,3)] = V5;
1334     o[INDEX3(i,2,3,numComp,3)] = V11;
1335     o[INDEX3(i,0,4,numComp,3)] = V2;
1336     o[INDEX3(i,1,4,numComp,3)] = V6;
1337     o[INDEX3(i,2,4,numComp,3)] = V8;
1338     o[INDEX3(i,0,5,numComp,3)] = V2;
1339     o[INDEX3(i,1,5,numComp,3)] = V7;
1340     o[INDEX3(i,2,5,numComp,3)] = V9;
1341     o[INDEX3(i,0,6,numComp,3)] = V3;
1342     o[INDEX3(i,1,6,numComp,3)] = V6;
1343     o[INDEX3(i,2,6,numComp,3)] = V10;
1344     o[INDEX3(i,0,7,numComp,3)] = V3;
1345     o[INDEX3(i,1,7,numComp,3)] = V7;
1346     o[INDEX3(i,2,7,numComp,3)] = V11;
1347     } // end of component loop i
1348     } // end of k0 loop
1349     } // end of k1 loop
1350     } // end of k2 loop
1351     } // end of parallel section
1352 caltinay 3711 } else if (out.getFunctionSpace().getTypeCode() == ReducedElements) {
1353 caltinay 3760 out.requireWrite();
1354 caltinay 3913 #pragma omp parallel
1355     {
1356     vector<double> f_000(numComp);
1357     vector<double> f_001(numComp);
1358     vector<double> f_010(numComp);
1359     vector<double> f_011(numComp);
1360     vector<double> f_100(numComp);
1361     vector<double> f_101(numComp);
1362     vector<double> f_110(numComp);
1363     vector<double> f_111(numComp);
1364     #pragma omp for
1365 caltinay 4334 for (index_t k2=0; k2 < m_NE[2]; ++k2) {
1366     for (index_t k1=0; k1 < m_NE[1]; ++k1) {
1367     for (index_t k0=0; k0 < m_NE[0]; ++k0) {
1368     memcpy(&f_000[0], in.getSampleDataRO(INDEX3(k0,k1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1369     memcpy(&f_001[0], in.getSampleDataRO(INDEX3(k0,k1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1370     memcpy(&f_010[0], in.getSampleDataRO(INDEX3(k0,k1+1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1371     memcpy(&f_011[0], in.getSampleDataRO(INDEX3(k0,k1+1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1372     memcpy(&f_100[0], in.getSampleDataRO(INDEX3(k0+1,k1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1373     memcpy(&f_101[0], in.getSampleDataRO(INDEX3(k0+1,k1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1374     memcpy(&f_110[0], in.getSampleDataRO(INDEX3(k0+1,k1+1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1375     memcpy(&f_111[0], in.getSampleDataRO(INDEX3(k0+1,k1+1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1376     double* o = out.getSampleDataRW(INDEX3(k0,k1,k2,m_NE[0],m_NE[1]));
1377 caltinay 3913 for (index_t i=0; i < numComp; ++i) {
1378 caltinay 4375 o[INDEX3(i,0,0,numComp,3)] = (f_100[i]+f_101[i]+f_110[i]+f_111[i]-f_000[i]-f_001[i]-f_010[i]-f_011[i])*C3 / m_dx[0];
1379     o[INDEX3(i,1,0,numComp,3)] = (f_010[i]+f_011[i]+f_110[i]+f_111[i]-f_000[i]-f_001[i]-f_100[i]-f_101[i])*C3 / m_dx[1];
1380     o[INDEX3(i,2,0,numComp,3)] = (f_001[i]+f_011[i]+f_101[i]+f_111[i]-f_000[i]-f_010[i]-f_100[i]-f_110[i])*C3 / m_dx[2];
1381 caltinay 3913 } // end of component loop i
1382     } // end of k0 loop
1383     } // end of k1 loop
1384     } // end of k2 loop
1385     } // end of parallel section
1386 caltinay 3707 } else if (out.getFunctionSpace().getTypeCode() == FaceElements) {
1387 caltinay 3760 out.requireWrite();
1388 caltinay 3722 #pragma omp parallel
1389     {
1390 caltinay 3913 vector<double> f_000(numComp);
1391     vector<double> f_001(numComp);
1392     vector<double> f_010(numComp);
1393     vector<double> f_011(numComp);
1394     vector<double> f_100(numComp);
1395     vector<double> f_101(numComp);
1396     vector<double> f_110(numComp);
1397     vector<double> f_111(numComp);
1398 caltinay 3722 if (m_faceOffset[0] > -1) {
1399     #pragma omp for nowait
1400 caltinay 4334 for (index_t k2=0; k2 < m_NE[2]; ++k2) {
1401     for (index_t k1=0; k1 < m_NE[1]; ++k1) {
1402     memcpy(&f_000[0], in.getSampleDataRO(INDEX3(0,k1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1403     memcpy(&f_001[0], in.getSampleDataRO(INDEX3(0,k1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1404     memcpy(&f_010[0], in.getSampleDataRO(INDEX3(0,k1+1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1405     memcpy(&f_011[0], in.getSampleDataRO(INDEX3(0,k1+1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1406     memcpy(&f_100[0], in.getSampleDataRO(INDEX3(1,k1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1407     memcpy(&f_101[0], in.getSampleDataRO(INDEX3(1,k1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1408     memcpy(&f_110[0], in.getSampleDataRO(INDEX3(1,k1+1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1409     memcpy(&f_111[0], in.getSampleDataRO(INDEX3(1,k1+1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1410     double* o = out.getSampleDataRW(m_faceOffset[0]+INDEX2(k1,k2,m_NE[1]));
1411 caltinay 3722 for (index_t i=0; i < numComp; ++i) {
1412 caltinay 4375 const double V0=((f_010[i]-f_000[i])*C6 + (f_011[i]-f_001[i])*C2) / m_dx[1];
1413     const double V1=((f_010[i]-f_000[i])*C2 + (f_011[i]-f_001[i])*C6) / m_dx[1];
1414     const double V2=((f_001[i]-f_000[i])*C6 + (f_010[i]-f_011[i])*C2) / m_dx[2];
1415     const double V3=((f_001[i]-f_000[i])*C2 + (f_011[i]-f_010[i])*C6) / m_dx[2];
1416     o[INDEX3(i,0,0,numComp,3)] = ((f_100[i]-f_000[i])*C5 + (f_111[i]-f_011[i])*C0 + (f_101[i]+f_110[i]-f_001[i]-f_010[i])*C1) / m_dx[0];
1417 caltinay 3731 o[INDEX3(i,1,0,numComp,3)] = V0;
1418     o[INDEX3(i,2,0,numComp,3)] = V2;
1419 caltinay 4375 o[INDEX3(i,0,1,numComp,3)] = ((f_110[i]-f_010[i])*C5 + (f_101[i]-f_001[i])*C0 + (f_100[i]+f_111[i]-f_000[i]-f_011[i])*C1) / m_dx[0];
1420 caltinay 3731 o[INDEX3(i,1,1,numComp,3)] = V0;
1421     o[INDEX3(i,2,1,numComp,3)] = V3;
1422 caltinay 4375 o[INDEX3(i,0,2,numComp,3)] = ((f_101[i]-f_001[i])*C5 + (f_110[i]-f_010[i])*C0 + (f_100[i]+f_111[i]-f_000[i]-f_011[i])*C1) / m_dx[0];
1423 caltinay 3731 o[INDEX3(i,1,2,numComp,3)] = V1;
1424     o[INDEX3(i,2,2,numComp,3)] = V2;
1425 caltinay 4375 o[INDEX3(i,0,3,numComp,3)] = ((f_111[i]-f_011[i])*C5 + (f_100[i]-f_000[i])*C0 + (f_101[i]+f_110[i]-f_001[i]-f_010[i])*C1) / m_dx[0];
1426 caltinay 3731 o[INDEX3(i,1,3,numComp,3)] = V1;
1427     o[INDEX3(i,2,3,numComp,3)] = V3;
1428 caltinay 3764 } // end of component loop i
1429     } // end of k1 loop
1430     } // end of k2 loop
1431     } // end of face 0
1432 caltinay 3722 if (m_faceOffset[1] > -1) {
1433     #pragma omp for nowait
1434 caltinay 4334 for (index_t k2=0; k2 < m_NE[2]; ++k2) {
1435     for (index_t k1=0; k1 < m_NE[1]; ++k1) {
1436     memcpy(&f_000[0], in.getSampleDataRO(INDEX3(m_NN[0]-2,k1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1437     memcpy(&f_001[0], in.getSampleDataRO(INDEX3(m_NN[0]-2,k1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1438     memcpy(&f_010[0], in.getSampleDataRO(INDEX3(m_NN[0]-2,k1+1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1439     memcpy(&f_011[0], in.getSampleDataRO(INDEX3(m_NN[0]-2,k1+1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1440     memcpy(&f_100[0], in.getSampleDataRO(INDEX3(m_NN[0]-1,k1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1441     memcpy(&f_101[0], in.getSampleDataRO(INDEX3(m_NN[0]-1,k1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1442     memcpy(&f_110[0], in.getSampleDataRO(INDEX3(m_NN[0]-1,k1+1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1443     memcpy(&f_111[0], in.getSampleDataRO(INDEX3(m_NN[0]-1,k1+1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1444     double* o = out.getSampleDataRW(m_faceOffset[1]+INDEX2(k1,k2,m_NE[1]));
1445 caltinay 3722 for (index_t i=0; i < numComp; ++i) {
1446 caltinay 4375 const double V0=((f_110[i]-f_100[i])*C6 + (f_111[i]-f_101[i])*C2) / m_dx[1];
1447     const double V1=((f_110[i]-f_100[i])*C2 + (f_111[i]-f_101[i])*C6) / m_dx[1];
1448     const double V2=((f_101[i]-f_100[i])*C6 + (f_111[i]-f_110[i])*C2) / m_dx[2];
1449     const double V3=((f_101[i]-f_100[i])*C2 + (f_111[i]-f_110[i])*C6) / m_dx[2];
1450     o[INDEX3(i,0,0,numComp,3)] = ((f_100[i]-f_000[i])*C5 + (f_111[i]-f_011[i])*C0 + (f_101[i]+f_110[i]-f_001[i]-f_010[i])*C1) / m_dx[0];
1451 caltinay 3731 o[INDEX3(i,1,0,numComp,3)] = V0;
1452     o[INDEX3(i,2,0,numComp,3)] = V2;
1453 caltinay 4375 o[INDEX3(i,0,1,numComp,3)] = ((f_110[i]-f_010[i])*C5 + (f_101[i]-f_001[i])*C0 + (f_100[i]+f_111[i]-f_000[i]-f_011[i])*C1) / m_dx[0];
1454 caltinay 3731 o[INDEX3(i,1,1,numComp,3)] = V0;
1455     o[INDEX3(i,2,1,numComp,3)] = V3;
1456 caltinay 4375 o[INDEX3(i,0,2,numComp,3)] = ((f_101[i]-f_001[i])*C5 + (f_110[i]-f_010[i])*C0 + (f_100[i]+f_111[i]-f_000[i]-f_011[i])*C1) / m_dx[0];
1457 caltinay 3731 o[INDEX3(i,1,2,numComp,3)] = V1;
1458     o[INDEX3(i,2,2,numComp,3)] = V2;
1459 caltinay 4375 o[INDEX3(i,0,3,numComp,3)] = ((f_111[i]-f_011[i])*C5 + (f_100[i]-f_000[i])*C0 + (f_101[i]+f_110[i]-f_001[i]-f_010[i])*C1) / m_dx[0];
1460 caltinay 3731 o[INDEX3(i,1,3,numComp,3)] = V1;
1461     o[INDEX3(i,2,3,numComp,3)] = V3;
1462 caltinay 3764 } // end of component loop i
1463     } // end of k1 loop
1464     } // end of k2 loop
1465     } // end of face 1
1466 caltinay 3722 if (m_faceOffset[2] > -1) {
1467     #pragma omp for nowait
1468 caltinay 4334 for (index_t k2=0; k2 < m_NE[2]; ++k2) {
1469     for (index_t k0=0; k0 < m_NE[0]; ++k0) {
1470     memcpy(&f_000[0], in.getSampleDataRO(INDEX3(k0,0,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1471     memcpy(&f_001[0], in.getSampleDataRO(INDEX3(k0,0,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1472     memcpy(&f_010[0], in.getSampleDataRO(INDEX3(k0,1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1473     memcpy(&f_011[0], in.getSampleDataRO(INDEX3(k0,1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1474     memcpy(&f_100[0], in.getSampleDataRO(INDEX3(k0+1,0,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1475     memcpy(&f_101[0], in.getSampleDataRO(INDEX3(k0+1,0,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1476     memcpy(&f_110[0], in.getSampleDataRO(INDEX3(k0+1,1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1477     memcpy(&f_111[0], in.getSampleDataRO(INDEX3(k0+1,1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1478     double* o = out.getSampleDataRW(m_faceOffset[2]+INDEX2(k0,k2,m_NE[0]));
1479 caltinay 3722 for (index_t i=0; i < numComp; ++i) {
1480 caltinay 4375 const double V0=((f_100[i]-f_000[i])*C6 + (f_101[i]-f_001[i])*C2) / m_dx[0];
1481     const double V1=((f_001[i]-f_000[i])*C6 + (f_101[i]-f_100[i])*C2) / m_dx[2];
1482     const double V2=((f_001[i]-f_000[i])*C2 + (f_101[i]-f_100[i])*C6) / m_dx[2];
1483 caltinay 3731 o[INDEX3(i,0,0,numComp,3)] = V0;
1484 caltinay 4375 o[INDEX3(i,1,0,numComp,3)] = ((f_010[i]-f_000[i])*C5 + (f_111[i]-f_101[i])*C0 + (f_011[i]+f_110[i]-f_001[i]-f_100[i])*C1) / m_dx[1];
1485 caltinay 3731 o[INDEX3(i,2,0,numComp,3)] = V1;
1486     o[INDEX3(i,0,1,numComp,3)] = V0;
1487 caltinay 4375 o[INDEX3(i,1,1,numComp,3)] = ((f_110[i]-f_100[i])*C5 + (f_011[i]-f_001[i])*C0 + (f_010[i]+f_111[i]-f_000[i]-f_101[i])*C1) / m_dx[1];
1488 caltinay 3731 o[INDEX3(i,2,1,numComp,3)] = V2;
1489     o[INDEX3(i,0,2,numComp,3)] = V0;
1490 caltinay 4375 o[INDEX3(i,1,2,numComp,3)] = ((f_011[i]-f_001[i])*C5 + (f_110[i]-f_100[i])*C0 + (f_010[i]+f_111[i]-f_000[i]-f_101[i])*C1) / m_dx[1];
1491 caltinay 3731 o[INDEX3(i,2,2,numComp,3)] = V1;
1492     o[INDEX3(i,0,3,numComp,3)] = V0;
1493 caltinay 4375 o[INDEX3(i,1,3,numComp,3)] = ((f_111[i]-f_101[i])*C5 + (f_010[i]-f_000[i])*C0 + (f_011[i]+f_110[i]-f_001[i]-f_100[i])*C1) / m_dx[1];
1494 caltinay 3731 o[INDEX3(i,2,3,numComp,3)] = V2;
1495 caltinay 3764 } // end of component loop i
1496     } // end of k0 loop
1497     } // end of k2 loop
1498     } // end of face 2
1499 caltinay 3722 if (m_faceOffset[3] > -1) {
1500     #pragma omp for nowait
1501 caltinay 4334 for (index_t k2=0; k2 < m_NE[2]; ++k2) {
1502     for (index_t k0=0; k0 < m_NE[0]; ++k0) {
1503     memcpy(&f_000[0], in.getSampleDataRO(INDEX3(k0,m_NN[1]-2,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1504     memcpy(&f_001[0], in.getSampleDataRO(INDEX3(k0,m_NN[1]-2,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1505     memcpy(&f_010[0], in.getSampleDataRO(INDEX3(k0,m_NN[1]-1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1506     memcpy(&f_011[0], in.getSampleDataRO(INDEX3(k0,m_NN[1]-1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1507     memcpy(&f_100[0], in.getSampleDataRO(INDEX3(k0+1,m_NN[1]-2,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1508     memcpy(&f_101[0], in.getSampleDataRO(INDEX3(k0+1,m_NN[1]-2,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1509     memcpy(&f_110[0], in.getSampleDataRO(INDEX3(k0+1,m_NN[1]-1,k2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1510     memcpy(&f_111[0], in.getSampleDataRO(INDEX3(k0+1,m_NN[1]-1,k2+1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1511     double* o = out.getSampleDataRW(m_faceOffset[3]+INDEX2(k0,k2,m_NE[0]));
1512 caltinay 3722 for (index_t i=0; i < numComp; ++i) {
1513 caltinay 4375 const double V0=((f_110[i]-f_010[i])*C6 + (f_111[i]-f_011[i])*C2) / m_dx[0];
1514     const double V1=((f_110[i]-f_010[i])*C2 + (f_111[i]-f_011[i])*C6) / m_dx[0];
1515     const double V2=((f_011[i]-f_010[i])*C6 + (f_111[i]-f_110[i])*C2) / m_dx[2];
1516     const double V3=((f_011[i]-f_010[i])*C2 + (f_111[i]-f_110[i])*C6) / m_dx[2];
1517 caltinay 3731 o[INDEX3(i,0,0,numComp,3)] = V0;
1518 caltinay 4375 o[INDEX3(i,1,0,numComp,3)] = ((f_010[i]-f_000[i])*C5 + (f_111[i]-f_101[i])*C0 + (f_011[i]+f_110[i]-f_001[i]-f_100[i])*C1) / m_dx[1];
1519 caltinay 3731 o[INDEX3(i,2,0,numComp,3)] = V2;
1520     o[INDEX3(i,0,1,numComp,3)] = V0;
1521 caltinay 4375 o[INDEX3(i,1,1,numComp,3)] = ((f_110[i]-f_100[i])*C5 + (f_011[i]-f_001[i])*C0 + (f_010[i]+f_111[i]-f_000[i]-f_101[i])*C1) / m_dx[1];
1522 caltinay 3731 o[INDEX3(i,2,1,numComp,3)] = V3;
1523     o[INDEX3(i,0,2,numComp,3)] = V1;
1524 caltinay 4375 o[INDEX3(i,1,2,numComp,3)] = ((f_011[i]-f_001[i])*C5 + (f_110[i]-f_100[i])*C0 + (f_010[i]+f_111[i]-f_000[i]-f_101[i])*C1) / m_dx[1];
1525 caltinay 3731 o[INDEX3(i,2,2,numComp,3)] = V2;
1526     o[INDEX3(i,0,3,numComp,3)] = V1;
1527 caltinay 4375 o[INDEX3(i,1,3,numComp,3)] = ((f_111[i]-f_101[i])*C5 + (f_010[i]-f_000[i])*C0 + (f_011[i]+f_110[i]-f_001[i]-f_100[i])*C1) / m_dx[1];
1528 caltinay 3731 o[INDEX3(i,2,3,numComp,3)] = V3;
1529 caltinay 3764 } // end of component loop i
1530     } // end of k0 loop
1531     } // end of k2 loop
1532     } // end of face 3
1533 caltinay 3722 if (m_faceOffset[4] > -1) {
1534     #pragma omp for nowait
1535 caltinay 4334 for (index_t k1=0; k1 < m_NE[1]; ++k1) {
1536     for (index_t k0=0; k0 < m_NE[0]; ++k0) {
1537     memcpy(&f_000[0], in.getSampleDataRO(INDEX3(k0,k1,0, m_NN[0],m_NN[1])), numComp*sizeof(double));
1538     memcpy(&f_001[0], in.getSampleDataRO(INDEX3(k0,k1,1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1539     memcpy(&f_010[0], in.getSampleDataRO(INDEX3(k0,k1+1,0, m_NN[0],m_NN[1])), numComp*sizeof(double));
1540     memcpy(&f_011[0], in.getSampleDataRO(INDEX3(k0,k1+1,1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1541     memcpy(&f_100[0], in.getSampleDataRO(INDEX3(k0+1,k1,0, m_NN[0],m_NN[1])), numComp*sizeof(double));
1542     memcpy(&f_101[0], in.getSampleDataRO(INDEX3(k0+1,k1,1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1543     memcpy(&f_110[0], in.getSampleDataRO(INDEX3(k0+1,k1+1,0, m_NN[0],m_NN[1])), numComp*sizeof(double));
1544     memcpy(&f_111[0], in.getSampleDataRO(INDEX3(k0+1,k1+1,1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1545     double* o = out.getSampleDataRW(m_faceOffset[4]+INDEX2(k0,k1,m_NE[0]));
1546 caltinay 3722 for (index_t i=0; i < numComp; ++i) {
1547 caltinay 4375 const double V0=((f_100[i]-f_000[i])*C6 + (f_110[i]-f_010[i])*C2) / m_dx[0];
1548     const double V1=((f_100[i]-f_000[i])*C2 + (f_110[i]-f_010[i])*C6) / m_dx[0];
1549     const double V2=((f_010[i]-f_000[i])*C6 + (f_110[i]-f_100[i])*C2) / m_dx[1];
1550     const double V3=((f_010[i]-f_000[i])*C2 + (f_110[i]-f_100[i])*C6) / m_dx[1];
1551 caltinay 3731 o[INDEX3(i,0,0,numComp,3)] = V0;
1552     o[INDEX3(i,1,0,numComp,3)] = V2;
1553 caltinay 4375 o[INDEX3(i,2,0,numComp,3)] = ((f_001[i]-f_000[i])*C5 + (f_111[i]-f_110[i])*C0 + (f_011[i]+f_101[i]-f_010[i]-f_100[i])*C1) / m_dx[2];
1554 caltinay 3731 o[INDEX3(i,0,1,numComp,3)] = V0;
1555     o[INDEX3(i,1,1,numComp,3)] = V3;
1556 caltinay 4375 o[INDEX3(i,2,1,numComp,3)] = ((f_101[i]-f_100[i])*C5 + (f_011[i]-f_010[i])*C0 + (f_001[i]+f_111[i]-f_000[i]-f_110[i])*C1) / m_dx[2];
1557 caltinay 3731 o[INDEX3(i,0,2,numComp,3)] = V1;
1558     o[INDEX3(i,1,2,numComp,3)] = V2;
1559 caltinay 4375 o[INDEX3(i,2,2,numComp,3)] = ((f_011[i]-f_010[i])*C5 + (f_101[i]-f_100[i])*C0 + (f_001[i]+f_111[i]-f_000[i]-f_110[i])*C1) / m_dx[2];
1560 caltinay 3731 o[INDEX3(i,0,3,numComp,3)] = V1;
1561     o[INDEX3(i,1,3,numComp,3)] = V3;
1562 caltinay 4375 o[INDEX3(i,2,3,numComp,3)] = ((f_111[i]-f_110[i])*C5 + (f_001[i]-f_000[i])*C0 + (f_011[i]+f_101[i]-f_010[i]-f_100[i])*C1) / m_dx[2];
1563 caltinay 3764 } // end of component loop i
1564     } // end of k0 loop
1565     } // end of k1 loop
1566     } // end of face 4
1567 caltinay 3722 if (m_faceOffset[5] > -1) {
1568     #pragma omp for nowait
1569 caltinay 4334 for (index_t k1=0; k1 < m_NE[1]; ++k1) {
1570     for (index_t k0=0; k0 < m_NE[0]; ++k0) {
1571     memcpy(&f_000[0], in.getSampleDataRO(INDEX3(k0,k1,m_NN[2]-2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1572     memcpy(&f_001[0], in.getSampleDataRO(INDEX3(k0,k1,m_NN[2]-1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1573     memcpy(&f_010[0], in.getSampleDataRO(INDEX3(k0,k1+1,m_NN[2]-2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1574     memcpy(&f_011[0], in.getSampleDataRO(INDEX3(k0,k1+1,m_NN[2]-1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1575     memcpy(&f_100[0], in.getSampleDataRO(INDEX3(k0+1,k1,m_NN[2]-2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1576     memcpy(&f_101[0], in.getSampleDataRO(INDEX3(k0+1,k1,m_NN[2]-1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1577     memcpy(&f_110[0], in.getSampleDataRO(INDEX3(k0+1,k1+1,m_NN[2]-2, m_NN[0],m_NN[1])), numComp*sizeof(double));
1578     memcpy(&f_111[0], in.getSampleDataRO(INDEX3(k0+1,k1+1,m_NN[2]-1, m_NN[0],m_NN[1])), numComp*sizeof(double));
1579     double* o = out.getSampleDataRW(m_faceOffset[5]+INDEX2(k0,k1,m_NE[0]));
1580 caltinay 3722 for (index_t i=0; i < numComp; ++i) {
1581 caltinay 4375 const double V0=((f_101[i]-f_001[i])*C6 + (f_111[i]-f_011[i])*C2) / m_dx[0];
1582     const double V1=((f_101[i]-f_001[i])*C2 + (f_111[i]-f_011[i])*C6) / m_dx[0];
1583     const double V2=((f_011[i]-f_001[i])*C6 + (f_111[i]-f_101[i])*C2) / m_dx[1];
1584     const double V3=((f_011[i]-f_001[i])*C2 + (f_111[i]-f_101[i])*C6) / m_dx[1];
1585 caltinay 3731 o[INDEX3(i,0,0,numComp,3)] = V0;
1586     o[INDEX3(i,1,0,numComp,3)] = V2;
1587 caltinay 4375 o[INDEX3(i,2,0,numComp,3)] = ((f_001[i]-f_000[i])*C5 + (f_111[i]-f_110[i])*C0 + (f_011[i]+f_101[i]-f_010[i]-f_100[i])*C1) / m_dx[2];
1588 caltinay 3731 o[INDEX3(i,0,1,numComp,3)] = V0;
1589     o[INDEX3(i,1,1,numComp,3)] = V3;
1590 caltinay 4375 o[INDEX3(i,2,1,numComp,3)] = ((f_011[i]-f_010[i])*C0 + (f_101[i]-f_100[i])*C5 + (f_001[i]+f_111[i]-f_000[i]-f_110[i])*C1) / m_dx[2];
1591 caltinay 3731 o[INDEX3(i,0,2,numComp,3)] = V1;
1592     o[INDEX3(i,1,2,numComp,3)] = V2;
1593 caltinay 4375 o[INDEX3(i,2,2,numComp,3)] = ((f_011[i]-f_010[i])*C5 + (f_101[i]-f_100[i])*C0 + (f_001[i]+f_111[i]-f_000[i]-f_110[i])*C1) / m_dx[2];
1594 caltinay 3731 o[INDEX3(i,0,3,numComp,3)] = V1;
1595