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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3773 - (hide annotations)
Wed Jan 18 04:27:53 2012 UTC (7 years, 8 months ago) by caltinay
Original Path: branches/ripleygmg_from_3668/ripley/src/Rectangle.cpp
File size: 194963 byte(s)
Adjusted interface for boundary conditions.

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