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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4495 - (hide annotations)
Fri Jul 5 02:19:47 2013 UTC (6 years, 3 months ago) by caltinay
File size: 198456 byte(s)
Added support for more input data types in ER Mapper files.
Tests will follow soon.

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