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

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

Parent Directory Parent Directory | Revision Log Revision Log


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