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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3806 - (hide annotations)
Mon Feb 6 02:32:48 2012 UTC (7 years, 9 months ago) by caltinay
File size: 227116 byte(s)
ripley::getSize() also returns circumcircle now.

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