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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4482 - (hide annotations)
Tue Jun 25 05:20:01 2013 UTC (6 years, 3 months ago) by caltinay
File size: 197105 byte(s)
More leak fixes and suppression file additions.

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