/[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 4622 - (hide annotations)
Fri Jan 17 04:55:41 2014 UTC (5 years, 3 months ago) by sshaw
Original Path: trunk/ripley/src/Brick.cpp
File size: 146646 byte(s)
Added dirac support to ripley, added interface for custom assemblers for ripleydomains (also added the custom assembler for 2D VTI waves), changed synthetic_VTI example to use the new, faster custom assembler

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