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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4174 - (hide annotations)
Wed Jan 30 03:21:27 2013 UTC (6 years, 9 months ago) by caltinay
File size: 237822 byte(s)
ripley's readBinaryGrid and readNcGrid now filter out NaN leaving the relevant
destination data points untouched.

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