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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4277 - (hide annotations)
Wed Mar 6 01:30:41 2013 UTC (6 years, 7 months ago) by caltinay
File size: 239006 byte(s)
Implemented claimed support for mixing data with different resolutions.

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