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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4705 - (hide annotations)
Fri Feb 21 02:36:15 2014 UTC (5 years, 8 months ago) by jfenwick
File size: 87954 byte(s)
Set the randomfill back to generating randoms and updated doco to tell people how to use it.

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