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

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

Parent Directory Parent Directory | Revision Log Revision Log


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