/[escript]/trunk/ripley/src/Rectangle.cpp
ViewVC logotype

Annotation of /trunk/ripley/src/Rectangle.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4753 - (hide annotations)
Mon Mar 17 02:39:44 2014 UTC (5 years, 7 months ago) by sshaw
File size: 89403 byte(s)
added exceptions for conflicting assembler types, added tests for lame assemblers
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 4722 #include <algorithm>
18    
19 caltinay 3691 #include <ripley/Rectangle.h>
20 caltinay 3791 #include <paso/SystemMatrix.h>
21 caltinay 4334 #include <esysUtils/esysFileWriter.h>
22 sshaw 4622 #include <ripley/DefaultAssembler2D.h>
23     #include <ripley/WaveAssembler2D.h>
24 sshaw 4712 #include <ripley/LameAssembler2D.h>
25     #include <ripley/domainhelpers.h>
26 caltinay 4477 #include <boost/scoped_array.hpp>
27 jfenwick 4521 #include "esysUtils/EsysRandom.h"
28 jfenwick 4687 #include "blocktools.h"
29 caltinay 4477
30 caltinay 4013 #ifdef USE_NETCDF
31     #include <netcdfcpp.h>
32     #endif
33    
34 caltinay 3691 #if USE_SILO
35     #include <silo.h>
36     #ifdef ESYS_MPI
37     #include <pmpio.h>
38     #endif
39     #endif
40    
41     #include <iomanip>
42    
43     using namespace std;
44 caltinay 4334 using esysUtils::FileWriter;
45 caltinay 3691
46     namespace ripley {
47    
48 caltinay 3781 Rectangle::Rectangle(int n0, int n1, double x0, double y0, double x1,
49 sshaw 4622 double y1, int d0, int d1,
50     const std::vector<double>& points,
51     const std::vector<int>& tags,
52 jfenwick 4638 const simap_t& tagnamestonums) :
53 caltinay 4334 RipleyDomain(2)
54 caltinay 3691 {
55 caltinay 3943 // ignore subdivision parameters for serial run
56     if (m_mpiInfo->size == 1) {
57     d0=1;
58     d1=1;
59     }
60    
61     bool warn=false;
62 sshaw 4712 std::vector<int> factors;
63     int ranks = m_mpiInfo->size;
64     int epr[2] = {n0,n1};
65     int d[2] = {d0,d1};
66     if (d0<=0 || d1<=0) {
67     for (int i = 0; i < 2; i++) {
68     if (d[i] < 1) {
69     d[i] = 1;
70     continue;
71 caltinay 3943 }
72 sshaw 4712 epr[i] = -1; // can no longer be max
73     //remove
74     if (ranks % d[i] != 0) {
75     throw RipleyException("Invalid number of spatial subdivisions");
76     }
77     ranks /= d[i];
78 caltinay 3943 }
79 sshaw 4712 factorise(factors, ranks);
80     if (factors.size() != 0) {
81     warn = true;
82     }
83 caltinay 3943 }
84 sshaw 4712
85     while (factors.size() > 0) {
86     int i = epr[0] > epr[1] ? 0 : 1;
87     int f = factors.back();
88     factors.pop_back();
89     d[i] *= f;
90     epr[i] /= f;
91 caltinay 3943 }
92 sshaw 4712 d0 = d[0]; d1 = d[1];
93    
94 caltinay 3691 // ensure number of subdivisions is valid and nodes can be distributed
95     // among number of ranks
96 caltinay 4334 if (d0*d1 != m_mpiInfo->size)
97 caltinay 3691 throw RipleyException("Invalid number of spatial subdivisions");
98    
99 caltinay 3943 if (warn) {
100     cout << "Warning: Automatic domain subdivision (d0=" << d0 << ", d1="
101     << d1 << "). This may not be optimal!" << endl;
102     }
103 caltinay 3691
104 caltinay 4334 double l0 = x1-x0;
105     double l1 = y1-y0;
106     m_dx[0] = l0/n0;
107     m_dx[1] = l1/n1;
108    
109     if ((n0+1)%d0 > 0) {
110 caltinay 3943 n0=(int)round((float)(n0+1)/d0+0.5)*d0-1;
111 caltinay 4334 l0=m_dx[0]*n0;
112 caltinay 3943 cout << "Warning: Adjusted number of elements and length. N0="
113 caltinay 4334 << n0 << ", l0=" << l0 << endl;
114 caltinay 3943 }
115 caltinay 4334 if ((n1+1)%d1 > 0) {
116 caltinay 3943 n1=(int)round((float)(n1+1)/d1+0.5)*d1-1;
117 caltinay 4334 l1=m_dx[1]*n1;
118 caltinay 3943 cout << "Warning: Adjusted number of elements and length. N1="
119 caltinay 4334 << n1 << ", l1=" << l1 << endl;
120 caltinay 3943 }
121    
122 caltinay 4334 if ((d0 > 1 && (n0+1)/d0<2) || (d1 > 1 && (n1+1)/d1<2))
123 caltinay 3752 throw RipleyException("Too few elements for the number of ranks");
124    
125 caltinay 4334 m_gNE[0] = n0;
126     m_gNE[1] = n1;
127     m_origin[0] = x0;
128     m_origin[1] = y0;
129     m_length[0] = l0;
130     m_length[1] = l1;
131     m_NX[0] = d0;
132     m_NX[1] = d1;
133    
134 caltinay 3764 // local number of elements (with and without overlap)
135 caltinay 4334 m_NE[0] = m_ownNE[0] = (d0>1 ? (n0+1)/d0 : n0);
136     if (m_mpiInfo->rank%d0>0 && m_mpiInfo->rank%d0<d0-1)
137     m_NE[0]++;
138     else if (d0>1 && m_mpiInfo->rank%d0==d0-1)
139     m_ownNE[0]--;
140 caltinay 3764
141 caltinay 4334 m_NE[1] = m_ownNE[1] = (d1>1 ? (n1+1)/d1 : n1);
142     if (m_mpiInfo->rank/d0>0 && m_mpiInfo->rank/d0<d1-1)
143     m_NE[1]++;
144     else if (d1>1 && m_mpiInfo->rank/d0==d1-1)
145     m_ownNE[1]--;
146 caltinay 3752
147     // local number of nodes
148 caltinay 4334 m_NN[0] = m_NE[0]+1;
149     m_NN[1] = m_NE[1]+1;
150 caltinay 3752
151 caltinay 3691 // bottom-left node is at (offset0,offset1) in global mesh
152 caltinay 4334 m_offset[0] = (n0+1)/d0*(m_mpiInfo->rank%d0);
153     if (m_offset[0] > 0)
154     m_offset[0]--;
155     m_offset[1] = (n1+1)/d1*(m_mpiInfo->rank/d0);
156     if (m_offset[1] > 0)
157     m_offset[1]--;
158 caltinay 3752
159 caltinay 3691 populateSampleIds();
160 caltinay 3756 createPattern();
161 sshaw 4622 assembler = new DefaultAssembler2D(this, m_dx, m_NX, m_NE, m_NN);
162     for (map<string, int>::const_iterator i = tagnamestonums.begin();
163     i != tagnamestonums.end(); i++) {
164     setTagMap(i->first, i->second);
165     }
166     addPoints(tags.size(), &points[0], &tags[0]);
167 caltinay 3691 }
168    
169     Rectangle::~Rectangle()
170     {
171 caltinay 3785 Paso_SystemMatrixPattern_free(m_pattern);
172     Paso_Connector_free(m_connector);
173 sshaw 4622 delete assembler;
174 caltinay 3691 }
175    
176     string Rectangle::getDescription() const
177     {
178     return "ripley::Rectangle";
179     }
180    
181     bool Rectangle::operator==(const AbstractDomain& other) const
182     {
183 caltinay 3744 const Rectangle* o=dynamic_cast<const Rectangle*>(&other);
184     if (o) {
185     return (RipleyDomain::operator==(other) &&
186 caltinay 4334 m_gNE[0]==o->m_gNE[0] && m_gNE[1]==o->m_gNE[1]
187     && m_origin[0]==o->m_origin[0] && m_origin[1]==o->m_origin[1]
188     && m_length[0]==o->m_length[0] && m_length[1]==o->m_length[1]
189     && m_NX[0]==o->m_NX[0] && m_NX[1]==o->m_NX[1]);
190 caltinay 3744 }
191 caltinay 3691
192     return false;
193     }
194    
195 caltinay 4013 void Rectangle::readNcGrid(escript::Data& out, string filename, string varname,
196 caltinay 4618 const ReaderParameters& params) const
197 caltinay 4013 {
198     #ifdef USE_NETCDF
199     // check destination function space
200     int myN0, myN1;
201     if (out.getFunctionSpace().getTypeCode() == Nodes) {
202 caltinay 4334 myN0 = m_NN[0];
203     myN1 = m_NN[1];
204 caltinay 4013 } else if (out.getFunctionSpace().getTypeCode() == Elements ||
205     out.getFunctionSpace().getTypeCode() == ReducedElements) {
206 caltinay 4334 myN0 = m_NE[0];
207     myN1 = m_NE[1];
208 caltinay 4013 } else
209     throw RipleyException("readNcGrid(): invalid function space for output data object");
210    
211 caltinay 4615 if (params.first.size() != 2)
212 caltinay 4013 throw RipleyException("readNcGrid(): argument 'first' must have 2 entries");
213    
214 caltinay 4615 if (params.numValues.size() != 2)
215 caltinay 4013 throw RipleyException("readNcGrid(): argument 'numValues' must have 2 entries");
216    
217 caltinay 4615 if (params.multiplier.size() != 2)
218 caltinay 4277 throw RipleyException("readNcGrid(): argument 'multiplier' must have 2 entries");
219 caltinay 4615 for (size_t i=0; i<params.multiplier.size(); i++)
220     if (params.multiplier[i]<1)
221 caltinay 4277 throw RipleyException("readNcGrid(): all multipliers must be positive");
222 caltinay 4618 if (params.reverse.size() != 2)
223     throw RipleyException("readNcGrid(): argument 'reverse' must have 2 entries");
224 caltinay 4277
225 caltinay 4013 // check file existence and size
226     NcFile f(filename.c_str(), NcFile::ReadOnly);
227     if (!f.is_valid())
228     throw RipleyException("readNcGrid(): cannot open file");
229    
230     NcVar* var = f.get_var(varname.c_str());
231     if (!var)
232     throw RipleyException("readNcGrid(): invalid variable");
233    
234     // TODO: rank>0 data support
235     const int numComp = out.getDataPointSize();
236     if (numComp > 1)
237     throw RipleyException("readNcGrid(): only scalar data supported");
238    
239     const int dims = var->num_dims();
240 caltinay 4477 boost::scoped_array<long> edges(var->edges());
241 caltinay 4013
242     // is this a slice of the data object (dims!=2)?
243     // note the expected ordering of edges (as in numpy: y,x)
244 caltinay 4615 if ( (dims==2 && (params.numValues[1] > edges[0] || params.numValues[0] > edges[1]))
245     || (dims==1 && params.numValues[1]>1) ) {
246 caltinay 4013 throw RipleyException("readNcGrid(): not enough data in file");
247     }
248    
249     // check if this rank contributes anything
250 caltinay 4615 if (params.first[0] >= m_offset[0]+myN0 ||
251     params.first[0]+params.numValues[0]*params.multiplier[0] <= m_offset[0] ||
252     params.first[1] >= m_offset[1]+myN1 ||
253     params.first[1]+params.numValues[1]*params.multiplier[1] <= m_offset[1])
254 caltinay 4013 return;
255    
256     // now determine how much this rank has to write
257    
258     // first coordinates in data object to write to
259 caltinay 4615 const int first0 = max(0, params.first[0]-m_offset[0]);
260     const int first1 = max(0, params.first[1]-m_offset[1]);
261 caltinay 4618 // indices to first value in file (not accounting for reverse yet)
262     int idx0 = max(0, m_offset[0]-params.first[0]);
263     int idx1 = max(0, m_offset[1]-params.first[1]);
264 caltinay 4277 // number of values to read
265 caltinay 4615 const int num0 = min(params.numValues[0]-idx0, myN0-first0);
266     const int num1 = min(params.numValues[1]-idx1, myN1-first1);
267 caltinay 4013
268 caltinay 4618 // make sure we read the right block if going backwards through file
269     if (params.reverse[0])
270     idx0 = edges[dims-1]-num0-idx0;
271     if (dims>1 && params.reverse[1])
272     idx1 = edges[dims-2]-num1-idx1;
273    
274 caltinay 4013 vector<double> values(num0*num1);
275     if (dims==2) {
276     var->set_cur(idx1, idx0);
277     var->get(&values[0], num1, num0);
278     } else {
279     var->set_cur(idx0);
280     var->get(&values[0], num0);
281     }
282    
283     const int dpp = out.getNumDataPointsPerSample();
284     out.requireWrite();
285    
286 caltinay 4618 // helpers for reversing
287     const int x0 = (params.reverse[0] ? num0-1 : 0);
288     const int x_mult = (params.reverse[0] ? -1 : 1);
289     const int y0 = (params.reverse[1] ? num1-1 : 0);
290     const int y_mult = (params.reverse[1] ? -1 : 1);
291    
292 caltinay 4013 for (index_t y=0; y<num1; y++) {
293     #pragma omp parallel for
294     for (index_t x=0; x<num0; x++) {
295 caltinay 4615 const int baseIndex = first0+x*params.multiplier[0]
296     +(first1+y*params.multiplier[1])*myN0;
297 caltinay 4618 const int srcIndex = (y0+y_mult*y)*num0+(x0+x_mult*x);
298 caltinay 4174 if (!isnan(values[srcIndex])) {
299 caltinay 4615 for (index_t m1=0; m1<params.multiplier[1]; m1++) {
300     for (index_t m0=0; m0<params.multiplier[0]; m0++) {
301 caltinay 4277 const int dataIndex = baseIndex+m0+m1*myN0;
302     double* dest = out.getSampleDataRW(dataIndex);
303     for (index_t q=0; q<dpp; q++) {
304     *dest++ = values[srcIndex];
305     }
306     }
307 caltinay 4174 }
308 caltinay 4013 }
309     }
310     }
311     #else
312     throw RipleyException("readNcGrid(): not compiled with netCDF support");
313     #endif
314     }
315    
316 caltinay 3971 void Rectangle::readBinaryGrid(escript::Data& out, string filename,
317 caltinay 4618 const ReaderParameters& params) const
318 caltinay 3971 {
319 caltinay 4495 // the mapping is not universally correct but should work on our
320     // supported platforms
321 caltinay 4615 switch (params.dataType) {
322 caltinay 4495 case DATATYPE_INT32:
323 caltinay 4615 readBinaryGridImpl<int>(out, filename, params);
324 caltinay 4495 break;
325     case DATATYPE_FLOAT32:
326 caltinay 4615 readBinaryGridImpl<float>(out, filename, params);
327 caltinay 4495 break;
328     case DATATYPE_FLOAT64:
329 caltinay 4615 readBinaryGridImpl<double>(out, filename, params);
330 caltinay 4495 break;
331     default:
332     throw RipleyException("readBinaryGrid(): invalid or unsupported datatype");
333     }
334     }
335    
336     template<typename ValueType>
337     void Rectangle::readBinaryGridImpl(escript::Data& out, const string& filename,
338 caltinay 4618 const ReaderParameters& params) const
339 caltinay 4495 {
340 caltinay 3971 // check destination function space
341     int myN0, myN1;
342     if (out.getFunctionSpace().getTypeCode() == Nodes) {
343 caltinay 4334 myN0 = m_NN[0];
344     myN1 = m_NN[1];
345 caltinay 3971 } else if (out.getFunctionSpace().getTypeCode() == Elements ||
346     out.getFunctionSpace().getTypeCode() == ReducedElements) {
347 caltinay 4334 myN0 = m_NE[0];
348     myN1 = m_NE[1];
349 caltinay 3971 } else
350     throw RipleyException("readBinaryGrid(): invalid function space for output data object");
351    
352     // check file existence and size
353     ifstream f(filename.c_str(), ifstream::binary);
354     if (f.fail()) {
355     throw RipleyException("readBinaryGrid(): cannot open file");
356     }
357     f.seekg(0, ios::end);
358     const int numComp = out.getDataPointSize();
359     const int filesize = f.tellg();
360 caltinay 4615 const int reqsize = params.numValues[0]*params.numValues[1]*numComp*sizeof(ValueType);
361 caltinay 3971 if (filesize < reqsize) {
362     f.close();
363     throw RipleyException("readBinaryGrid(): not enough data in file");
364     }
365    
366     // check if this rank contributes anything
367 caltinay 4615 if (params.first[0] >= m_offset[0]+myN0 ||
368     params.first[0]+params.numValues[0] <= m_offset[0] ||
369     params.first[1] >= m_offset[1]+myN1 ||
370     params.first[1]+params.numValues[1] <= m_offset[1]) {
371 caltinay 3971 f.close();
372     return;
373     }
374    
375     // now determine how much this rank has to write
376    
377     // first coordinates in data object to write to
378 caltinay 4615 const int first0 = max(0, params.first[0]-m_offset[0]);
379     const int first1 = max(0, params.first[1]-m_offset[1]);
380 caltinay 3971 // indices to first value in file
381 caltinay 4615 const int idx0 = max(0, m_offset[0]-params.first[0]);
382     const int idx1 = max(0, m_offset[1]-params.first[1]);
383 caltinay 4277 // number of values to read
384 caltinay 4615 const int num0 = min(params.numValues[0]-idx0, myN0-first0);
385     const int num1 = min(params.numValues[1]-idx1, myN1-first1);
386 caltinay 3971
387     out.requireWrite();
388 caltinay 4495 vector<ValueType> values(num0*numComp);
389 caltinay 3971 const int dpp = out.getNumDataPointsPerSample();
390    
391 caltinay 4529 for (int y=0; y<num1; y++) {
392 caltinay 4615 const int fileofs = numComp*(idx0+(idx1+y)*params.numValues[0]);
393 caltinay 4495 f.seekg(fileofs*sizeof(ValueType));
394     f.read((char*)&values[0], num0*numComp*sizeof(ValueType));
395     for (int x=0; x<num0; x++) {
396 caltinay 4615 const int baseIndex = first0+x*params.multiplier[0]
397     +(first1+y*params.multiplier[1])*myN0;
398     for (int m1=0; m1<params.multiplier[1]; m1++) {
399     for (int m0=0; m0<params.multiplier[0]; m0++) {
400 caltinay 4277 const int dataIndex = baseIndex+m0+m1*myN0;
401     double* dest = out.getSampleDataRW(dataIndex);
402 caltinay 4495 for (int c=0; c<numComp; c++) {
403 caltinay 4529 ValueType val = values[x*numComp+c];
404    
405 caltinay 4615 if (params.byteOrder != BYTEORDER_NATIVE) {
406 caltinay 4529 char* cval = reinterpret_cast<char*>(&val);
407     // this will alter val!!
408     byte_swap32(cval);
409     }
410     if (!std::isnan(val)) {
411 caltinay 4495 for (int q=0; q<dpp; q++) {
412 caltinay 4529 *dest++ = static_cast<double>(val);
413 caltinay 4277 }
414     }
415 caltinay 4174 }
416 caltinay 3971 }
417     }
418     }
419     }
420    
421     f.close();
422     }
423    
424 caltinay 4357 void Rectangle::writeBinaryGrid(const escript::Data& in, string filename,
425     int byteOrder, int dataType) const
426 caltinay 4334 {
427 caltinay 4357 // the mapping is not universally correct but should work on our
428     // supported platforms
429     switch (dataType) {
430     case DATATYPE_INT32:
431     writeBinaryGridImpl<int>(in, filename, byteOrder);
432     break;
433     case DATATYPE_FLOAT32:
434     writeBinaryGridImpl<float>(in, filename, byteOrder);
435     break;
436     case DATATYPE_FLOAT64:
437     writeBinaryGridImpl<double>(in, filename, byteOrder);
438     break;
439     default:
440     throw RipleyException("writeBinaryGrid(): invalid or unsupported datatype");
441     }
442     }
443    
444     template<typename ValueType>
445     void Rectangle::writeBinaryGridImpl(const escript::Data& in,
446     const string& filename, int byteOrder) const
447     {
448 caltinay 4334 // check function space and determine number of points
449     int myN0, myN1;
450     int totalN0, totalN1;
451     if (in.getFunctionSpace().getTypeCode() == Nodes) {
452     myN0 = m_NN[0];
453     myN1 = m_NN[1];
454     totalN0 = m_gNE[0]+1;
455     totalN1 = m_gNE[1]+1;
456     } else if (in.getFunctionSpace().getTypeCode() == Elements ||
457     in.getFunctionSpace().getTypeCode() == ReducedElements) {
458     myN0 = m_NE[0];
459     myN1 = m_NE[1];
460     totalN0 = m_gNE[0];
461     totalN1 = m_gNE[1];
462     } else
463     throw RipleyException("writeBinaryGrid(): invalid function space of data object");
464    
465     const int numComp = in.getDataPointSize();
466     const int dpp = in.getNumDataPointsPerSample();
467    
468     if (numComp > 1 || dpp > 1)
469     throw RipleyException("writeBinaryGrid(): only scalar, single-value data supported");
470    
471 caltinay 4357 const int fileSize = sizeof(ValueType)*numComp*dpp*totalN0*totalN1;
472 caltinay 4334
473     // from here on we know that each sample consists of one value
474 caltinay 4482 FileWriter fw;
475     fw.openFile(filename, fileSize);
476 caltinay 4334 MPIBarrier();
477    
478     for (index_t y=0; y<myN1; y++) {
479 caltinay 4357 const int fileofs = (m_offset[0]+(m_offset[1]+y)*totalN0)*sizeof(ValueType);
480 caltinay 4334 ostringstream oss;
481    
482     for (index_t x=0; x<myN0; x++) {
483 caltinay 4626 const double* sample = in.getSampleDataRO(y*myN0+x);
484 caltinay 4357 ValueType fvalue = static_cast<ValueType>(*sample);
485     if (byteOrder == BYTEORDER_NATIVE) {
486 caltinay 4334 oss.write((char*)&fvalue, sizeof(fvalue));
487     } else {
488     char* value = reinterpret_cast<char*>(&fvalue);
489 caltinay 4357 oss.write(byte_swap32(value), sizeof(fvalue));
490 caltinay 4334 }
491     }
492 caltinay 4482 fw.writeAt(oss, fileofs);
493 caltinay 4334 }
494 caltinay 4482 fw.close();
495 caltinay 4334 }
496    
497 caltinay 3691 void Rectangle::dump(const string& fileName) const
498     {
499     #if USE_SILO
500     string fn(fileName);
501     if (fileName.length() < 6 || fileName.compare(fileName.length()-5, 5, ".silo") != 0) {
502     fn+=".silo";
503     }
504    
505 jfenwick 4687 int driver=DB_HDF5;
506 caltinay 3691 DBfile* dbfile = NULL;
507 gross 3793 const char* blockDirFmt = "/block%04d";
508 caltinay 3691
509     #ifdef ESYS_MPI
510     PMPIO_baton_t* baton = NULL;
511 gross 3793 const int NUM_SILO_FILES = 1;
512 caltinay 3691 #endif
513    
514     if (m_mpiInfo->size > 1) {
515     #ifdef ESYS_MPI
516     baton = PMPIO_Init(NUM_SILO_FILES, PMPIO_WRITE, m_mpiInfo->comm,
517     0x1337, PMPIO_DefaultCreate, PMPIO_DefaultOpen,
518     PMPIO_DefaultClose, (void*)&driver);
519     // try the fallback driver in case of error
520     if (!baton && driver != DB_PDB) {
521     driver = DB_PDB;
522     baton = PMPIO_Init(NUM_SILO_FILES, PMPIO_WRITE, m_mpiInfo->comm,
523     0x1338, PMPIO_DefaultCreate, PMPIO_DefaultOpen,
524     PMPIO_DefaultClose, (void*)&driver);
525     }
526     if (baton) {
527 caltinay 3766 char siloPath[64];
528     snprintf(siloPath, 64, blockDirFmt, PMPIO_RankInGroup(baton, m_mpiInfo->rank));
529     dbfile = (DBfile*) PMPIO_WaitForBaton(baton, fn.c_str(), siloPath);
530 caltinay 3691 }
531     #endif
532     } else {
533     dbfile = DBCreate(fn.c_str(), DB_CLOBBER, DB_LOCAL,
534     getDescription().c_str(), driver);
535     // try the fallback driver in case of error
536     if (!dbfile && driver != DB_PDB) {
537     driver = DB_PDB;
538     dbfile = DBCreate(fn.c_str(), DB_CLOBBER, DB_LOCAL,
539     getDescription().c_str(), driver);
540     }
541 caltinay 3766 char siloPath[64];
542     snprintf(siloPath, 64, blockDirFmt, 0);
543     DBMkDir(dbfile, siloPath);
544     DBSetDir(dbfile, siloPath);
545 caltinay 3691 }
546    
547     if (!dbfile)
548     throw RipleyException("dump: Could not create Silo file");
549    
550     /*
551     if (driver==DB_HDF5) {
552     // gzip level 1 already provides good compression with minimal
553     // performance penalty. Some tests showed that gzip levels >3 performed
554     // rather badly on escript data both in terms of time and space
555     DBSetCompression("ERRMODE=FALLBACK METHOD=GZIP LEVEL=1");
556     }
557     */
558    
559 caltinay 4334 boost::scoped_ptr<double> x(new double[m_NN[0]]);
560     boost::scoped_ptr<double> y(new double[m_NN[1]]);
561 caltinay 3691 double* coords[2] = { x.get(), y.get() };
562     #pragma omp parallel
563     {
564 caltinay 3722 #pragma omp for nowait
565 caltinay 4334 for (dim_t i0 = 0; i0 < m_NN[0]; i0++) {
566     coords[0][i0]=getLocalCoordinate(i0, 0);
567 caltinay 3691 }
568 caltinay 3722 #pragma omp for nowait
569 caltinay 4334 for (dim_t i1 = 0; i1 < m_NN[1]; i1++) {
570     coords[1][i1]=getLocalCoordinate(i1, 1);
571 caltinay 3691 }
572     }
573 caltinay 4334 int* dims = const_cast<int*>(getNumNodesPerDim());
574 caltinay 3697
575     // write mesh
576 caltinay 4334 DBPutQuadmesh(dbfile, "mesh", NULL, coords, dims, 2, DB_DOUBLE,
577 caltinay 3691 DB_COLLINEAR, NULL);
578    
579 caltinay 3697 // write node ids
580 caltinay 4334 DBPutQuadvar1(dbfile, "nodeId", "mesh", (void*)&m_nodeId[0], dims, 2,
581 caltinay 3697 NULL, 0, DB_INT, DB_NODECENT, NULL);
582    
583     // write element ids
584 caltinay 4334 dims = const_cast<int*>(getNumElementsPerDim());
585 caltinay 3697 DBPutQuadvar1(dbfile, "elementId", "mesh", (void*)&m_elementId[0],
586 caltinay 4334 dims, 2, NULL, 0, DB_INT, DB_ZONECENT, NULL);
587 caltinay 3697
588     // rank 0 writes multimesh and multivar
589 caltinay 3691 if (m_mpiInfo->rank == 0) {
590     vector<string> tempstrings;
591 caltinay 3697 vector<char*> names;
592 caltinay 3691 for (dim_t i=0; i<m_mpiInfo->size; i++) {
593     stringstream path;
594     path << "/block" << setw(4) << setfill('0') << right << i << "/mesh";
595     tempstrings.push_back(path.str());
596 caltinay 3697 names.push_back((char*)tempstrings.back().c_str());
597 caltinay 3691 }
598 caltinay 3697 vector<int> types(m_mpiInfo->size, DB_QUAD_RECT);
599 caltinay 3691 DBSetDir(dbfile, "/");
600 caltinay 3697 DBPutMultimesh(dbfile, "multimesh", m_mpiInfo->size, &names[0],
601     &types[0], NULL);
602     tempstrings.clear();
603     names.clear();
604     for (dim_t i=0; i<m_mpiInfo->size; i++) {
605     stringstream path;
606     path << "/block" << setw(4) << setfill('0') << right << i << "/nodeId";
607     tempstrings.push_back(path.str());
608     names.push_back((char*)tempstrings.back().c_str());
609     }
610     types.assign(m_mpiInfo->size, DB_QUADVAR);
611     DBPutMultivar(dbfile, "nodeId", m_mpiInfo->size, &names[0],
612     &types[0], NULL);
613     tempstrings.clear();
614     names.clear();
615     for (dim_t i=0; i<m_mpiInfo->size; i++) {
616     stringstream path;
617     path << "/block" << setw(4) << setfill('0') << right << i << "/elementId";
618     tempstrings.push_back(path.str());
619     names.push_back((char*)tempstrings.back().c_str());
620     }
621     DBPutMultivar(dbfile, "elementId", m_mpiInfo->size, &names[0],
622     &types[0], NULL);
623 caltinay 3691 }
624    
625     if (m_mpiInfo->size > 1) {
626     #ifdef ESYS_MPI
627     PMPIO_HandOffBaton(baton, dbfile);
628     PMPIO_Finish(baton);
629     #endif
630     } else {
631     DBClose(dbfile);
632     }
633    
634     #else // USE_SILO
635 caltinay 3791 throw RipleyException("dump: no Silo support");
636 caltinay 3691 #endif
637     }
638    
639 caltinay 3697 const int* Rectangle::borrowSampleReferenceIDs(int fsType) const
640 caltinay 3691 {
641 caltinay 3697 switch (fsType) {
642 caltinay 3691 case Nodes:
643 caltinay 3769 case ReducedNodes: // FIXME: reduced
644 caltinay 3691 return &m_nodeId[0];
645 caltinay 3750 case DegreesOfFreedom:
646 caltinay 3769 case ReducedDegreesOfFreedom: // FIXME: reduced
647 caltinay 3750 return &m_dofId[0];
648 caltinay 3691 case Elements:
649 caltinay 3733 case ReducedElements:
650 caltinay 3691 return &m_elementId[0];
651     case FaceElements:
652 caltinay 3733 case ReducedFaceElements:
653 caltinay 3691 return &m_faceId[0];
654 sshaw 4629 case Points:
655     return &m_diracPointNodeIDs[0];
656 caltinay 3691 default:
657     break;
658     }
659    
660     stringstream msg;
661 caltinay 3791 msg << "borrowSampleReferenceIDs: invalid function space type " << fsType;
662 caltinay 3691 throw RipleyException(msg.str());
663     }
664    
665 caltinay 3757 bool Rectangle::ownSample(int fsType, index_t id) const
666 caltinay 3691 {
667 caltinay 3759 if (getMPISize()==1)
668     return true;
669    
670 caltinay 3757 switch (fsType) {
671     case Nodes:
672 caltinay 3769 case ReducedNodes: // FIXME: reduced
673 caltinay 3757 return (m_dofMap[id] < getNumDOF());
674     case DegreesOfFreedom:
675     case ReducedDegreesOfFreedom:
676     return true;
677     case Elements:
678     case ReducedElements:
679     // check ownership of element's bottom left node
680 caltinay 4334 return (m_dofMap[id%m_NE[0]+m_NN[0]*(id/m_NE[0])] < getNumDOF());
681 caltinay 3757 case FaceElements:
682     case ReducedFaceElements:
683 caltinay 3759 {
684 caltinay 3764 // determine which face the sample belongs to before
685 caltinay 3768 // checking ownership of corresponding element's first node
686 caltinay 3759 dim_t n=0;
687 caltinay 4334 for (size_t i=0; i<4; i++) {
688     n+=m_faceCount[i];
689 caltinay 3759 if (id<n) {
690     index_t k;
691     if (i==1)
692 caltinay 4334 k=m_NN[0]-2;
693 caltinay 3759 else if (i==3)
694 caltinay 4334 k=m_NN[0]*(m_NN[1]-2);
695 caltinay 3759 else
696     k=0;
697     // determine whether to move right or up
698 caltinay 4334 const index_t delta=(i/2==0 ? m_NN[0] : 1);
699     return (m_dofMap[k+(id-n+m_faceCount[i])*delta] < getNumDOF());
700 caltinay 3759 }
701     }
702     return false;
703     }
704 caltinay 3757 default:
705     break;
706 caltinay 3702 }
707 caltinay 3757
708     stringstream msg;
709 caltinay 3791 msg << "ownSample: invalid function space type " << fsType;
710 caltinay 3757 throw RipleyException(msg.str());
711 caltinay 3691 }
712    
713 caltinay 3764 void Rectangle::setToNormal(escript::Data& out) const
714 caltinay 3691 {
715 caltinay 3764 if (out.getFunctionSpace().getTypeCode() == FaceElements) {
716     out.requireWrite();
717     #pragma omp parallel
718     {
719     if (m_faceOffset[0] > -1) {
720     #pragma omp for nowait
721 caltinay 4334 for (index_t k1 = 0; k1 < m_NE[1]; ++k1) {
722 caltinay 3764 double* o = out.getSampleDataRW(m_faceOffset[0]+k1);
723     // set vector at two quadrature points
724     *o++ = -1.;
725     *o++ = 0.;
726     *o++ = -1.;
727     *o = 0.;
728     }
729     }
730    
731     if (m_faceOffset[1] > -1) {
732     #pragma omp for nowait
733 caltinay 4334 for (index_t k1 = 0; k1 < m_NE[1]; ++k1) {
734 caltinay 3764 double* o = out.getSampleDataRW(m_faceOffset[1]+k1);
735     // set vector at two quadrature points
736     *o++ = 1.;
737     *o++ = 0.;
738     *o++ = 1.;
739     *o = 0.;
740     }
741     }
742    
743     if (m_faceOffset[2] > -1) {
744     #pragma omp for nowait
745 caltinay 4334 for (index_t k0 = 0; k0 < m_NE[0]; ++k0) {
746 caltinay 3764 double* o = out.getSampleDataRW(m_faceOffset[2]+k0);
747     // set vector at two quadrature points
748     *o++ = 0.;
749     *o++ = -1.;
750     *o++ = 0.;
751     *o = -1.;
752     }
753     }
754    
755     if (m_faceOffset[3] > -1) {
756     #pragma omp for nowait
757 caltinay 4334 for (index_t k0 = 0; k0 < m_NE[0]; ++k0) {
758 caltinay 3764 double* o = out.getSampleDataRW(m_faceOffset[3]+k0);
759     // set vector at two quadrature points
760     *o++ = 0.;
761     *o++ = 1.;
762     *o++ = 0.;
763     *o = 1.;
764     }
765     }
766     } // end of parallel section
767     } else if (out.getFunctionSpace().getTypeCode() == ReducedFaceElements) {
768     out.requireWrite();
769     #pragma omp parallel
770     {
771     if (m_faceOffset[0] > -1) {
772     #pragma omp for nowait
773 caltinay 4334 for (index_t k1 = 0; k1 < m_NE[1]; ++k1) {
774 caltinay 3764 double* o = out.getSampleDataRW(m_faceOffset[0]+k1);
775     *o++ = -1.;
776     *o = 0.;
777     }
778     }
779    
780     if (m_faceOffset[1] > -1) {
781     #pragma omp for nowait
782 caltinay 4334 for (index_t k1 = 0; k1 < m_NE[1]; ++k1) {
783 caltinay 3764 double* o = out.getSampleDataRW(m_faceOffset[1]+k1);
784     *o++ = 1.;
785     *o = 0.;
786     }
787     }
788    
789     if (m_faceOffset[2] > -1) {
790     #pragma omp for nowait
791 caltinay 4334 for (index_t k0 = 0; k0 < m_NE[0]; ++k0) {
792 caltinay 3764 double* o = out.getSampleDataRW(m_faceOffset[2]+k0);
793     *o++ = 0.;
794     *o = -1.;
795     }
796     }
797    
798     if (m_faceOffset[3] > -1) {
799     #pragma omp for nowait
800 caltinay 4334 for (index_t k0 = 0; k0 < m_NE[0]; ++k0) {
801 caltinay 3764 double* o = out.getSampleDataRW(m_faceOffset[3]+k0);
802     *o++ = 0.;
803     *o = 1.;
804     }
805     }
806     } // end of parallel section
807    
808     } else {
809     stringstream msg;
810 caltinay 3791 msg << "setToNormal: invalid function space type "
811     << out.getFunctionSpace().getTypeCode();
812 caltinay 3764 throw RipleyException(msg.str());
813     }
814     }
815    
816     void Rectangle::setToSize(escript::Data& out) const
817     {
818     if (out.getFunctionSpace().getTypeCode() == Elements
819     || out.getFunctionSpace().getTypeCode() == ReducedElements) {
820     out.requireWrite();
821     const dim_t numQuad=out.getNumDataPointsPerSample();
822 caltinay 4334 const double size=sqrt(m_dx[0]*m_dx[0]+m_dx[1]*m_dx[1]);
823 caltinay 3764 #pragma omp parallel for
824     for (index_t k = 0; k < getNumElements(); ++k) {
825     double* o = out.getSampleDataRW(k);
826     fill(o, o+numQuad, size);
827     }
828     } else if (out.getFunctionSpace().getTypeCode() == FaceElements
829     || out.getFunctionSpace().getTypeCode() == ReducedFaceElements) {
830     out.requireWrite();
831     const dim_t numQuad=out.getNumDataPointsPerSample();
832     #pragma omp parallel
833     {
834     if (m_faceOffset[0] > -1) {
835     #pragma omp for nowait
836 caltinay 4334 for (index_t k1 = 0; k1 < m_NE[1]; ++k1) {
837 caltinay 3764 double* o = out.getSampleDataRW(m_faceOffset[0]+k1);
838 caltinay 4334 fill(o, o+numQuad, m_dx[1]);
839 caltinay 3764 }
840     }
841    
842     if (m_faceOffset[1] > -1) {
843     #pragma omp for nowait
844 caltinay 4334 for (index_t k1 = 0; k1 < m_NE[1]; ++k1) {
845 caltinay 3764 double* o = out.getSampleDataRW(m_faceOffset[1]+k1);
846 caltinay 4334 fill(o, o+numQuad, m_dx[1]);
847 caltinay 3764 }
848     }
849    
850     if (m_faceOffset[2] > -1) {
851     #pragma omp for nowait
852 caltinay 4334 for (index_t k0 = 0; k0 < m_NE[0]; ++k0) {
853 caltinay 3764 double* o = out.getSampleDataRW(m_faceOffset[2]+k0);
854 caltinay 4334 fill(o, o+numQuad, m_dx[0]);
855 caltinay 3764 }
856     }
857    
858     if (m_faceOffset[3] > -1) {
859     #pragma omp for nowait
860 caltinay 4334 for (index_t k0 = 0; k0 < m_NE[0]; ++k0) {
861 caltinay 3764 double* o = out.getSampleDataRW(m_faceOffset[3]+k0);
862 caltinay 4334 fill(o, o+numQuad, m_dx[0]);
863 caltinay 3764 }
864     }
865     } // end of parallel section
866    
867     } else {
868     stringstream msg;
869 caltinay 3791 msg << "setToSize: invalid function space type "
870     << out.getFunctionSpace().getTypeCode();
871 caltinay 3764 throw RipleyException(msg.str());
872     }
873     }
874    
875     void Rectangle::Print_Mesh_Info(const bool full) const
876     {
877     RipleyDomain::Print_Mesh_Info(full);
878     if (full) {
879     cout << " Id Coordinates" << endl;
880     cout.precision(15);
881     cout.setf(ios::scientific, ios::floatfield);
882     for (index_t i=0; i < getNumNodes(); i++) {
883     cout << " " << setw(5) << m_nodeId[i]
884 caltinay 4334 << " " << getLocalCoordinate(i%m_NN[0], 0)
885     << " " << getLocalCoordinate(i/m_NN[0], 1) << endl;
886 caltinay 3764 }
887     }
888     }
889    
890    
891     //protected
892     void Rectangle::assembleCoordinates(escript::Data& arg) const
893     {
894     escriptDataC x = arg.getDataC();
895     int numDim = m_numDim;
896     if (!isDataPointShapeEqual(&x, 1, &numDim))
897     throw RipleyException("setToX: Invalid Data object shape");
898     if (!numSamplesEqual(&x, 1, getNumNodes()))
899     throw RipleyException("setToX: Illegal number of samples in Data object");
900    
901     arg.requireWrite();
902     #pragma omp parallel for
903 caltinay 4334 for (dim_t i1 = 0; i1 < m_NN[1]; i1++) {
904     for (dim_t i0 = 0; i0 < m_NN[0]; i0++) {
905     double* point = arg.getSampleDataRW(i0+m_NN[0]*i1);
906     point[0] = getLocalCoordinate(i0, 0);
907     point[1] = getLocalCoordinate(i1, 1);
908 caltinay 3764 }
909     }
910     }
911    
912     //protected
913 caltinay 4626 void Rectangle::assembleGradient(escript::Data& out, const escript::Data& in) const
914 caltinay 3764 {
915 caltinay 3701 const dim_t numComp = in.getDataPointSize();
916 caltinay 4375 const double cx0 = .21132486540518711775/m_dx[0];
917     const double cx1 = .78867513459481288225/m_dx[0];
918     const double cx2 = 1./m_dx[0];
919     const double cy0 = .21132486540518711775/m_dx[1];
920     const double cy1 = .78867513459481288225/m_dx[1];
921     const double cy2 = 1./m_dx[1];
922 caltinay 3724
923 caltinay 3702 if (out.getFunctionSpace().getTypeCode() == Elements) {
924 caltinay 3760 out.requireWrite();
925 caltinay 3913 #pragma omp parallel
926     {
927     vector<double> f_00(numComp);
928     vector<double> f_01(numComp);
929     vector<double> f_10(numComp);
930     vector<double> f_11(numComp);
931     #pragma omp for
932 caltinay 4334 for (index_t k1=0; k1 < m_NE[1]; ++k1) {
933     for (index_t k0=0; k0 < m_NE[0]; ++k0) {
934     memcpy(&f_00[0], in.getSampleDataRO(INDEX2(k0,k1, m_NN[0])), numComp*sizeof(double));
935     memcpy(&f_01[0], in.getSampleDataRO(INDEX2(k0,k1+1, m_NN[0])), numComp*sizeof(double));
936     memcpy(&f_10[0], in.getSampleDataRO(INDEX2(k0+1,k1, m_NN[0])), numComp*sizeof(double));
937     memcpy(&f_11[0], in.getSampleDataRO(INDEX2(k0+1,k1+1, m_NN[0])), numComp*sizeof(double));
938     double* o = out.getSampleDataRW(INDEX2(k0,k1,m_NE[0]));
939 caltinay 3913 for (index_t i=0; i < numComp; ++i) {
940 caltinay 4375 o[INDEX3(i,0,0,numComp,2)] = (f_10[i]-f_00[i])*cx1 + (f_11[i]-f_01[i])*cx0;
941     o[INDEX3(i,1,0,numComp,2)] = (f_01[i]-f_00[i])*cy1 + (f_11[i]-f_10[i])*cy0;
942     o[INDEX3(i,0,1,numComp,2)] = (f_10[i]-f_00[i])*cx1 + (f_11[i]-f_01[i])*cx0;
943     o[INDEX3(i,1,1,numComp,2)] = (f_01[i]-f_00[i])*cy0 + (f_11[i]-f_10[i])*cy1;
944     o[INDEX3(i,0,2,numComp,2)] = (f_10[i]-f_00[i])*cx0 + (f_11[i]-f_01[i])*cx1;
945     o[INDEX3(i,1,2,numComp,2)] = (f_01[i]-f_00[i])*cy1 + (f_11[i]-f_10[i])*cy0;
946     o[INDEX3(i,0,3,numComp,2)] = (f_10[i]-f_00[i])*cx0 + (f_11[i]-f_01[i])*cx1;
947     o[INDEX3(i,1,3,numComp,2)] = (f_01[i]-f_00[i])*cy0 + (f_11[i]-f_10[i])*cy1;
948 caltinay 3913 } // end of component loop i
949     } // end of k0 loop
950     } // end of k1 loop
951     } // end of parallel section
952 caltinay 3711 } else if (out.getFunctionSpace().getTypeCode() == ReducedElements) {
953 caltinay 3760 out.requireWrite();
954 caltinay 3913 #pragma omp parallel
955     {
956     vector<double> f_00(numComp);
957     vector<double> f_01(numComp);
958     vector<double> f_10(numComp);
959     vector<double> f_11(numComp);
960     #pragma omp for
961 caltinay 4334 for (index_t k1=0; k1 < m_NE[1]; ++k1) {
962     for (index_t k0=0; k0 < m_NE[0]; ++k0) {
963     memcpy(&f_00[0], in.getSampleDataRO(INDEX2(k0,k1, m_NN[0])), numComp*sizeof(double));
964     memcpy(&f_01[0], in.getSampleDataRO(INDEX2(k0,k1+1, m_NN[0])), numComp*sizeof(double));
965     memcpy(&f_10[0], in.getSampleDataRO(INDEX2(k0+1,k1, m_NN[0])), numComp*sizeof(double));
966     memcpy(&f_11[0], in.getSampleDataRO(INDEX2(k0+1,k1+1, m_NN[0])), numComp*sizeof(double));
967     double* o = out.getSampleDataRW(INDEX2(k0,k1,m_NE[0]));
968 caltinay 3913 for (index_t i=0; i < numComp; ++i) {
969 caltinay 4375 o[INDEX3(i,0,0,numComp,2)] = (f_10[i] + f_11[i] - f_00[i] - f_01[i])*cx2/2;
970     o[INDEX3(i,1,0,numComp,2)] = (f_01[i] + f_11[i] - f_00[i] - f_10[i])*cy2/2;
971 caltinay 3913 } // end of component loop i
972     } // end of k0 loop
973     } // end of k1 loop
974     } // end of parallel section
975 caltinay 3707 } else if (out.getFunctionSpace().getTypeCode() == FaceElements) {
976 caltinay 3760 out.requireWrite();
977 caltinay 3722 #pragma omp parallel
978     {
979 caltinay 3913 vector<double> f_00(numComp);
980     vector<double> f_01(numComp);
981     vector<double> f_10(numComp);
982     vector<double> f_11(numComp);
983 caltinay 3722 if (m_faceOffset[0] > -1) {
984     #pragma omp for nowait
985 caltinay 4334 for (index_t k1=0; k1 < m_NE[1]; ++k1) {
986     memcpy(&f_00[0], in.getSampleDataRO(INDEX2(0,k1, m_NN[0])), numComp*sizeof(double));
987     memcpy(&f_01[0], in.getSampleDataRO(INDEX2(0,k1+1, m_NN[0])), numComp*sizeof(double));
988     memcpy(&f_10[0], in.getSampleDataRO(INDEX2(1,k1, m_NN[0])), numComp*sizeof(double));
989     memcpy(&f_11[0], in.getSampleDataRO(INDEX2(1,k1+1, m_NN[0])), numComp*sizeof(double));
990 caltinay 3722 double* o = out.getSampleDataRW(m_faceOffset[0]+k1);
991     for (index_t i=0; i < numComp; ++i) {
992 caltinay 4375 o[INDEX3(i,0,0,numComp,2)] = (f_10[i]-f_00[i])*cx1 + (f_11[i]-f_01[i])*cx0;
993     o[INDEX3(i,1,0,numComp,2)] = (f_01[i]-f_00[i])*cy2;
994     o[INDEX3(i,0,1,numComp,2)] = (f_10[i]-f_00[i])*cx0 + (f_11[i]-f_01[i])*cx1;
995     o[INDEX3(i,1,1,numComp,2)] = (f_01[i]-f_00[i])*cy2;
996 caltinay 3800 } // end of component loop i
997     } // end of k1 loop
998     } // end of face 0
999 caltinay 3722 if (m_faceOffset[1] > -1) {
1000     #pragma omp for nowait
1001 caltinay 4334 for (index_t k1=0; k1 < m_NE[1]; ++k1) {
1002     memcpy(&f_00[0], in.getSampleDataRO(INDEX2(m_NN[0]-2,k1, m_NN[0])), numComp*sizeof(double));
1003     memcpy(&f_01[0], in.getSampleDataRO(INDEX2(m_NN[0]-2,k1+1, m_NN[0])), numComp*sizeof(double));
1004     memcpy(&f_10[0], in.getSampleDataRO(INDEX2(m_NN[0]-1,k1, m_NN[0])), numComp*sizeof(double));
1005     memcpy(&f_11[0], in.getSampleDataRO(INDEX2(m_NN[0]-1,k1+1, m_NN[0])), numComp*sizeof(double));
1006 caltinay 3722 double* o = out.getSampleDataRW(m_faceOffset[1]+k1);
1007     for (index_t i=0; i < numComp; ++i) {
1008 caltinay 4375 o[INDEX3(i,0,0,numComp,2)] = (f_10[i]-f_00[i])*cx1 + (f_11[i]-f_01[i])*cx0;
1009     o[INDEX3(i,1,0,numComp,2)] = (f_11[i]-f_10[i])*cy2;
1010     o[INDEX3(i,0,1,numComp,2)] = (f_10[i]-f_00[i])*cx0 + (f_11[i]-f_01[i])*cx1;
1011     o[INDEX3(i,1,1,numComp,2)] = (f_11[i]-f_10[i])*cy2;
1012 caltinay 3800 } // end of component loop i
1013     } // end of k1 loop
1014     } // end of face 1
1015 caltinay 3722 if (m_faceOffset[2] > -1) {
1016     #pragma omp for nowait
1017 caltinay 4334 for (index_t k0=0; k0 < m_NE[0]; ++k0) {
1018     memcpy(&f_00[0], in.getSampleDataRO(INDEX2(k0,0, m_NN[0])), numComp*sizeof(double));
1019     memcpy(&f_01[0], in.getSampleDataRO(INDEX2(k0,1, m_NN[0])), numComp*sizeof(double));
1020     memcpy(&f_10[0], in.getSampleDataRO(INDEX2(k0+1,0, m_NN[0])), numComp*sizeof(double));
1021     memcpy(&f_11[0], in.getSampleDataRO(INDEX2(k0+1,1, m_NN[0])), numComp*sizeof(double));
1022 caltinay 3722 double* o = out.getSampleDataRW(m_faceOffset[2]+k0);
1023     for (index_t i=0; i < numComp; ++i) {
1024 caltinay 4375 o[INDEX3(i,0,0,numComp,2)] = (f_10[i]-f_00[i])*cx2;
1025     o[INDEX3(i,1,0,numComp,2)] = (f_01[i]-f_00[i])*cy1 + (f_11[i]-f_10[i])*cy0;
1026     o[INDEX3(i,0,1,numComp,2)] = (f_10[i]-f_00[i])*cx2;
1027     o[INDEX3(i,1,1,numComp,2)] = (f_01[i]-f_00[i])*cy0 + (f_11[i]-f_10[i])*cy1;
1028 caltinay 3800 } // end of component loop i
1029     } // end of k0 loop
1030     } // end of face 2
1031 caltinay 3722 if (m_faceOffset[3] > -1) {
1032     #pragma omp for nowait
1033 caltinay 4334 for (index_t k0=0; k0 < m_NE[0]; ++k0) {
1034     memcpy(&f_00[0], in.getSampleDataRO(INDEX2(k0,m_NN[1]-2, m_NN[0])), numComp*sizeof(double));
1035     memcpy(&f_01[0], in.getSampleDataRO(INDEX2(k0,m_NN[1]-1, m_NN[0])), numComp*sizeof(double));
1036     memcpy(&f_10[0], in.getSampleDataRO(INDEX2(k0+1,m_NN[1]-2, m_NN[0])), numComp*sizeof(double));
1037     memcpy(&f_11[0], in.getSampleDataRO(INDEX2(k0+1,m_NN[1]-1, m_NN[0])), numComp*sizeof(double));
1038 caltinay 3722 double* o = out.getSampleDataRW(m_faceOffset[3]+k0);
1039     for (index_t i=0; i < numComp; ++i) {
1040 caltinay 4375 o[INDEX3(i,0,0,numComp,2)] = (f_11[i]-f_01[i])*cx2;
1041     o[INDEX3(i,1,0,numComp,2)] = (f_01[i]-f_00[i])*cy1 + (f_11[i]-f_10[i])*cy0;
1042     o[INDEX3(i,0,1,numComp,2)] = (f_11[i]-f_01[i])*cx2;
1043     o[INDEX3(i,1,1,numComp,2)] = (f_01[i]-f_00[i])*cy0 + (f_11[i]-f_10[i])*cy1;
1044 caltinay 3800 } // end of component loop i
1045     } // end of k0 loop
1046     } // end of face 3
1047 caltinay 3722 } // end of parallel section
1048 caltinay 3800
1049 caltinay 3711 } else if (out.getFunctionSpace().getTypeCode() == ReducedFaceElements) {
1050 caltinay 3760 out.requireWrite();
1051 caltinay 3722 #pragma omp parallel
1052     {
1053 caltinay 3913 vector<double> f_00(numComp);
1054     vector<double> f_01(numComp);
1055     vector<double> f_10(numComp);
1056     vector<double> f_11(numComp);
1057 caltinay 3722 if (m_faceOffset[0] > -1) {
1058     #pragma omp for nowait
1059 caltinay 4334 for (index_t k1=0; k1 < m_NE[1]; ++k1) {
1060     memcpy(&f_00[0], in.getSampleDataRO(INDEX2(0,k1, m_NN[0])), numComp*sizeof(double));
1061     memcpy(&f_01[0], in.getSampleDataRO(INDEX2(0,k1+1, m_NN[0])), numComp*sizeof(double));
1062     memcpy(&f_10[0], in.getSampleDataRO(INDEX2(1,k1, m_NN[0])), numComp*sizeof(double));
1063     memcpy(&f_11[0], in.getSampleDataRO(INDEX2(1,k1+1, m_NN[0])), numComp*sizeof(double));
1064 caltinay 3722 double* o = out.getSampleDataRW(m_faceOffset[0]+k1);
1065     for (index_t i=0; i < numComp; ++i) {
1066 caltinay 4375 o[INDEX3(i,0,0,numComp,2)] = (f_10[i] + f_11[i] - f_00[i] - f_01[i])*cx2/2;
1067     o[INDEX3(i,1,0,numComp,2)] = (f_01[i]-f_00[i])*cy2;
1068 caltinay 3800 } // end of component loop i
1069     } // end of k1 loop
1070     } // end of face 0
1071 caltinay 3722 if (m_faceOffset[1] > -1) {
1072     #pragma omp for nowait
1073 caltinay 4334 for (index_t k1=0; k1 < m_NE[1]; ++k1) {
1074     memcpy(&f_00[0], in.getSampleDataRO(INDEX2(m_NN[0]-2,k1, m_NN[0])), numComp*sizeof(double));
1075     memcpy(&f_01[0], in.getSampleDataRO(INDEX2(m_NN[0]-2,k1+1, m_NN[0])), numComp*sizeof(double));
1076     memcpy(&f_10[0], in.getSampleDataRO(INDEX2(m_NN[0]-1,k1, m_NN[0])), numComp*sizeof(double));
1077     memcpy(&f_11[0], in.getSampleDataRO(INDEX2(m_NN[0]-1,k1+1, m_NN[0])), numComp*sizeof(double));
1078 caltinay 3722 double* o = out.getSampleDataRW(m_faceOffset[1]+k1);
1079     for (index_t i=0; i < numComp; ++i) {
1080 caltinay 4375 o[INDEX3(i,0,0,numComp,2)] = (f_10[i] + f_11[i] - f_00[i] - f_01[i])*cx2/2;
1081     o[INDEX3(i,1,0,numComp,2)] = (f_11[i]-f_10[i])*cy2;
1082 caltinay 3800 } // end of component loop i
1083     } // end of k1 loop
1084     } // end of face 1
1085 caltinay 3722 if (m_faceOffset[2] > -1) {
1086     #pragma omp for nowait
1087 caltinay 4334 for (index_t k0=0; k0 < m_NE[0]; ++k0) {
1088     memcpy(&f_00[0], in.getSampleDataRO(INDEX2(k0,0, m_NN[0])), numComp*sizeof(double));
1089     memcpy(&f_01[0], in.getSampleDataRO(INDEX2(k0,1, m_NN[0])), numComp*sizeof(double));
1090     memcpy(&f_10[0], in.getSampleDataRO(INDEX2(k0+1,0, m_NN[0])), numComp*sizeof(double));
1091     memcpy(&f_11[0], in.getSampleDataRO(INDEX2(k0+1,1, m_NN[0])), numComp*sizeof(double));
1092 caltinay 3722 double* o = out.getSampleDataRW(m_faceOffset[2]+k0);
1093     for (index_t i=0; i < numComp; ++i) {
1094 caltinay 4375 o[INDEX3(i,0,0,numComp,2)] = (f_10[i]-f_00[i])*cx2;
1095     o[INDEX3(i,1,0,numComp,2)] = (f_01[i] + f_11[i] - f_00[i] - f_10[i])*cy2/2;
1096 caltinay 3800 } // end of component loop i
1097     } // end of k0 loop
1098     } // end of face 2
1099 caltinay 3722 if (m_faceOffset[3] > -1) {
1100     #pragma omp for nowait
1101 caltinay 4334 for (index_t k0=0; k0 < m_NE[0]; ++k0) {
1102     memcpy(&f_00[0], in.getSampleDataRO(INDEX2(k0,m_NN[1]-2, m_NN[0])), numComp*sizeof(double));
1103     memcpy(&f_01[0], in.getSampleDataRO(INDEX2(k0,m_NN[1]-1, m_NN[0])), numComp*sizeof(double));
1104     memcpy(&f_10[0], in.getSampleDataRO(INDEX2(k0+1,m_NN[1]-2, m_NN[0])), numComp*sizeof(double));
1105     memcpy(&f_11[0], in.getSampleDataRO(INDEX2(k0+1,m_NN[1]-1, m_NN[0])), numComp*sizeof(double));
1106 caltinay 3722 double* o = out.getSampleDataRW(m_faceOffset[3]+k0);
1107     for (index_t i=0; i < numComp; ++i) {
1108 caltinay 4375 o[INDEX3(i,0,0,numComp,2)] = (f_11[i]-f_01[i])*cx2;
1109     o[INDEX3(i,1,0,numComp,2)] = (f_01[i] + f_11[i] - f_00[i] - f_10[i])*cy2/2;
1110 caltinay 3800 } // end of component loop i
1111     } // end of k0 loop
1112     } // end of face 3
1113 caltinay 3722 } // end of parallel section
1114 caltinay 3702 }
1115 caltinay 3701 }
1116 caltinay 3697
1117 caltinay 3764 //protected
1118 caltinay 4626 void Rectangle::assembleIntegrate(vector<double>& integrals,
1119     const escript::Data& arg) const
1120 caltinay 3713 {
1121 caltinay 3764 const dim_t numComp = arg.getDataPointSize();
1122 caltinay 4334 const index_t left = (m_offset[0]==0 ? 0 : 1);
1123     const index_t bottom = (m_offset[1]==0 ? 0 : 1);
1124 caltinay 3800 const int fs=arg.getFunctionSpace().getTypeCode();
1125     if (fs == Elements && arg.actsExpanded()) {
1126 caltinay 3713 #pragma omp parallel
1127     {
1128     vector<double> int_local(numComp, 0);
1129 caltinay 4334 const double w = m_dx[0]*m_dx[1]/4.;
1130 caltinay 3722 #pragma omp for nowait
1131 caltinay 4334 for (index_t k1 = bottom; k1 < bottom+m_ownNE[1]; ++k1) {
1132     for (index_t k0 = left; k0 < left+m_ownNE[0]; ++k0) {
1133     const double* f = arg.getSampleDataRO(INDEX2(k0, k1, m_NE[0]));
1134 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
1135 caltinay 3769 const double f0 = f[INDEX2(i,0,numComp)];
1136     const double f1 = f[INDEX2(i,1,numComp)];
1137     const double f2 = f[INDEX2(i,2,numComp)];
1138     const double f3 = f[INDEX2(i,3,numComp)];
1139 caltinay 3764 int_local[i]+=(f0+f1+f2+f3)*w;
1140 caltinay 3800 } // end of component loop i
1141     } // end of k0 loop
1142     } // end of k1 loop
1143 caltinay 3713 #pragma omp critical
1144     for (index_t i=0; i<numComp; i++)
1145     integrals[i]+=int_local[i];
1146 caltinay 3722 } // end of parallel section
1147 caltinay 3800
1148     } else if (fs==ReducedElements || (fs==Elements && !arg.actsExpanded())) {
1149 caltinay 4334 const double w = m_dx[0]*m_dx[1];
1150 caltinay 3713 #pragma omp parallel
1151     {
1152     vector<double> int_local(numComp, 0);
1153 caltinay 3722 #pragma omp for nowait
1154 caltinay 4334 for (index_t k1 = bottom; k1 < bottom+m_ownNE[1]; ++k1) {
1155     for (index_t k0 = left; k0 < left+m_ownNE[0]; ++k0) {
1156     const double* f = arg.getSampleDataRO(INDEX2(k0, k1, m_NE[0]));
1157 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
1158 caltinay 3764 int_local[i]+=f[i]*w;
1159 caltinay 3800 }
1160     }
1161     }
1162 caltinay 3713 #pragma omp critical
1163     for (index_t i=0; i<numComp; i++)
1164     integrals[i]+=int_local[i];
1165 caltinay 3722 } // end of parallel section
1166 caltinay 3800
1167     } else if (fs == FaceElements && arg.actsExpanded()) {
1168 caltinay 3713 #pragma omp parallel
1169     {
1170     vector<double> int_local(numComp, 0);
1171 caltinay 4334 const double w0 = m_dx[0]/2.;
1172     const double w1 = m_dx[1]/2.;
1173 caltinay 3713 if (m_faceOffset[0] > -1) {
1174 caltinay 3722 #pragma omp for nowait
1175 caltinay 4334 for (index_t k1 = bottom; k1 < bottom+m_ownNE[1]; ++k1) {
1176 caltinay 3764 const double* f = arg.getSampleDataRO(m_faceOffset[0]+k1);
1177 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
1178 caltinay 3769 const double f0 = f[INDEX2(i,0,numComp)];
1179     const double f1 = f[INDEX2(i,1,numComp)];
1180 caltinay 3764 int_local[i]+=(f0+f1)*w1;
1181 caltinay 3800 } // end of component loop i
1182     } // end of k1 loop
1183 caltinay 3713 }
1184    
1185     if (m_faceOffset[1] > -1) {
1186 caltinay 3722 #pragma omp for nowait
1187 caltinay 4334 for (index_t k1 = bottom; k1 < bottom+m_ownNE[1]; ++k1) {
1188 caltinay 3764 const double* f = arg.getSampleDataRO(m_faceOffset[1]+k1);
1189 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
1190 caltinay 3769 const double f0 = f[INDEX2(i,0,numComp)];
1191     const double f1 = f[INDEX2(i,1,numComp)];
1192 caltinay 3764 int_local[i]+=(f0+f1)*w1;
1193 caltinay 3800 } // end of component loop i
1194     } // end of k1 loop
1195 caltinay 3713 }
1196    
1197     if (m_faceOffset[2] > -1) {
1198 caltinay 3722 #pragma omp for nowait
1199 caltinay 4334 for (index_t k0 = left; k0 < left+m_ownNE[0]; ++k0) {
1200 caltinay 3764 const double* f = arg.getSampleDataRO(m_faceOffset[2]+k0);
1201 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
1202 caltinay 3769 const double f0 = f[INDEX2(i,0,numComp)];
1203     const double f1 = f[INDEX2(i,1,numComp)];
1204 caltinay 3764 int_local[i]+=(f0+f1)*w0;
1205 caltinay 3800 } // end of component loop i
1206     } // end of k0 loop
1207 caltinay 3713 }
1208    
1209     if (m_faceOffset[3] > -1) {
1210 caltinay 3722 #pragma omp for nowait
1211 caltinay 4334 for (index_t k0 = left; k0 < left+m_ownNE[0]; ++k0) {
1212 caltinay 3764 const double* f = arg.getSampleDataRO(m_faceOffset[3]+k0);
1213 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
1214 caltinay 3769 const double f0 = f[INDEX2(i,0,numComp)];
1215     const double f1 = f[INDEX2(i,1,numComp)];
1216 caltinay 3764 int_local[i]+=(f0+f1)*w0;
1217 caltinay 3800 } // end of component loop i
1218     } // end of k0 loop
1219 caltinay 3713 }
1220     #pragma omp critical
1221     for (index_t i=0; i<numComp; i++)
1222     integrals[i]+=int_local[i];
1223 caltinay 3722 } // end of parallel section
1224 caltinay 3800
1225     } else if (fs==ReducedFaceElements || (fs==FaceElements && !arg.actsExpanded())) {
1226 caltinay 3713 #pragma omp parallel
1227     {
1228     vector<double> int_local(numComp, 0);
1229     if (m_faceOffset[0] > -1) {
1230 caltinay 3722 #pragma omp for nowait
1231 caltinay 4334 for (index_t k1 = bottom; k1 < bottom+m_ownNE[1]; ++k1) {
1232 caltinay 3764 const double* f = arg.getSampleDataRO(m_faceOffset[0]+k1);
1233 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
1234 caltinay 4334 int_local[i]+=f[i]*m_dx[1];
1235 caltinay 3800 }
1236     }
1237 caltinay 3713 }
1238    
1239     if (m_faceOffset[1] > -1) {
1240 caltinay 3722 #pragma omp for nowait
1241 caltinay 4334 for (index_t k1 = bottom; k1 < bottom+m_ownNE[1]; ++k1) {
1242 caltinay 3764 const double* f = arg.getSampleDataRO(m_faceOffset[1]+k1);
1243 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
1244 caltinay 4334 int_local[i]+=f[i]*m_dx[1];
1245 caltinay 3800 }
1246     }
1247 caltinay 3713 }
1248    
1249     if (m_faceOffset[2] > -1) {
1250 caltinay 3722 #pragma omp for nowait
1251 caltinay 4334 for (index_t k0 = left; k0 < left+m_ownNE[0]; ++k0) {
1252 caltinay 3764 const double* f = arg.getSampleDataRO(m_faceOffset[2]+k0);
1253 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
1254 caltinay 4334 int_local[i]+=f[i]*m_dx[0];
1255 caltinay 3800 }
1256     }
1257 caltinay 3713 }
1258    
1259     if (m_faceOffset[3] > -1) {
1260 caltinay 3722 #pragma omp for nowait
1261 caltinay 4334 for (index_t k0 = left; k0 < left+m_ownNE[0]; ++k0) {
1262 caltinay 3764 const double* f = arg.getSampleDataRO(m_faceOffset[3]+k0);
1263 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
1264 caltinay 4334 int_local[i]+=f[i]*m_dx[0];
1265 caltinay 3800 }
1266     }
1267 caltinay 3713 }
1268    
1269     #pragma omp critical
1270     for (index_t i=0; i<numComp; i++)
1271     integrals[i]+=int_local[i];
1272 caltinay 3722 } // end of parallel section
1273 caltinay 3800 } // function space selector
1274 caltinay 3713 }
1275    
1276 caltinay 3691 //protected
1277 caltinay 3756 dim_t Rectangle::insertNeighbourNodes(IndexVector& index, index_t node) const
1278     {
1279 caltinay 4334 const dim_t nDOF0 = (m_gNE[0]+1)/m_NX[0];
1280     const dim_t nDOF1 = (m_gNE[1]+1)/m_NX[1];
1281 caltinay 3756 const int x=node%nDOF0;
1282     const int y=node/nDOF0;
1283     dim_t num=0;
1284     // loop through potential neighbours and add to index if positions are
1285     // within bounds
1286     for (int i1=-1; i1<2; i1++) {
1287     for (int i0=-1; i0<2; i0++) {
1288     // skip node itself
1289     if (i0==0 && i1==0)
1290     continue;
1291     // location of neighbour node
1292     const int nx=x+i0;
1293     const int ny=y+i1;
1294     if (nx>=0 && ny>=0 && nx<nDOF0 && ny<nDOF1) {
1295     index.push_back(ny*nDOF0+nx);
1296     num++;
1297     }
1298     }
1299     }
1300    
1301     return num;
1302     }
1303    
1304     //protected
1305 caltinay 4626 void Rectangle::nodesToDOF(escript::Data& out, const escript::Data& in) const
1306 caltinay 3756 {
1307     const dim_t numComp = in.getDataPointSize();
1308     out.requireWrite();
1309    
1310 caltinay 4334 const index_t left = (m_offset[0]==0 ? 0 : 1);
1311     const index_t bottom = (m_offset[1]==0 ? 0 : 1);
1312     const dim_t nDOF0 = (m_gNE[0]+1)/m_NX[0];
1313     const dim_t nDOF1 = (m_gNE[1]+1)/m_NX[1];
1314 caltinay 3756 #pragma omp parallel for
1315     for (index_t i=0; i<nDOF1; i++) {
1316     for (index_t j=0; j<nDOF0; j++) {
1317 caltinay 4334 const index_t n=j+left+(i+bottom)*m_NN[0];
1318 caltinay 3756 const double* src=in.getSampleDataRO(n);
1319     copy(src, src+numComp, out.getSampleDataRW(j+i*nDOF0));
1320     }
1321     }
1322     }
1323    
1324     //protected
1325 caltinay 4626 void Rectangle::dofToNodes(escript::Data& out, const escript::Data& in) const
1326 caltinay 3756 {
1327     const dim_t numComp = in.getDataPointSize();
1328     Paso_Coupler* coupler = Paso_Coupler_alloc(m_connector, numComp);
1329 caltinay 4633 // expand data object if necessary to be able to grab the whole data
1330     const_cast<escript::Data*>(&in)->expand();
1331 caltinay 4626 Paso_Coupler_startCollect(coupler, in.getSampleDataRO(0));
1332 caltinay 3756
1333     const dim_t numDOF = getNumDOF();
1334     out.requireWrite();
1335     const double* buffer = Paso_Coupler_finishCollect(coupler);
1336    
1337     #pragma omp parallel for
1338     for (index_t i=0; i<getNumNodes(); i++) {
1339     const double* src=(m_dofMap[i]<numDOF ?
1340     in.getSampleDataRO(m_dofMap[i])
1341     : &buffer[(m_dofMap[i]-numDOF)*numComp]);
1342     copy(src, src+numComp, out.getSampleDataRW(i));
1343     }
1344 caltinay 4002 Paso_Coupler_free(coupler);
1345 caltinay 3756 }
1346    
1347 caltinay 3691 //private
1348     void Rectangle::populateSampleIds()
1349     {
1350 caltinay 4334 // degrees of freedom are numbered from left to right, bottom to top in
1351     // each rank, continuing on the next rank (ranks also go left-right,
1352     // bottom-top).
1353     // This means rank 0 has id 0...n0-1, rank 1 has id n0...n1-1 etc. which
1354     // helps when writing out data rank after rank.
1355 caltinay 3697
1356     // build node distribution vector first.
1357 caltinay 4334 // rank i owns m_nodeDistribution[i+1]-nodeDistribution[i] nodes which is
1358     // constant for all ranks in this implementation
1359 caltinay 3697 m_nodeDistribution.assign(m_mpiInfo->size+1, 0);
1360 caltinay 3752 const dim_t numDOF=getNumDOF();
1361     for (dim_t k=1; k<m_mpiInfo->size; k++) {
1362     m_nodeDistribution[k]=k*numDOF;
1363 caltinay 3697 }
1364     m_nodeDistribution[m_mpiInfo->size]=getNumDataPointsGlobal();
1365 caltinay 3691 m_nodeId.resize(getNumNodes());
1366 caltinay 3753 m_dofId.resize(numDOF);
1367     m_elementId.resize(getNumElements());
1368 caltinay 4334
1369     // populate face element counts
1370     //left
1371     if (m_offset[0]==0)
1372     m_faceCount[0]=m_NE[1];
1373     else
1374     m_faceCount[0]=0;
1375     //right
1376     if (m_mpiInfo->rank%m_NX[0]==m_NX[0]-1)
1377     m_faceCount[1]=m_NE[1];
1378     else
1379     m_faceCount[1]=0;
1380     //bottom
1381     if (m_offset[1]==0)
1382     m_faceCount[2]=m_NE[0];
1383     else
1384     m_faceCount[2]=0;
1385     //top
1386     if (m_mpiInfo->rank/m_NX[0]==m_NX[1]-1)
1387     m_faceCount[3]=m_NE[0];
1388     else
1389     m_faceCount[3]=0;
1390    
1391 caltinay 3753 m_faceId.resize(getNumFaceElements());
1392 caltinay 3697
1393 caltinay 4334 const index_t left = (m_offset[0]==0 ? 0 : 1);
1394     const index_t bottom = (m_offset[1]==0 ? 0 : 1);
1395     const dim_t nDOF0 = (m_gNE[0]+1)/m_NX[0];
1396     const dim_t nDOF1 = (m_gNE[1]+1)/m_NX[1];
1397    
1398     #define globalNodeId(x,y) \
1399     ((m_offset[0]+x)/nDOF0)*nDOF0*nDOF1+(m_offset[0]+x)%nDOF0 \
1400     + ((m_offset[1]+y)/nDOF1)*nDOF0*nDOF1*m_NX[0]+((m_offset[1]+y)%nDOF1)*nDOF0
1401    
1402     // set corner id's outside the parallel region
1403     m_nodeId[0] = globalNodeId(0, 0);
1404     m_nodeId[m_NN[0]-1] = globalNodeId(m_NN[0]-1, 0);
1405     m_nodeId[m_NN[0]*(m_NN[1]-1)] = globalNodeId(0, m_NN[1]-1);
1406     m_nodeId[m_NN[0]*m_NN[1]-1] = globalNodeId(m_NN[0]-1,m_NN[1]-1);
1407     #undef globalNodeId
1408    
1409 caltinay 3753 #pragma omp parallel
1410     {
1411 caltinay 4334 // populate degrees of freedom and own nodes (identical id)
1412 caltinay 3753 #pragma omp for nowait
1413 caltinay 4334 for (dim_t i=0; i<nDOF1; i++) {
1414     for (dim_t j=0; j<nDOF0; j++) {
1415     const index_t nodeIdx=j+left+(i+bottom)*m_NN[0];
1416     const index_t dofIdx=j+i*nDOF0;
1417     m_dofId[dofIdx] = m_nodeId[nodeIdx]
1418     = m_nodeDistribution[m_mpiInfo->rank]+dofIdx;
1419 caltinay 3753 }
1420 caltinay 3697 }
1421    
1422 caltinay 4334 // populate the rest of the nodes (shared with other ranks)
1423     if (m_faceCount[0]==0) { // left column
1424 caltinay 3753 #pragma omp for nowait
1425 caltinay 4334 for (dim_t i=0; i<nDOF1; i++) {
1426     const index_t nodeIdx=(i+bottom)*m_NN[0];
1427     const index_t dofId=(i+1)*nDOF0-1;
1428     m_nodeId[nodeIdx]
1429     = m_nodeDistribution[m_mpiInfo->rank-1]+dofId;
1430     }
1431     }
1432     if (m_faceCount[1]==0) { // right column
1433     #pragma omp for nowait
1434     for (dim_t i=0; i<nDOF1; i++) {
1435     const index_t nodeIdx=(i+bottom+1)*m_NN[0]-1;
1436     const index_t dofId=i*nDOF0;
1437     m_nodeId[nodeIdx]
1438     = m_nodeDistribution[m_mpiInfo->rank+1]+dofId;
1439     }
1440     }
1441     if (m_faceCount[2]==0) { // bottom row
1442     #pragma omp for nowait
1443     for (dim_t i=0; i<nDOF0; i++) {
1444     const index_t nodeIdx=i+left;
1445     const index_t dofId=nDOF0*(nDOF1-1)+i;
1446     m_nodeId[nodeIdx]
1447     = m_nodeDistribution[m_mpiInfo->rank-m_NX[0]]+dofId;
1448     }
1449     }
1450     if (m_faceCount[3]==0) { // top row
1451     #pragma omp for nowait
1452     for (dim_t i=0; i<nDOF0; i++) {
1453     const index_t nodeIdx=m_NN[0]*(m_NN[1]-1)+i+left;
1454     const index_t dofId=i;
1455     m_nodeId[nodeIdx]
1456     = m_nodeDistribution[m_mpiInfo->rank+m_NX[0]]+dofId;
1457     }
1458     }
1459 caltinay 3752
1460 caltinay 4334 // populate element id's
1461 caltinay 3753 #pragma omp for nowait
1462 caltinay 4334 for (dim_t i1=0; i1<m_NE[1]; i1++) {
1463     for (dim_t i0=0; i0<m_NE[0]; i0++) {
1464     m_elementId[i0+i1*m_NE[0]]=(m_offset[1]+i1)*m_gNE[0]+m_offset[0]+i0;
1465 caltinay 3755 }
1466     }
1467 caltinay 3753
1468     // face elements
1469     #pragma omp for
1470     for (dim_t k=0; k<getNumFaceElements(); k++)
1471     m_faceId[k]=k;
1472     } // end parallel section
1473    
1474 caltinay 3735 m_nodeTags.assign(getNumNodes(), 0);
1475     updateTagsInUse(Nodes);
1476 caltinay 3697
1477 caltinay 3735 m_elementTags.assign(getNumElements(), 0);
1478     updateTagsInUse(Elements);
1479 caltinay 3697
1480 caltinay 3722 // generate face offset vector and set face tags
1481     const index_t LEFT=1, RIGHT=2, BOTTOM=10, TOP=20;
1482     const index_t faceTag[] = { LEFT, RIGHT, BOTTOM, TOP };
1483 caltinay 4334 m_faceOffset.assign(4, -1);
1484 caltinay 3722 m_faceTags.clear();
1485 caltinay 3704 index_t offset=0;
1486 caltinay 4334 for (size_t i=0; i<4; i++) {
1487     if (m_faceCount[i]>0) {
1488 caltinay 3704 m_faceOffset[i]=offset;
1489 caltinay 4334 offset+=m_faceCount[i];
1490     m_faceTags.insert(m_faceTags.end(), m_faceCount[i], faceTag[i]);
1491 caltinay 3704 }
1492     }
1493 caltinay 3722 setTagMap("left", LEFT);
1494     setTagMap("right", RIGHT);
1495     setTagMap("bottom", BOTTOM);
1496     setTagMap("top", TOP);
1497     updateTagsInUse(FaceElements);
1498 caltinay 3691 }
1499    
1500 caltinay 3699 //private
1501 caltinay 3756 void Rectangle::createPattern()
1502 caltinay 3699 {
1503 caltinay 4334 const dim_t nDOF0 = (m_gNE[0]+1)/m_NX[0];
1504     const dim_t nDOF1 = (m_gNE[1]+1)/m_NX[1];
1505     const index_t left = (m_offset[0]==0 ? 0 : 1);
1506     const index_t bottom = (m_offset[1]==0 ? 0 : 1);
1507 caltinay 3756
1508     // populate node->DOF mapping with own degrees of freedom.
1509     // The rest is assigned in the loop further down
1510     m_dofMap.assign(getNumNodes(), 0);
1511     #pragma omp parallel for
1512 caltinay 3766 for (index_t i=bottom; i<bottom+nDOF1; i++) {
1513     for (index_t j=left; j<left+nDOF0; j++) {
1514 caltinay 4334 m_dofMap[i*m_NN[0]+j]=(i-bottom)*nDOF0+j-left;
1515 caltinay 3756 }
1516     }
1517    
1518     // build list of shared components and neighbours by looping through
1519     // all potential neighbouring ranks and checking if positions are
1520 caltinay 3754 // within bounds
1521 caltinay 3756 const dim_t numDOF=nDOF0*nDOF1;
1522     vector<IndexVector> colIndices(numDOF); // for the couple blocks
1523     RankVector neighbour;
1524     IndexVector offsetInShared(1,0);
1525     IndexVector sendShared, recvShared;
1526 sshaw 4722 int numShared=0, expectedShared=0;
1527 caltinay 4334 const int x=m_mpiInfo->rank%m_NX[0];
1528     const int y=m_mpiInfo->rank/m_NX[0];
1529 sshaw 4722 if (x > 0)
1530     expectedShared += nDOF1;
1531     if (x < m_NX[0] - 1)
1532     expectedShared += nDOF1;
1533     if (y > 0)
1534     expectedShared += nDOF0;
1535     if (y < m_NX[1] - 1)
1536     expectedShared += nDOF0;
1537     if (x > 0 && y > 0) expectedShared++;
1538     if (x > 0 && y < m_NX[1] - 1) expectedShared++;
1539     if (x < m_NX[0] - 1 && y > 0) expectedShared++;
1540     if (x < m_NX[0] - 1 && y < m_NX[1] - 1) expectedShared++;
1541    
1542     vector<IndexVector> rowIndices(expectedShared);
1543    
1544 caltinay 3754 for (int i1=-1; i1<2; i1++) {
1545     for (int i0=-1; i0<2; i0++) {
1546 caltinay 3756 // skip this rank
1547 caltinay 3754 if (i0==0 && i1==0)
1548     continue;
1549 caltinay 3756 // location of neighbour rank
1550 caltinay 3754 const int nx=x+i0;
1551     const int ny=y+i1;
1552 caltinay 4334 if (nx>=0 && ny>=0 && nx<m_NX[0] && ny<m_NX[1]) {
1553     neighbour.push_back(ny*m_NX[0]+nx);
1554 caltinay 3756 if (i0==0) {
1555     // sharing top or bottom edge
1556     const int firstDOF=(i1==-1 ? 0 : numDOF-nDOF0);
1557 caltinay 4334 const int firstNode=(i1==-1 ? left : m_NN[0]*(m_NN[1]-1)+left);
1558 caltinay 3756 offsetInShared.push_back(offsetInShared.back()+nDOF0);
1559     for (dim_t i=0; i<nDOF0; i++, numShared++) {
1560     sendShared.push_back(firstDOF+i);
1561     recvShared.push_back(numDOF+numShared);
1562     if (i>0)
1563 sshaw 4722 doublyLink(colIndices, rowIndices, firstDOF+i-1, numShared);
1564     doublyLink(colIndices, rowIndices, firstDOF+i, numShared);
1565 caltinay 3756 if (i<nDOF0-1)
1566 sshaw 4722 doublyLink(colIndices, rowIndices, firstDOF+i+1, numShared);
1567 caltinay 3756 m_dofMap[firstNode+i]=numDOF+numShared;
1568     }
1569     } else if (i1==0) {
1570     // sharing left or right edge
1571     const int firstDOF=(i0==-1 ? 0 : nDOF0-1);
1572 caltinay 4334 const int firstNode=(i0==-1 ? bottom*m_NN[0] : (bottom+1)*m_NN[0]-1);
1573 caltinay 3756 offsetInShared.push_back(offsetInShared.back()+nDOF1);
1574     for (dim_t i=0; i<nDOF1; i++, numShared++) {
1575     sendShared.push_back(firstDOF+i*nDOF0);
1576     recvShared.push_back(numDOF+numShared);
1577     if (i>0)
1578 sshaw 4722 doublyLink(colIndices, rowIndices, firstDOF+(i-1)*nDOF0, numShared);
1579     doublyLink(colIndices, rowIndices, firstDOF+i*nDOF0, numShared);
1580 caltinay 3756 if (i<nDOF1-1)
1581 sshaw 4722 doublyLink(colIndices, rowIndices, firstDOF+(i+1)*nDOF0, numShared);
1582 caltinay 4334 m_dofMap[firstNode+i*m_NN[0]]=numDOF+numShared;
1583 caltinay 3756 }
1584     } else {
1585     // sharing a node
1586     const int dof=(i0+1)/2*(nDOF0-1)+(i1+1)/2*(numDOF-nDOF0);
1587 caltinay 4334 const int node=(i0+1)/2*(m_NN[0]-1)+(i1+1)/2*m_NN[0]*(m_NN[1]-1);
1588 caltinay 3756 offsetInShared.push_back(offsetInShared.back()+1);
1589     sendShared.push_back(dof);
1590     recvShared.push_back(numDOF+numShared);
1591 sshaw 4722 doublyLink(colIndices, rowIndices, dof, numShared);
1592 caltinay 3756 m_dofMap[node]=numDOF+numShared;
1593     ++numShared;
1594     }
1595 caltinay 3754 }
1596 caltinay 3699 }
1597     }
1598 sshaw 4722
1599     #pragma omp parallel for
1600     for (int i = 0; i < numShared; i++) {
1601     std::sort(rowIndices[i].begin(), rowIndices[i].end());
1602     }
1603    
1604 caltinay 3756 // create connector
1605     Paso_SharedComponents *snd_shcomp = Paso_SharedComponents_alloc(
1606     numDOF, neighbour.size(), &neighbour[0], &sendShared[0],
1607     &offsetInShared[0], 1, 0, m_mpiInfo);
1608     Paso_SharedComponents *rcv_shcomp = Paso_SharedComponents_alloc(
1609     numDOF, neighbour.size(), &neighbour[0], &recvShared[0],
1610     &offsetInShared[0], 1, 0, m_mpiInfo);
1611     m_connector = Paso_Connector_alloc(snd_shcomp, rcv_shcomp);
1612     Paso_SharedComponents_free(snd_shcomp);
1613     Paso_SharedComponents_free(rcv_shcomp);
1614 caltinay 3754
1615 caltinay 3756 // create main and couple blocks
1616     Paso_Pattern *mainPattern = createMainPattern();
1617     Paso_Pattern *colPattern, *rowPattern;
1618 sshaw 4722 createCouplePatterns(colIndices, rowIndices, numShared, &colPattern, &rowPattern);
1619 caltinay 3754
1620 caltinay 3756 // allocate paso distribution
1621     Paso_Distribution* distribution = Paso_Distribution_alloc(m_mpiInfo,
1622     const_cast<index_t*>(&m_nodeDistribution[0]), 1, 0);
1623 caltinay 3755
1624 caltinay 3756 // finally create the system matrix
1625     m_pattern = Paso_SystemMatrixPattern_alloc(MATRIX_FORMAT_DEFAULT,
1626     distribution, distribution, mainPattern, colPattern, rowPattern,
1627     m_connector, m_connector);
1628 caltinay 3755
1629 caltinay 3756 Paso_Distribution_free(distribution);
1630 caltinay 3755
1631 caltinay 3756 // useful debug output
1632     /*
1633     cout << "--- rcv_shcomp ---" << endl;
1634     cout << "numDOF=" << numDOF << ", numNeighbors=" << neighbour.size() << endl;
1635     for (size_t i=0; i<neighbour.size(); i++) {
1636     cout << "neighbor[" << i << "]=" << neighbour[i]
1637     << " offsetInShared[" << i+1 << "]=" << offsetInShared[i+1] << endl;
1638 caltinay 3699 }
1639 caltinay 3756 for (size_t i=0; i<recvShared.size(); i++) {
1640     cout << "shared[" << i << "]=" << recvShared[i] << endl;
1641     }
1642     cout << "--- snd_shcomp ---" << endl;
1643     for (size_t i=0; i<sendShared.size(); i++) {
1644     cout << "shared[" << i << "]=" << sendShared[i] << endl;
1645     }
1646     cout << "--- dofMap ---" << endl;
1647     for (size_t i=0; i<m_dofMap.size(); i++) {
1648     cout << "m_dofMap[" << i << "]=" << m_dofMap[i] << endl;
1649     }
1650     cout << "--- colIndices ---" << endl;
1651     for (size_t i=0; i<colIndices.size(); i++) {
1652     cout << "colIndices[" << i << "].size()=" << colIndices[i].size() << endl;
1653     }
1654     */
1655 caltinay 3754
1656 caltinay 3756 /*
1657     cout << "--- main_pattern ---" << endl;
1658     cout << "M=" << mainPattern->numOutput << ", N=" << mainPattern->numInput << endl;
1659     for (size_t i=0; i<mainPattern->numOutput+1; i++) {
1660     cout << "ptr[" << i << "]=" << mainPattern->ptr[i] << endl;
1661     }
1662     for (size_t i=0; i<mainPattern->ptr[mainPattern->numOutput]; i++) {
1663     cout << "index[" << i << "]=" << mainPattern->index[i] << endl;
1664     }
1665     */
1666 caltinay 3754
1667 caltinay 3756 /*
1668     cout << "--- colCouple_pattern ---" << endl;
1669     cout << "M=" << colPattern->numOutput << ", N=" << colPattern->numInput << endl;
1670     for (size_t i=0; i<colPattern->numOutput+1; i++) {
1671     cout << "ptr[" << i << "]=" << colPattern->ptr[i] << endl;
1672     }
1673     for (size_t i=0; i<colPattern->ptr[colPattern->numOutput]; i++) {
1674     cout << "index[" << i << "]=" << colPattern->index[i] << endl;
1675     }
1676     */
1677 caltinay 3754
1678 caltinay 3756 /*
1679     cout << "--- rowCouple_pattern ---" << endl;
1680     cout << "M=" << rowPattern->numOutput << ", N=" << rowPattern->numInput << endl;
1681     for (size_t i=0; i<rowPattern->numOutput+1; i++) {
1682     cout << "ptr[" << i << "]=" << rowPattern->ptr[i] << endl;
1683 caltinay 3699 }
1684 caltinay 3756 for (size_t i=0; i<rowPattern->ptr[rowPattern->numOutput]; i++) {
1685     cout << "index[" << i << "]=" << rowPattern->index[i] << endl;
1686     }
1687     */
1688    
1689     Paso_Pattern_free(mainPattern);
1690     Paso_Pattern_free(colPattern);
1691     Paso_Pattern_free(rowPattern);
1692 caltinay 3699 }
1693    
1694 caltinay 3776 //private
1695     void Rectangle::addToMatrixAndRHS(Paso_SystemMatrix* S, escript::Data& F,
1696     const vector<double>& EM_S, const vector<double>& EM_F, bool addS,
1697     bool addF, index_t firstNode, dim_t nEq, dim_t nComp) const
1698     {
1699     IndexVector rowIndex;
1700     rowIndex.push_back(m_dofMap[firstNode]);
1701     rowIndex.push_back(m_dofMap[firstNode+1]);
1702 caltinay 4334 rowIndex.push_back(m_dofMap[firstNode+m_NN[0]]);
1703     rowIndex.push_back(m_dofMap[firstNode+m_NN[0]+1]);
1704 caltinay 3776 if (addF) {
1705     double *F_p=F.getSampleDataRW(0);
1706     for (index_t i=0; i<rowIndex.size(); i++) {
1707     if (rowIndex[i]<getNumDOF()) {
1708     for (index_t eq=0; eq<nEq; eq++) {
1709     F_p[INDEX2(eq, rowIndex[i], nEq)]+=EM_F[INDEX2(eq,i,nEq)];
1710     }
1711     }
1712     }
1713     }
1714     if (addS) {
1715     addToSystemMatrix(S, rowIndex, nEq, rowIndex, nComp, EM_S);
1716     }
1717     }
1718    
1719 caltinay 3702 //protected
1720 caltinay 3711 void Rectangle::interpolateNodesOnElements(escript::Data& out,
1721 caltinay 4626 const escript::Data& in,
1722     bool reduced) const
1723 caltinay 3702 {
1724     const dim_t numComp = in.getDataPointSize();
1725 caltinay 3711 if (reduced) {
1726 caltinay 3760 out.requireWrite();
1727 caltinay 3913 const double c0 = 0.25;
1728     #pragma omp parallel
1729     {
1730     vector<double> f_00(numComp);
1731     vector<double> f_01(numComp);
1732     vector<double> f_10(numComp);
1733     vector<double> f_11(numComp);
1734     #pragma omp for
1735 caltinay 4334 for (index_t k1=0; k1 < m_NE[1]; ++k1) {
1736     for (index_t k0=0; k0 < m_NE[0]; ++k0) {
1737     memcpy(&f_00[0], in.getSampleDataRO(INDEX2(k0,k1, m_NN[0])), numComp*sizeof(double));
1738     memcpy(&f_01[0], in.getSampleDataRO(INDEX2(k0,k1+1, m_NN[0])), numComp*sizeof(double));
1739     memcpy(&f_10[0], in.getSampleDataRO(INDEX2(k0+1,k1, m_NN[0])), numComp*sizeof(double));
1740     memcpy(&f_11[0], in.getSampleDataRO(INDEX2(k0+1,k1+1, m_NN[0])), numComp*sizeof(double));
1741     double* o = out.getSampleDataRW(INDEX2(k0,k1,m_NE[0]));
1742 caltinay 3913 for (index_t i=0; i < numComp; ++i) {
1743     o[INDEX2(i,numComp,0)] = c0*(f_00[i] + f_01[i] + f_10[i] + f_11[i]);
1744     } /* end of component loop i */
1745     } /* end of k0 loop */
1746     } /* end of k1 loop */
1747     } /* end of parallel section */
1748 caltinay 3711 } else {
1749 caltinay 3760 out.requireWrite();
1750 caltinay 3913 const double c0 = 0.16666666666666666667;
1751     const double c1 = 0.044658198738520451079;
1752     const double c2 = 0.62200846792814621559;
1753     #pragma omp parallel
1754     {
1755     vector<double> f_00(numComp);
1756     vector<double> f_01(numComp);
1757     vector<double> f_10(numComp);
1758     vector<double> f_11(numComp);
1759     #pragma omp for
1760 caltinay 4334 for (index_t k1=0; k1 < m_NE[1]; ++k1) {
1761     for (index_t k0=0; k0 < m_NE[0]; ++k0) {
1762     memcpy(&f_00[0], in.getSampleDataRO(INDEX2(k0,k1, m_NN[0])), numComp*sizeof(double));
1763     memcpy(&f_01[0], in.getSampleDataRO(INDEX2(k0,k1+1, m_NN[0])), numComp*sizeof(double));
1764     memcpy(&f_10[0], in.getSampleDataRO(INDEX2(k0+1,k1, m_NN[0])), numComp*sizeof(double));
1765     memcpy(&f_11[0], in.getSampleDataRO(INDEX2(k0+1,k1+1, m_NN[0])), numComp*sizeof(double));
1766     double* o = out.getSampleDataRW(INDEX2(k0,k1,m_NE[0]));
1767 caltinay 3913 for (index_t i=0; i < numComp; ++i) {
1768     o[INDEX2(i,numComp,0)] = c0*(f_01[i] + f_10[i]) + c1*f_11[i] + c2*f_00[i];
1769     o[INDEX2(i,numComp,1)] = c0*(f_00[i] + f_11[i]) + c1*f_01[i] + c2*f_10[i];
1770     o[INDEX2(i,numComp,2)] = c0*(f_00[i] + f_11[i]) + c1*f_10[i] + c2*f_01[i];
1771     o[INDEX2(i,numComp,3)] = c0*(f_01[i] + f_10[i]) + c1*f_00[i] + c2*f_11[i];
1772     } /* end of component loop i */
1773     } /* end of k0 loop */
1774     } /* end of k1 loop */
1775     } /* end of parallel section */
1776 caltinay 3711 }
1777 caltinay 3702 }
1778    
1779     //protected
1780 caltinay 4626 void Rectangle::interpolateNodesOnFaces(escript::Data& out,
1781     const escript::Data& in,
1782 caltinay 3711 bool reduced) const
1783 caltinay 3702 {
1784 caltinay 3704 const dim_t numComp = in.getDataPointSize();
1785 caltinay 3711 if (reduced) {
1786 caltinay 3760 out.requireWrite();
1787 caltinay 3724 #pragma omp parallel
1788     {
1789 caltinay 3913 vector<double> f_00(numComp);
1790     vector<double> f_01(numComp);
1791     vector<double> f_10(numComp);
1792     vector<double> f_11(numComp);
1793 caltinay 3724 if (m_faceOffset[0] > -1) {
1794     #pragma omp for nowait
1795 caltinay 4334 for (index_t k1=0; k1 < m_NE[1]; ++k1) {
1796     memcpy(&f_00[0], in.getSampleDataRO(INDEX2(0,k1, m_NN[0])), numComp*sizeof(double));
1797     memcpy(&f_01[0], in.getSampleDataRO(INDEX2(0,k1+1, m_NN[0])), numComp*sizeof(double));
1798 caltinay 3724 double* o = out.getSampleDataRW(m_faceOffset[0]+k1);
1799     for (index_t i=0; i < numComp; ++i) {
1800 caltinay 4375 o[INDEX2(i,numComp,0)] = (f_00[i] + f_01[i])/2;
1801 caltinay 3724 } /* end of component loop i */
1802     } /* end of k1 loop */
1803     } /* end of face 0 */
1804     if (m_faceOffset[1] > -1) {
1805     #pragma omp for nowait
1806 caltinay 4334 for (index_t k1=0; k1 < m_NE[1]; ++k1) {
1807     memcpy(&f_10[0], in.getSampleDataRO(INDEX2(m_NN[0]-1,k1, m_NN[0])), numComp*sizeof(double));
1808     memcpy(&f_11[0], in.getSampleDataRO(INDEX2(m_NN[0]-1,k1+1, m_NN[0])), numComp*sizeof(double));
1809 caltinay 3724 double* o = out.getSampleDataRW(m_faceOffset[1]+k1);
1810     for (index_t i=0; i < numComp; ++i) {
1811 caltinay 4375 o[INDEX2(i,numComp,0)] = (f_10[i] + f_11[i])/2;
1812 caltinay 3724 } /* end of component loop i */
1813     } /* end of k1 loop */
1814     } /* end of face 1 */
1815     if (m_faceOffset[2] > -1) {
1816     #pragma omp for nowait
1817 caltinay 4334 for (index_t k0=0; k0 < m_NE[0]; ++k0) {
1818     memcpy(&f_00[0], in.getSampleDataRO(INDEX2(k0,0, m_NN[0])), numComp*sizeof(double));
1819     memcpy(&f_10[0], in.getSampleDataRO(INDEX2(k0+1,0, m_NN[0])), numComp*sizeof(double));
1820 caltinay 3724 double* o = out.getSampleDataRW(m_faceOffset[2]+k0);
1821     for (index_t i=0; i < numComp; ++i) {
1822 caltinay 4375 o[INDEX2(i,numComp,0)] = (f_00[i] + f_10[i])/2;
1823 caltinay 3724 } /* end of component loop i */
1824     } /* end of k0 loop */
1825     } /* end of face 2 */
1826     if (m_faceOffset[3] > -1) {
1827     #pragma omp for nowait
1828 caltinay 4334 for (index_t k0=0; k0 < m_NE[0]; ++k0) {
1829     memcpy(&f_01[0], in.getSampleDataRO(INDEX2(k0,m_NN[1]-1, m_NN[0])), numComp*sizeof(double));
1830     memcpy(&f_11[0], in.getSampleDataRO(INDEX2(k0+1,m_NN[1]-1, m_NN[0])), numComp*sizeof(double));
1831 caltinay 3724 double* o = out.getSampleDataRW(m_faceOffset[3]+k0);
1832     for (index_t i=0; i < numComp; ++i) {
1833 caltinay 4375 o[INDEX2(i,numComp,0)] = (f_01[i] + f_11[i])/2;
1834 caltinay 3724 } /* end of component loop i */
1835     } /* end of k0 loop */
1836     } /* end of face 3 */
1837 caltinay 3913 } /* end of parallel section */
1838 caltinay 3711 } else {
1839 caltinay 3760 out.requireWrite();
1840 caltinay 3724 const double c0 = 0.21132486540518711775;
1841     const double c1 = 0.78867513459481288225;
1842     #pragma omp parallel
1843     {
1844 caltinay 3913 vector<double> f_00(numComp);
1845     vector<double> f_01(numComp);
1846     vector<double> f_10(numComp);
1847     vector<double> f_11(numComp);