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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4378 - (hide annotations)
Wed Apr 24 06:21:42 2013 UTC (6 years, 5 months ago) by caltinay
File size: 197080 byte(s)
ripley work: Almost done - one more round in Brick left....

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