/[escript]/branches/ripleygmg_from_3668/ripley/src/Rectangle.cpp
ViewVC logotype

Annotation of /branches/ripleygmg_from_3668/ripley/src/Rectangle.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3776 - (hide annotations)
Thu Jan 19 03:48:35 2012 UTC (7 years, 10 months ago) by caltinay
File size: 230379 byte(s)
Finished 2D - all 2D PDE (non-lumped) tests pass 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 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 caltinay 3776 /* FIXME: reduced
492 caltinay 3764 if (reducedRowOrder || reducedColOrder)
493     throw RipleyException("getPattern() not implemented for reduced order");
494 caltinay 3776 */
495 caltinay 3764 return m_pattern;
496     }
497    
498     void Rectangle::Print_Mesh_Info(const bool full) const
499     {
500     RipleyDomain::Print_Mesh_Info(full);
501     if (full) {
502     cout << " Id Coordinates" << endl;
503     cout.precision(15);
504     cout.setf(ios::scientific, ios::floatfield);
505     pair<double,double> xdx = getFirstCoordAndSpacing(0);
506     pair<double,double> ydy = getFirstCoordAndSpacing(1);
507     for (index_t i=0; i < getNumNodes(); i++) {
508     cout << " " << setw(5) << m_nodeId[i]
509     << " " << xdx.first+(i%m_N0)*xdx.second
510     << " " << ydy.first+(i/m_N0)*ydy.second << endl;
511     }
512     }
513     }
514    
515     IndexVector Rectangle::getNumNodesPerDim() const
516     {
517     IndexVector ret;
518     ret.push_back(m_N0);
519     ret.push_back(m_N1);
520     return ret;
521     }
522    
523     IndexVector Rectangle::getNumElementsPerDim() const
524     {
525     IndexVector ret;
526     ret.push_back(m_NE0);
527     ret.push_back(m_NE1);
528     return ret;
529     }
530    
531     IndexVector Rectangle::getNumFacesPerBoundary() const
532     {
533     IndexVector ret(4, 0);
534     //left
535     if (m_offset0==0)
536     ret[0]=m_NE1;
537     //right
538     if (m_mpiInfo->rank%m_NX==m_NX-1)
539     ret[1]=m_NE1;
540     //bottom
541     if (m_offset1==0)
542     ret[2]=m_NE0;
543     //top
544     if (m_mpiInfo->rank/m_NX==m_NY-1)
545     ret[3]=m_NE0;
546     return ret;
547     }
548    
549 caltinay 3766 IndexVector Rectangle::getNumSubdivisionsPerDim() const
550     {
551     IndexVector ret;
552     ret.push_back(m_NX);
553     ret.push_back(m_NY);
554     return ret;
555     }
556    
557 caltinay 3764 pair<double,double> Rectangle::getFirstCoordAndSpacing(dim_t dim) const
558     {
559     if (dim==0) {
560     return pair<double,double>((m_l0*m_offset0)/m_gNE0, m_l0/m_gNE0);
561     } else if (dim==1) {
562     return pair<double,double>((m_l1*m_offset1)/m_gNE1, m_l1/m_gNE1);
563     }
564     throw RipleyException("getFirstCoordAndSpacing(): invalid argument");
565     }
566    
567     //protected
568     dim_t Rectangle::getNumDOF() const
569     {
570     return (m_gNE0+1)/m_NX*(m_gNE1+1)/m_NY;
571     }
572    
573     //protected
574     dim_t Rectangle::getNumFaceElements() const
575     {
576     const IndexVector faces = getNumFacesPerBoundary();
577     dim_t n=0;
578     for (size_t i=0; i<faces.size(); i++)
579     n+=faces[i];
580     return n;
581     }
582    
583     //protected
584     void Rectangle::assembleCoordinates(escript::Data& arg) const
585     {
586     escriptDataC x = arg.getDataC();
587     int numDim = m_numDim;
588     if (!isDataPointShapeEqual(&x, 1, &numDim))
589     throw RipleyException("setToX: Invalid Data object shape");
590     if (!numSamplesEqual(&x, 1, getNumNodes()))
591     throw RipleyException("setToX: Illegal number of samples in Data object");
592    
593     pair<double,double> xdx = getFirstCoordAndSpacing(0);
594     pair<double,double> ydy = getFirstCoordAndSpacing(1);
595     arg.requireWrite();
596     #pragma omp parallel for
597     for (dim_t i1 = 0; i1 < m_N1; i1++) {
598     for (dim_t i0 = 0; i0 < m_N0; i0++) {
599     double* point = arg.getSampleDataRW(i0+m_N0*i1);
600     point[0] = xdx.first+i0*xdx.second;
601     point[1] = ydy.first+i1*ydy.second;
602     }
603     }
604     }
605    
606     //protected
607     void Rectangle::assembleGradient(escript::Data& out, escript::Data& in) const
608     {
609 caltinay 3701 const dim_t numComp = in.getDataPointSize();
610 caltinay 3702 const double h0 = m_l0/m_gNE0;
611     const double h1 = m_l1/m_gNE1;
612 caltinay 3724 const double cx0 = -1./h0;
613     const double cx1 = -.78867513459481288225/h0;
614     const double cx2 = -.5/h0;
615     const double cx3 = -.21132486540518711775/h0;
616     const double cx4 = .21132486540518711775/h0;
617     const double cx5 = .5/h0;
618     const double cx6 = .78867513459481288225/h0;
619     const double cx7 = 1./h0;
620     const double cy0 = -1./h1;
621     const double cy1 = -.78867513459481288225/h1;
622     const double cy2 = -.5/h1;
623     const double cy3 = -.21132486540518711775/h1;
624     const double cy4 = .21132486540518711775/h1;
625     const double cy5 = .5/h1;
626     const double cy6 = .78867513459481288225/h1;
627     const double cy7 = 1./h1;
628    
629 caltinay 3702 if (out.getFunctionSpace().getTypeCode() == Elements) {
630 caltinay 3760 out.requireWrite();
631 caltinay 3697 #pragma omp parallel for
632 caltinay 3707 for (index_t k1=0; k1 < m_NE1; ++k1) {
633     for (index_t k0=0; k0 < m_NE0; ++k0) {
634 caltinay 3769 const double* f_10 = in.getSampleDataRO(INDEX2(k0+1,k1, m_N0));
635     const double* f_11 = in.getSampleDataRO(INDEX2(k0+1,k1+1, m_N0));
636     const double* f_01 = in.getSampleDataRO(INDEX2(k0,k1+1, m_N0));
637     const double* f_00 = in.getSampleDataRO(INDEX2(k0,k1, m_N0));
638 caltinay 3702 double* o = out.getSampleDataRW(INDEX2(k0,k1,m_NE0));
639     for (index_t i=0; i < numComp; ++i) {
640 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;
641     o[INDEX3(i,1,0,numComp,2)] = f_00[i]*cy1 + f_01[i]*cy6 + f_10[i]*cy3 + f_11[i]*cy4;
642     o[INDEX3(i,0,1,numComp,2)] = f_00[i]*cx1 + f_01[i]*cx3 + f_10[i]*cx6 + f_11[i]*cx4;
643     o[INDEX3(i,1,1,numComp,2)] = f_00[i]*cy3 + f_01[i]*cy4 + f_10[i]*cy1 + f_11[i]*cy6;
644     o[INDEX3(i,0,2,numComp,2)] = f_00[i]*cx3 + f_01[i]*cx1 + f_10[i]*cx4 + f_11[i]*cx6;
645     o[INDEX3(i,1,2,numComp,2)] = f_00[i]*cy1 + f_01[i]*cy6 + f_10[i]*cy3 + f_11[i]*cy4;
646     o[INDEX3(i,0,3,numComp,2)] = f_00[i]*cx3 + f_01[i]*cx1 + f_10[i]*cx4 + f_11[i]*cx6;
647     o[INDEX3(i,1,3,numComp,2)] = f_00[i]*cy3 + f_01[i]*cy4 + f_10[i]*cy1 + f_11[i]*cy6;
648 caltinay 3702 } /* end of component loop i */
649     } /* end of k0 loop */
650     } /* end of k1 loop */
651 caltinay 3711 } else if (out.getFunctionSpace().getTypeCode() == ReducedElements) {
652 caltinay 3760 out.requireWrite();
653 caltinay 3711 #pragma omp parallel for
654     for (index_t k1=0; k1 < m_NE1; ++k1) {
655     for (index_t k0=0; k0 < m_NE0; ++k0) {
656 caltinay 3769 const double* f_10 = in.getSampleDataRO(INDEX2(k0+1,k1, m_N0));
657     const double* f_11 = in.getSampleDataRO(INDEX2(k0+1,k1+1, m_N0));
658     const double* f_01 = in.getSampleDataRO(INDEX2(k0,k1+1, m_N0));
659     const double* f_00 = in.getSampleDataRO(INDEX2(k0,k1, m_N0));
660 caltinay 3711 double* o = out.getSampleDataRW(INDEX2(k0,k1,m_NE0));
661     for (index_t i=0; i < numComp; ++i) {
662 caltinay 3724 o[INDEX3(i,0,0,numComp,2)] = cx5*(f_10[i] + f_11[i]) + cx2*(f_00[i] + f_01[i]);
663     o[INDEX3(i,1,0,numComp,2)] = cy2*(f_00[i] + f_10[i]) + cy5*(f_01[i] + f_11[i]);
664 caltinay 3711 } /* end of component loop i */
665     } /* end of k0 loop */
666     } /* end of k1 loop */
667 caltinay 3707 } else if (out.getFunctionSpace().getTypeCode() == FaceElements) {
668 caltinay 3760 out.requireWrite();
669 caltinay 3722 #pragma omp parallel
670     {
671     if (m_faceOffset[0] > -1) {
672     #pragma omp for nowait
673     for (index_t k1=0; k1 < m_NE1; ++k1) {
674 caltinay 3769 const double* f_10 = in.getSampleDataRO(INDEX2(1,k1, m_N0));
675     const double* f_11 = in.getSampleDataRO(INDEX2(1,k1+1, m_N0));
676     const double* f_01 = in.getSampleDataRO(INDEX2(0,k1+1, m_N0));
677     const double* f_00 = in.getSampleDataRO(INDEX2(0,k1, m_N0));
678 caltinay 3722 double* o = out.getSampleDataRW(m_faceOffset[0]+k1);
679     for (index_t i=0; i < numComp; ++i) {
680 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;
681     o[INDEX3(i,1,0,numComp,2)] = f_00[i]*cy0 + f_01[i]*cy7;
682     o[INDEX3(i,0,1,numComp,2)] = f_00[i]*cx3 + f_01[i]*cx1 + f_10[i]*cx4 + f_11[i]*cx6;
683     o[INDEX3(i,1,1,numComp,2)] = f_00[i]*cy0 + f_01[i]*cy7;
684 caltinay 3722 } /* end of component loop i */
685     } /* end of k1 loop */
686     } /* end of face 0 */
687     if (m_faceOffset[1] > -1) {
688     #pragma omp for nowait
689     for (index_t k1=0; k1 < m_NE1; ++k1) {
690 caltinay 3769 const double* f_10 = in.getSampleDataRO(INDEX2(m_N0-1,k1, m_N0));
691     const double* f_11 = in.getSampleDataRO(INDEX2(m_N0-1,k1+1, m_N0));
692     const double* f_01 = in.getSampleDataRO(INDEX2(m_N0-2,k1+1, m_N0));
693     const double* f_00 = in.getSampleDataRO(INDEX2(m_N0-2,k1, m_N0));
694 caltinay 3722 double* o = out.getSampleDataRW(m_faceOffset[1]+k1);
695     for (index_t i=0; i < numComp; ++i) {
696 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;
697     o[INDEX3(i,1,0,numComp,2)] = f_10[i]*cy0 + f_11[i]*cy7;
698     o[INDEX3(i,0,1,numComp,2)] = f_00[i]*cx3 + f_01[i]*cx1 + f_10[i]*cx4 + f_11[i]*cx6;
699     o[INDEX3(i,1,1,numComp,2)] = f_10[i]*cy0 + f_11[i]*cy7;
700 caltinay 3722 } /* end of component loop i */
701     } /* end of k1 loop */
702     } /* end of face 1 */
703     if (m_faceOffset[2] > -1) {
704     #pragma omp for nowait
705     for (index_t k0=0; k0 < m_NE0; ++k0) {
706 caltinay 3769 const double* f_00 = in.getSampleDataRO(INDEX2(k0,0, m_N0));
707     const double* f_10 = in.getSampleDataRO(INDEX2(k0+1,0, m_N0));
708     const double* f_11 = in.getSampleDataRO(INDEX2(k0+1,1, m_N0));
709     const double* f_01 = in.getSampleDataRO(INDEX2(k0,1, m_N0));
710 caltinay 3722 double* o = out.getSampleDataRW(m_faceOffset[2]+k0);
711     for (index_t i=0; i < numComp; ++i) {
712 caltinay 3724 o[INDEX3(i,0,0,numComp,2)] = f_00[i]*cx0 + f_10[i]*cx7;
713     o[INDEX3(i,1,0,numComp,2)] = f_00[i]*cy1 + f_01[i]*cy6 + f_10[i]*cy3 + f_11[i]*cy4;
714     o[INDEX3(i,0,1,numComp,2)] = f_00[i]*cx0 + f_10[i]*cx7;
715     o[INDEX3(i,1,1,numComp,2)] = f_00[i]*cy3 + f_01[i]*cy4 + f_10[i]*cy1 + f_11[i]*cy6;
716 caltinay 3722 } /* end of component loop i */
717     } /* end of k0 loop */
718     } /* end of face 2 */
719     if (m_faceOffset[3] > -1) {
720     #pragma omp for nowait
721     for (index_t k0=0; k0 < m_NE0; ++k0) {
722 caltinay 3769 const double* f_11 = in.getSampleDataRO(INDEX2(k0+1,m_N1-1, m_N0));
723     const double* f_01 = in.getSampleDataRO(INDEX2(k0,m_N1-1, m_N0));
724     const double* f_10 = in.getSampleDataRO(INDEX2(k0+1,m_N1-2, m_N0));
725     const double* f_00 = in.getSampleDataRO(INDEX2(k0,m_N1-2, m_N0));
726 caltinay 3722 double* o = out.getSampleDataRW(m_faceOffset[3]+k0);
727     for (index_t i=0; i < numComp; ++i) {
728 caltinay 3724 o[INDEX3(i,0,0,numComp,2)] = f_01[i]*cx0 + f_11[i]*cx7;
729     o[INDEX3(i,1,0,numComp,2)] = f_00[i]*cy1 + f_01[i]*cy6 + f_10[i]*cy3 + f_11[i]*cy4;
730     o[INDEX3(i,0,1,numComp,2)] = f_01[i]*cx0 + f_11[i]*cx7;
731     o[INDEX3(i,1,1,numComp,2)] = f_00[i]*cy3 + f_01[i]*cy4 + f_10[i]*cy1 + f_11[i]*cy6;
732 caltinay 3722 } /* end of component loop i */
733     } /* end of k0 loop */
734     } /* end of face 3 */
735     } // end of parallel section
736 caltinay 3711 } else if (out.getFunctionSpace().getTypeCode() == ReducedFaceElements) {
737 caltinay 3760 out.requireWrite();
738 caltinay 3722 #pragma omp parallel
739     {
740     if (m_faceOffset[0] > -1) {
741     #pragma omp for nowait
742     for (index_t k1=0; k1 < m_NE1; ++k1) {
743 caltinay 3769 const double* f_10 = in.getSampleDataRO(INDEX2(1,k1, m_N0));
744     const double* f_11 = in.getSampleDataRO(INDEX2(1,k1+1, m_N0));
745     const double* f_01 = in.getSampleDataRO(INDEX2(0,k1+1, m_N0));
746     const double* f_00 = in.getSampleDataRO(INDEX2(0,k1, m_N0));
747 caltinay 3722 double* o = out.getSampleDataRW(m_faceOffset[0]+k1);
748     for (index_t i=0; i < numComp; ++i) {
749 caltinay 3724 o[INDEX3(i,0,0,numComp,2)] = cx5*(f_10[i] + f_11[i]) + cx2*(f_00[i] + f_01[i]);
750     o[INDEX3(i,1,0,numComp,2)] = f_00[i]*cy0 + f_01[i]*cy7;
751 caltinay 3722 } /* end of component loop i */
752     } /* end of k1 loop */
753     } /* end of face 0 */
754     if (m_faceOffset[1] > -1) {
755     #pragma omp for nowait
756     for (index_t k1=0; k1 < m_NE1; ++k1) {
757 caltinay 3769 const double* f_10 = in.getSampleDataRO(INDEX2(m_N0-1,k1, m_N0));
758     const double* f_11 = in.getSampleDataRO(INDEX2(m_N0-1,k1+1, m_N0));
759     const double* f_01 = in.getSampleDataRO(INDEX2(m_N0-2,k1+1, m_N0));
760     const double* f_00 = in.getSampleDataRO(INDEX2(m_N0-2,k1, m_N0));
761 caltinay 3722 double* o = out.getSampleDataRW(m_faceOffset[1]+k1);
762     for (index_t i=0; i < numComp; ++i) {
763 caltinay 3724 o[INDEX3(i,0,0,numComp,2)] = cx5*(f_10[i] + f_11[i]) + cx2*(f_00[i] + f_01[i]);
764     o[INDEX3(i,1,0,numComp,2)] = f_10[i]*cy0 + f_11[i]*cy7;
765 caltinay 3722 } /* end of component loop i */
766     } /* end of k1 loop */
767     } /* end of face 1 */
768     if (m_faceOffset[2] > -1) {
769     #pragma omp for nowait
770     for (index_t k0=0; k0 < m_NE0; ++k0) {
771 caltinay 3769 const double* f_00 = in.getSampleDataRO(INDEX2(k0,0, m_N0));
772     const double* f_10 = in.getSampleDataRO(INDEX2(k0+1,0, m_N0));
773     const double* f_11 = in.getSampleDataRO(INDEX2(k0+1,1, m_N0));
774     const double* f_01 = in.getSampleDataRO(INDEX2(k0,1, m_N0));
775 caltinay 3722 double* o = out.getSampleDataRW(m_faceOffset[2]+k0);
776     for (index_t i=0; i < numComp; ++i) {
777 caltinay 3724 o[INDEX3(i,0,0,numComp,2)] = f_00[i]*cx0 + f_10[i]*cx7;
778     o[INDEX3(i,1,0,numComp,2)] = cy2*(f_00[i] + f_10[i]) + cy5*(f_01[i] + f_11[i]);
779 caltinay 3722 } /* end of component loop i */
780     } /* end of k0 loop */
781     } /* end of face 2 */
782     if (m_faceOffset[3] > -1) {
783     #pragma omp for nowait
784     for (index_t k0=0; k0 < m_NE0; ++k0) {
785 caltinay 3769 const double* f_11 = in.getSampleDataRO(INDEX2(k0+1,m_N1-1, m_N0));
786     const double* f_01 = in.getSampleDataRO(INDEX2(k0,m_N1-1, m_N0));
787     const double* f_10 = in.getSampleDataRO(INDEX2(k0+1,m_N1-2, m_N0));
788     const double* f_00 = in.getSampleDataRO(INDEX2(k0,m_N1-2, m_N0));
789 caltinay 3722 double* o = out.getSampleDataRW(m_faceOffset[3]+k0);
790     for (index_t i=0; i < numComp; ++i) {
791 caltinay 3724 o[INDEX3(i,0,0,numComp,2)] = f_01[i]*cx0 + f_11[i]*cx7;
792     o[INDEX3(i,1,0,numComp,2)] = cy5*(f_01[i] + f_11[i]) + cy2*(f_00[i] + f_10[i]);
793 caltinay 3722 } /* end of component loop i */
794     } /* end of k0 loop */
795     } /* end of face 3 */
796     } // end of parallel section
797 caltinay 3702 }
798 caltinay 3701 }
799 caltinay 3697
800 caltinay 3764 //protected
801     void Rectangle::assembleIntegrate(vector<double>& integrals, escript::Data& arg) const
802 caltinay 3713 {
803 caltinay 3764 const dim_t numComp = arg.getDataPointSize();
804 caltinay 3713 const double h0 = m_l0/m_gNE0;
805     const double h1 = m_l1/m_gNE1;
806 caltinay 3764 const index_t left = (m_offset0==0 ? 0 : 1);
807     const index_t bottom = (m_offset1==0 ? 0 : 1);
808 caltinay 3713 if (arg.getFunctionSpace().getTypeCode() == Elements) {
809 caltinay 3764 const double w = h0*h1/4.;
810 caltinay 3713 #pragma omp parallel
811     {
812     vector<double> int_local(numComp, 0);
813 caltinay 3722 #pragma omp for nowait
814 caltinay 3764 for (index_t k1 = bottom; k1 < bottom+m_ownNE1; ++k1) {
815     for (index_t k0 = left; k0 < left+m_ownNE0; ++k0) {
816     const double* f = arg.getSampleDataRO(INDEX2(k0, k1, m_NE0));
817 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
818 caltinay 3769 const double f0 = f[INDEX2(i,0,numComp)];
819     const double f1 = f[INDEX2(i,1,numComp)];
820     const double f2 = f[INDEX2(i,2,numComp)];
821     const double f3 = f[INDEX2(i,3,numComp)];
822 caltinay 3764 int_local[i]+=(f0+f1+f2+f3)*w;
823 caltinay 3713 } /* end of component loop i */
824     } /* end of k0 loop */
825     } /* end of k1 loop */
826    
827     #pragma omp critical
828     for (index_t i=0; i<numComp; i++)
829     integrals[i]+=int_local[i];
830 caltinay 3722 } // end of parallel section
831 caltinay 3713 } else if (arg.getFunctionSpace().getTypeCode() == ReducedElements) {
832 caltinay 3764 const double w = h0*h1;
833 caltinay 3713 #pragma omp parallel
834     {
835     vector<double> int_local(numComp, 0);
836 caltinay 3722 #pragma omp for nowait
837 caltinay 3764 for (index_t k1 = bottom; k1 < bottom+m_ownNE1; ++k1) {
838     for (index_t k0 = left; k0 < left+m_ownNE0; ++k0) {
839     const double* f = arg.getSampleDataRO(INDEX2(k0, k1, m_NE0));
840 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
841 caltinay 3764 int_local[i]+=f[i]*w;
842 caltinay 3713 } /* end of component loop i */
843     } /* end of k0 loop */
844     } /* end of k1 loop */
845    
846     #pragma omp critical
847     for (index_t i=0; i<numComp; i++)
848     integrals[i]+=int_local[i];
849 caltinay 3722 } // end of parallel section
850 caltinay 3713 } else if (arg.getFunctionSpace().getTypeCode() == FaceElements) {
851 caltinay 3764 const double w0 = h0/2.;
852     const double w1 = h1/2.;
853 caltinay 3713 #pragma omp parallel
854     {
855     vector<double> int_local(numComp, 0);
856     if (m_faceOffset[0] > -1) {
857 caltinay 3722 #pragma omp for nowait
858 caltinay 3764 for (index_t k1 = bottom; k1 < bottom+m_ownNE1; ++k1) {
859     const double* f = arg.getSampleDataRO(m_faceOffset[0]+k1);
860 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
861 caltinay 3769 const double f0 = f[INDEX2(i,0,numComp)];
862     const double f1 = f[INDEX2(i,1,numComp)];
863 caltinay 3764 int_local[i]+=(f0+f1)*w1;
864 caltinay 3713 } /* end of component loop i */
865     } /* end of k1 loop */
866     }
867    
868     if (m_faceOffset[1] > -1) {
869 caltinay 3722 #pragma omp for nowait
870 caltinay 3764 for (index_t k1 = bottom; k1 < bottom+m_ownNE1; ++k1) {
871     const double* f = arg.getSampleDataRO(m_faceOffset[1]+k1);
872 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
873 caltinay 3769 const double f0 = f[INDEX2(i,0,numComp)];
874     const double f1 = f[INDEX2(i,1,numComp)];
875 caltinay 3764 int_local[i]+=(f0+f1)*w1;
876 caltinay 3713 } /* end of component loop i */
877     } /* end of k1 loop */
878     }
879    
880     if (m_faceOffset[2] > -1) {
881 caltinay 3722 #pragma omp for nowait
882 caltinay 3764 for (index_t k0 = left; k0 < left+m_ownNE0; ++k0) {
883     const double* f = arg.getSampleDataRO(m_faceOffset[2]+k0);
884 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
885 caltinay 3769 const double f0 = f[INDEX2(i,0,numComp)];
886     const double f1 = f[INDEX2(i,1,numComp)];
887 caltinay 3764 int_local[i]+=(f0+f1)*w0;
888 caltinay 3713 } /* end of component loop i */
889     } /* end of k0 loop */
890     }
891    
892     if (m_faceOffset[3] > -1) {
893 caltinay 3722 #pragma omp for nowait
894 caltinay 3764 for (index_t k0 = left; k0 < left+m_ownNE0; ++k0) {
895     const double* f = arg.getSampleDataRO(m_faceOffset[3]+k0);
896 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
897 caltinay 3769 const double f0 = f[INDEX2(i,0,numComp)];
898     const double f1 = f[INDEX2(i,1,numComp)];
899 caltinay 3764 int_local[i]+=(f0+f1)*w0;
900 caltinay 3713 } /* end of component loop i */
901     } /* end of k0 loop */
902     }
903    
904     #pragma omp critical
905     for (index_t i=0; i<numComp; i++)
906     integrals[i]+=int_local[i];
907 caltinay 3722 } // end of parallel section
908 caltinay 3713 } else if (arg.getFunctionSpace().getTypeCode() == ReducedFaceElements) {
909     #pragma omp parallel
910     {
911     vector<double> int_local(numComp, 0);
912     if (m_faceOffset[0] > -1) {
913 caltinay 3722 #pragma omp for nowait
914 caltinay 3764 for (index_t k1 = bottom; k1 < bottom+m_ownNE1; ++k1) {
915     const double* f = arg.getSampleDataRO(m_faceOffset[0]+k1);
916 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
917     int_local[i]+=f[i]*h1;
918     } /* end of component loop i */
919     } /* end of k1 loop */
920     }
921    
922     if (m_faceOffset[1] > -1) {
923 caltinay 3722 #pragma omp for nowait
924 caltinay 3764 for (index_t k1 = bottom; k1 < bottom+m_ownNE1; ++k1) {
925     const double* f = arg.getSampleDataRO(m_faceOffset[1]+k1);
926 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
927     int_local[i]+=f[i]*h1;
928     } /* end of component loop i */
929     } /* end of k1 loop */
930     }
931    
932     if (m_faceOffset[2] > -1) {
933 caltinay 3722 #pragma omp for nowait
934 caltinay 3764 for (index_t k0 = left; k0 < left+m_ownNE0; ++k0) {
935     const double* f = arg.getSampleDataRO(m_faceOffset[2]+k0);
936 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
937     int_local[i]+=f[i]*h0;
938     } /* end of component loop i */
939     } /* end of k0 loop */
940     }
941    
942     if (m_faceOffset[3] > -1) {
943 caltinay 3722 #pragma omp for nowait
944 caltinay 3764 for (index_t k0 = left; k0 < left+m_ownNE0; ++k0) {
945     const double* f = arg.getSampleDataRO(m_faceOffset[3]+k0);
946 caltinay 3713 for (index_t i=0; i < numComp; ++i) {
947     int_local[i]+=f[i]*h0;
948     } /* end of component loop i */
949     } /* end of k0 loop */
950     }
951    
952     #pragma omp critical
953     for (index_t i=0; i<numComp; i++)
954     integrals[i]+=int_local[i];
955 caltinay 3722 } // end of parallel section
956 caltinay 3713 }
957     }
958    
959 caltinay 3691 //protected
960 caltinay 3756 dim_t Rectangle::insertNeighbourNodes(IndexVector& index, index_t node) const
961     {
962     const dim_t nDOF0 = (m_gNE0+1)/m_NX;
963     const dim_t nDOF1 = (m_gNE1+1)/m_NY;
964     const int x=node%nDOF0;
965     const int y=node/nDOF0;
966     dim_t num=0;
967     // loop through potential neighbours and add to index if positions are
968     // within bounds
969     for (int i1=-1; i1<2; i1++) {
970     for (int i0=-1; i0<2; i0++) {
971     // skip node itself
972     if (i0==0 && i1==0)
973     continue;
974     // location of neighbour node
975     const int nx=x+i0;
976     const int ny=y+i1;
977     if (nx>=0 && ny>=0 && nx<nDOF0 && ny<nDOF1) {
978     index.push_back(ny*nDOF0+nx);
979     num++;
980     }
981     }
982     }
983    
984     return num;
985     }
986    
987     //protected
988     void Rectangle::nodesToDOF(escript::Data& out, escript::Data& in) const
989     {
990     const dim_t numComp = in.getDataPointSize();
991     out.requireWrite();
992    
993     const index_t left = (m_offset0==0 ? 0 : 1);
994     const index_t bottom = (m_offset1==0 ? 0 : 1);
995     const dim_t nDOF0 = (m_gNE0+1)/m_NX;
996     const dim_t nDOF1 = (m_gNE1+1)/m_NY;
997     #pragma omp parallel for
998     for (index_t i=0; i<nDOF1; i++) {
999     for (index_t j=0; j<nDOF0; j++) {
1000     const index_t n=j+left+(i+bottom)*m_N0;
1001     const double* src=in.getSampleDataRO(n);
1002     copy(src, src+numComp, out.getSampleDataRW(j+i*nDOF0));
1003     }
1004     }
1005     }
1006    
1007     //protected
1008     void Rectangle::dofToNodes(escript::Data& out, escript::Data& in) const
1009     {
1010     const dim_t numComp = in.getDataPointSize();
1011     Paso_Coupler* coupler = Paso_Coupler_alloc(m_connector, numComp);
1012     in.requireWrite();
1013     Paso_Coupler_startCollect(coupler, in.getSampleDataRW(0));
1014    
1015     const dim_t numDOF = getNumDOF();
1016     out.requireWrite();
1017     const double* buffer = Paso_Coupler_finishCollect(coupler);
1018    
1019     #pragma omp parallel for
1020     for (index_t i=0; i<getNumNodes(); i++) {
1021     const double* src=(m_dofMap[i]<numDOF ?
1022     in.getSampleDataRO(m_dofMap[i])
1023     : &buffer[(m_dofMap[i]-numDOF)*numComp]);
1024     copy(src, src+numComp, out.getSampleDataRW(i));
1025     }
1026     }
1027    
1028 caltinay 3691 //private
1029     void Rectangle::populateSampleIds()
1030     {
1031 caltinay 3752 // identifiers are ordered from left to right, bottom to top globablly.
1032 caltinay 3697
1033     // build node distribution vector first.
1034     // rank i owns m_nodeDistribution[i+1]-nodeDistribution[i] nodes
1035     m_nodeDistribution.assign(m_mpiInfo->size+1, 0);
1036 caltinay 3752 const dim_t numDOF=getNumDOF();
1037     for (dim_t k=1; k<m_mpiInfo->size; k++) {
1038     m_nodeDistribution[k]=k*numDOF;
1039 caltinay 3697 }
1040     m_nodeDistribution[m_mpiInfo->size]=getNumDataPointsGlobal();
1041 caltinay 3691 m_nodeId.resize(getNumNodes());
1042 caltinay 3753 m_dofId.resize(numDOF);
1043     m_elementId.resize(getNumElements());
1044     m_faceId.resize(getNumFaceElements());
1045 caltinay 3697
1046 caltinay 3753 #pragma omp parallel
1047     {
1048     // nodes
1049     #pragma omp for nowait
1050     for (dim_t i1=0; i1<m_N1; i1++) {
1051     for (dim_t i0=0; i0<m_N0; i0++) {
1052     m_nodeId[i0+i1*m_N0] = (m_offset1+i1)*(m_gNE0+1)+m_offset0+i0;
1053     }
1054 caltinay 3697 }
1055    
1056 caltinay 3753 // degrees of freedom
1057     #pragma omp for nowait
1058     for (dim_t k=0; k<numDOF; k++)
1059     m_dofId[k] = m_nodeDistribution[m_mpiInfo->rank]+k;
1060 caltinay 3752
1061 caltinay 3753 // elements
1062     #pragma omp for nowait
1063 caltinay 3755 for (dim_t i1=0; i1<m_NE1; i1++) {
1064     for (dim_t i0=0; i0<m_NE0; i0++) {
1065     m_elementId[i0+i1*m_NE0]=(m_offset1+i1)*m_gNE0+m_offset0+i0;
1066     }
1067     }
1068 caltinay 3753
1069     // face elements
1070     #pragma omp for
1071     for (dim_t k=0; k<getNumFaceElements(); k++)
1072     m_faceId[k]=k;
1073     } // end parallel section
1074    
1075 caltinay 3735 m_nodeTags.assign(getNumNodes(), 0);
1076     updateTagsInUse(Nodes);
1077 caltinay 3697
1078 caltinay 3735 m_elementTags.assign(getNumElements(), 0);
1079     updateTagsInUse(Elements);
1080 caltinay 3697
1081 caltinay 3722 // generate face offset vector and set face tags
1082 caltinay 3704 const IndexVector facesPerEdge = getNumFacesPerBoundary();
1083 caltinay 3722 const index_t LEFT=1, RIGHT=2, BOTTOM=10, TOP=20;
1084     const index_t faceTag[] = { LEFT, RIGHT, BOTTOM, TOP };
1085 caltinay 3704 m_faceOffset.assign(facesPerEdge.size(), -1);
1086 caltinay 3722 m_faceTags.clear();
1087 caltinay 3704 index_t offset=0;
1088     for (size_t i=0; i<facesPerEdge.size(); i++) {
1089     if (facesPerEdge[i]>0) {
1090     m_faceOffset[i]=offset;
1091     offset+=facesPerEdge[i];
1092 caltinay 3722 m_faceTags.insert(m_faceTags.end(), facesPerEdge[i], faceTag[i]);
1093 caltinay 3704 }
1094     }
1095 caltinay 3722 setTagMap("left", LEFT);
1096     setTagMap("right", RIGHT);
1097     setTagMap("bottom", BOTTOM);
1098     setTagMap("top", TOP);
1099     updateTagsInUse(FaceElements);
1100 caltinay 3691 }
1101    
1102 caltinay 3699 //private
1103 caltinay 3756 void Rectangle::createPattern()
1104 caltinay 3699 {
1105 caltinay 3756 const dim_t nDOF0 = (m_gNE0+1)/m_NX;
1106     const dim_t nDOF1 = (m_gNE1+1)/m_NY;
1107     const index_t left = (m_offset0==0 ? 0 : 1);
1108     const index_t bottom = (m_offset1==0 ? 0 : 1);
1109    
1110     // populate node->DOF mapping with own degrees of freedom.
1111     // The rest is assigned in the loop further down
1112     m_dofMap.assign(getNumNodes(), 0);
1113     #pragma omp parallel for
1114 caltinay 3766 for (index_t i=bottom; i<bottom+nDOF1; i++) {
1115     for (index_t j=left; j<left+nDOF0; j++) {
1116 caltinay 3756 m_dofMap[i*m_N0+j]=(i-bottom)*nDOF0+j-left;
1117     }
1118     }
1119    
1120     // build list of shared components and neighbours by looping through
1121     // all potential neighbouring ranks and checking if positions are
1122 caltinay 3754 // within bounds
1123 caltinay 3756 const dim_t numDOF=nDOF0*nDOF1;
1124     vector<IndexVector> colIndices(numDOF); // for the couple blocks
1125     RankVector neighbour;
1126     IndexVector offsetInShared(1,0);
1127     IndexVector sendShared, recvShared;
1128     int numShared=0;
1129     const int x=m_mpiInfo->rank%m_NX;
1130     const int y=m_mpiInfo->rank/m_NX;
1131 caltinay 3754 for (int i1=-1; i1<2; i1++) {
1132     for (int i0=-1; i0<2; i0++) {
1133 caltinay 3756 // skip this rank
1134 caltinay 3754 if (i0==0 && i1==0)
1135     continue;
1136 caltinay 3756 // location of neighbour rank
1137 caltinay 3754 const int nx=x+i0;
1138     const int ny=y+i1;
1139 caltinay 3756 if (nx>=0 && ny>=0 && nx<m_NX && ny<m_NY) {
1140     neighbour.push_back(ny*m_NX+nx);
1141     if (i0==0) {
1142     // sharing top or bottom edge
1143     const int firstDOF=(i1==-1 ? 0 : numDOF-nDOF0);
1144     const int firstNode=(i1==-1 ? left : m_N0*(m_N1-1)+left);
1145     offsetInShared.push_back(offsetInShared.back()+nDOF0);
1146     for (dim_t i=0; i<nDOF0; i++, numShared++) {
1147     sendShared.push_back(firstDOF+i);
1148     recvShared.push_back(numDOF+numShared);
1149     if (i>0)
1150     colIndices[firstDOF+i-1].push_back(numShared);
1151     colIndices[firstDOF+i].push_back(numShared);
1152     if (i<nDOF0-1)
1153     colIndices[firstDOF+i+1].push_back(numShared);
1154     m_dofMap[firstNode+i]=numDOF+numShared;
1155     }
1156     } else if (i1==0) {
1157     // sharing left or right edge
1158     const int firstDOF=(i0==-1 ? 0 : nDOF0-1);
1159     const int firstNode=(i0==-1 ? bottom*m_N0 : (bottom+1)*m_N0-1);
1160     offsetInShared.push_back(offsetInShared.back()+nDOF1);
1161     for (dim_t i=0; i<nDOF1; i++, numShared++) {
1162     sendShared.push_back(firstDOF+i*nDOF0);
1163     recvShared.push_back(numDOF+numShared);
1164     if (i>0)
1165     colIndices[firstDOF+(i-1)*nDOF0].push_back(numShared);
1166     colIndices[firstDOF+i*nDOF0].push_back(numShared);
1167     if (i<nDOF1-1)
1168     colIndices[firstDOF+(i+1)*nDOF0].push_back(numShared);
1169     m_dofMap[firstNode+i*m_N0]=numDOF+numShared;
1170     }
1171     } else {
1172     // sharing a node
1173     const int dof=(i0+1)/2*(nDOF0-1)+(i1+1)/2*(numDOF-nDOF0);
1174     const int node=(i0+1)/2*(m_N0-1)+(i1+1)/2*m_N0*(m_N1-1);
1175     offsetInShared.push_back(offsetInShared.back()+1);
1176     sendShared.push_back(dof);
1177     recvShared.push_back(numDOF+numShared);
1178     colIndices[dof].push_back(numShared);
1179     m_dofMap[node]=numDOF+numShared;
1180     ++numShared;
1181     }
1182 caltinay 3754 }
1183 caltinay 3699 }
1184     }
1185 caltinay 3754
1186 caltinay 3756 // create connector
1187     Paso_SharedComponents *snd_shcomp = Paso_SharedComponents_alloc(
1188     numDOF, neighbour.size(), &neighbour[0], &sendShared[0],
1189     &offsetInShared[0], 1, 0, m_mpiInfo);
1190     Paso_SharedComponents *rcv_shcomp = Paso_SharedComponents_alloc(
1191     numDOF, neighbour.size(), &neighbour[0], &recvShared[0],
1192     &offsetInShared[0], 1, 0, m_mpiInfo);
1193     m_connector = Paso_Connector_alloc(snd_shcomp, rcv_shcomp);
1194     Paso_SharedComponents_free(snd_shcomp);
1195     Paso_SharedComponents_free(rcv_shcomp);
1196 caltinay 3754
1197 caltinay 3756 // create main and couple blocks
1198     Paso_Pattern *mainPattern = createMainPattern();
1199     Paso_Pattern *colPattern, *rowPattern;
1200     createCouplePatterns(colIndices, numShared, &colPattern, &rowPattern);
1201 caltinay 3754
1202 caltinay 3756 // allocate paso distribution
1203     Paso_Distribution* distribution = Paso_Distribution_alloc(m_mpiInfo,
1204     const_cast<index_t*>(&m_nodeDistribution[0]), 1, 0);
1205 caltinay 3755
1206 caltinay 3756 // finally create the system matrix
1207     m_pattern = Paso_SystemMatrixPattern_alloc(MATRIX_FORMAT_DEFAULT,
1208     distribution, distribution, mainPattern, colPattern, rowPattern,
1209     m_connector, m_connector);
1210 caltinay 3755
1211 caltinay 3756 Paso_Distribution_free(distribution);
1212 caltinay 3755
1213 caltinay 3756 // useful debug output
1214     /*
1215     cout << "--- rcv_shcomp ---" << endl;
1216     cout << "numDOF=" << numDOF << ", numNeighbors=" << neighbour.size() << endl;
1217     for (size_t i=0; i<neighbour.size(); i++) {
1218     cout << "neighbor[" << i << "]=" << neighbour[i]
1219     << " offsetInShared[" << i+1 << "]=" << offsetInShared[i+1] << endl;
1220 caltinay 3699 }
1221 caltinay 3756 for (size_t i=0; i<recvShared.size(); i++) {
1222     cout << "shared[" << i << "]=" << recvShared[i] << endl;
1223     }
1224     cout << "--- snd_shcomp ---" << endl;
1225     for (size_t i=0; i<sendShared.size(); i++) {
1226     cout << "shared[" << i << "]=" << sendShared[i] << endl;
1227     }
1228     cout << "--- dofMap ---" << endl;
1229     for (size_t i=0; i<m_dofMap.size(); i++) {
1230     cout << "m_dofMap[" << i << "]=" << m_dofMap[i] << endl;
1231     }
1232     cout << "--- colIndices ---" << endl;
1233     for (size_t i=0; i<colIndices.size(); i++) {
1234     cout << "colIndices[" << i << "].size()=" << colIndices[i].size() << endl;
1235     }
1236     */
1237 caltinay 3754
1238 caltinay 3756 /*
1239     cout << "--- main_pattern ---" << endl;
1240     cout << "M=" << mainPattern->numOutput << ", N=" << mainPattern->numInput << endl;
1241     for (size_t i=0; i<mainPattern->numOutput+1; i++) {
1242     cout << "ptr[" << i << "]=" << mainPattern->ptr[i] << endl;
1243     }
1244     for (size_t i=0; i<mainPattern->ptr[mainPattern->numOutput]; i++) {
1245     cout << "index[" << i << "]=" << mainPattern->index[i] << endl;
1246     }
1247     */
1248 caltinay 3754
1249 caltinay 3756 /*
1250     cout << "--- colCouple_pattern ---" << endl;
1251     cout << "M=" << colPattern->numOutput << ", N=" << colPattern->numInput << endl;
1252     for (size_t i=0; i<colPattern->numOutput+1; i++) {
1253     cout << "ptr[" << i << "]=" << colPattern->ptr[i] << endl;
1254     }
1255     for (size_t i=0; i<colPattern->ptr[colPattern->numOutput]; i++) {
1256     cout << "index[" << i << "]=" << colPattern->index[i] << endl;
1257     }
1258     */
1259 caltinay 3754
1260 caltinay 3756 /*
1261     cout << "--- rowCouple_pattern ---" << endl;
1262     cout << "M=" << rowPattern->numOutput << ", N=" << rowPattern->numInput << endl;
1263     for (size_t i=0; i<rowPattern->numOutput+1; i++) {
1264     cout << "ptr[" << i << "]=" << rowPattern->ptr[i] << endl;
1265 caltinay 3699 }
1266 caltinay 3756 for (size_t i=0; i<rowPattern->ptr[rowPattern->numOutput]; i++) {
1267     cout << "index[" << i << "]=" << rowPattern->index[i] << endl;
1268     }
1269     */
1270    
1271     Paso_Pattern_free(mainPattern);
1272     Paso_Pattern_free(colPattern);
1273     Paso_Pattern_free(rowPattern);
1274 caltinay 3699 }
1275    
1276 caltinay 3776 //private
1277     void Rectangle::addToMatrixAndRHS(Paso_SystemMatrix* S, escript::Data& F,
1278     const vector<double>& EM_S, const vector<double>& EM_F, bool addS,
1279     bool addF, index_t firstNode, dim_t nEq, dim_t nComp) const
1280     {
1281     IndexVector rowIndex;
1282     rowIndex.push_back(m_dofMap[firstNode]);
1283     rowIndex.push_back(m_dofMap[firstNode+1]);
1284     rowIndex.push_back(m_dofMap[firstNode+m_N0]);
1285     rowIndex.push_back(m_dofMap[firstNode+m_N0+1]);
1286     if (addF) {
1287     double *F_p=F.getSampleDataRW(0);
1288     for (index_t i=0; i<rowIndex.size(); i++) {
1289     if (rowIndex[i]<getNumDOF()) {
1290     for (index_t eq=0; eq<nEq; eq++) {
1291     F_p[INDEX2(eq, rowIndex[i], nEq)]+=EM_F[INDEX2(eq,i,nEq)];
1292     }
1293     }
1294     }
1295     }
1296     if (addS) {
1297     addToSystemMatrix(S, rowIndex, nEq, rowIndex, nComp, EM_S);
1298     }
1299     }
1300    
1301 caltinay 3702 //protected
1302 caltinay 3711 void Rectangle::interpolateNodesOnElements(escript::Data& out,
1303     escript::Data& in, bool reduced) const
1304 caltinay 3702 {
1305     const dim_t numComp = in.getDataPointSize();
1306 caltinay 3711 if (reduced) {
1307 caltinay 3760 out.requireWrite();
1308 caltinay 3724 const double c0 = .25;
1309 caltinay 3702 #pragma omp parallel for
1310 caltinay 3711 for (index_t k1=0; k1 < m_NE1; ++k1) {
1311     for (index_t k0=0; k0 < m_NE0; ++k0) {
1312 caltinay 3769 const double* f_11 = in.getSampleDataRO(INDEX2(k0+1,k1+1, m_N0));
1313     const double* f_10 = in.getSampleDataRO(INDEX2(k0+1,k1, m_N0));
1314     const double* f_00 = in.getSampleDataRO(INDEX2(k0,k1, m_N0));
1315     const double* f_01 = in.getSampleDataRO(INDEX2(k0,k1+1, m_N0));
1316 caltinay 3711 double* o = out.getSampleDataRW(INDEX2(k0,k1,m_NE0));
1317     for (index_t i=0; i < numComp; ++i) {
1318 caltinay 3724 o[INDEX2(i,numComp,0)] = c0*(f_00[i] + f_01[i] + f_10[i] + f_11[i]);
1319 caltinay 3711 } /* end of component loop i */
1320     } /* end of k0 loop */
1321     } /* end of k1 loop */
1322     } else {
1323 caltinay 3760 out.requireWrite();
1324 caltinay 3724 const double c0 = .16666666666666666667;
1325     const double c1 = .044658198738520451079;
1326     const double c2 = .62200846792814621559;
1327 caltinay 3711 #pragma omp parallel for
1328     for (index_t k1=0; k1 < m_NE1; ++k1) {
1329     for (index_t k0=0; k0 < m_NE0; ++k0) {
1330 caltinay 3769 const double* f_10 = in.getSampleDataRO(INDEX2(k0+1,k1, m_N0));
1331     const double* f_11 = in.getSampleDataRO(INDEX2(k0+1,k1+1, m_N0));
1332     const double* f_01 = in.getSampleDataRO(INDEX2(k0,k1+1, m_N0));
1333     const double* f_00 = in.getSampleDataRO(INDEX2(k0,k1, m_N0));
1334 caltinay 3711 double* o = out.getSampleDataRW(INDEX2(k0,k1,m_NE0));
1335     for (index_t i=0; i < numComp; ++i) {
1336 caltinay 3724 o[INDEX2(i,numComp,0)] = f_00[i]*c2 + f_11[i]*c1 + c0*(f_01[i] + f_10[i]);
1337     o[INDEX2(i,numComp,1)] = f_01[i]*c1 + f_10[i]*c2 + c0*(f_00[i] + f_11[i]);
1338     o[INDEX2(i,numComp,2)] = f_01[i]*c2 + f_10[i]*c1 + c0*(f_00[i] + f_11[i]);
1339     o[INDEX2(i,numComp,3)] = f_00[i]*c1 + f_11[i]*c2 + c0*(f_01[i] + f_10[i]);
1340 caltinay 3711 } /* end of component loop i */
1341     } /* end of k0 loop */
1342     } /* end of k1 loop */
1343     }
1344 caltinay 3702 }
1345    
1346     //protected
1347 caltinay 3711 void Rectangle::interpolateNodesOnFaces(escript::Data& out, escript::Data& in,
1348     bool reduced) const
1349 caltinay 3702 {
1350 caltinay 3704 const dim_t numComp = in.getDataPointSize();
1351 caltinay 3711 if (reduced) {
1352 caltinay 3760 out.requireWrite();
1353 caltinay 3724 const double c0 = .5;
1354     #pragma omp parallel
1355     {
1356     if (m_faceOffset[0] > -1) {
1357     #pragma omp for nowait
1358     for (index_t k1=0; k1 < m_NE1; ++k1) {
1359 caltinay 3769 const double* f_00 = in.getSampleDataRO(INDEX2(0,k1, m_N0));
1360     const double* f_01 = in.getSampleDataRO(INDEX2(0,k1+1, m_N0));
1361 caltinay 3724 double* o = out.getSampleDataRW(m_faceOffset[0]+k1);
1362     for (index_t i=0; i < numComp; ++i) {
1363     o[INDEX2(i,numComp,0)] = c0*(f_00[i] + f_01[i]);
1364     } /* end of component loop i */
1365     } /* end of k1 loop */
1366     } /* end of face 0 */
1367     if (m_faceOffset[1] > -1) {
1368     #pragma omp for nowait
1369     for (index_t k1=0; k1 < m_NE1; ++k1) {
1370 caltinay 3769 const double* f_10 = in.getSampleDataRO(INDEX2(m_N0-1,k1, m_N0));
1371     const double* f_11 = in.getSampleDataRO(INDEX2(m_N0-1,k1+1, m_N0));
1372 caltinay 3724 double* o = out.getSampleDataRW(m_faceOffset[1]+k1);
1373     for (index_t i=0; i < numComp; ++i) {
1374     o[INDEX2(i,numComp,0)] = c0*(f_10[i] + f_11[i]);
1375     } /* end of component loop i */
1376     } /* end of k1 loop */
1377     } /* end of face 1 */
1378     if (m_faceOffset[2] > -1) {
1379     #pragma omp for nowait
1380     for (index_t k0=0; k0 < m_NE0; ++k0) {
1381 caltinay 3769 const double* f_10 = in.getSampleDataRO(INDEX2(k0+1,0, m_N0));
1382     const double* f_00 = in.getSampleDataRO(INDEX2(k0,0, m_N0));
1383 caltinay 3724 double* o = out.getSampleDataRW(m_faceOffset[2]+k0);
1384     for (index_t i=0; i < numComp; ++i) {
1385     o[INDEX2(i,numComp,0)] = c0*(f_00[i] + f_10[i]);
1386     } /* end of component loop i */
1387     } /* end of k0 loop */
1388     } /* end of face 2 */
1389     if (m_faceOffset[3] > -1) {
1390     #pragma omp for nowait
1391     for (index_t k0=0; k0 < m_NE0; ++k0) {
1392 caltinay 3769 const double* f_01 = in.getSampleDataRO(INDEX2(k0,m_N1-1, m_N0));
1393     const double* f_11 = in.getSampleDataRO(INDEX2(k0+1,m_N1-1, m_N0));
1394 caltinay 3724 double* o = out.getSampleDataRW(m_faceOffset[3]+k0);
1395     for (index_t i=0; i < numComp; ++i) {
1396     o[INDEX2(i,numComp,0)] = c0*(f_01[i] + f_11[i]);
1397     } /* end of component loop i */
1398     } /* end of k0 loop */
1399     } /* end of face 3 */
1400     } // end of parallel section
1401 caltinay 3711 } else {
1402 caltinay 3760 out.requireWrite();
1403 caltinay 3724 const double c0 = 0.21132486540518711775;
1404     const double c1 = 0.78867513459481288225;
1405     #pragma omp parallel
1406     {
1407     if (m_faceOffset[0] > -1) {
1408     #pragma omp for nowait
1409     for (index_t k1=0; k1 < m_NE1; ++k1) {
1410 caltinay 3769 const double* f_01 = in.getSampleDataRO(INDEX2(0,k1+1, m_N0));
1411     const double* f_00 = in.getSampleDataRO(INDEX2(0,k1, m_N0));
1412 caltinay 3724 double* o = out.getSampleDataRW(m_faceOffset[0]+k1);
1413     for (index_t i=0; i < numComp; ++i) {
1414     o[INDEX2(i,numComp,0)] = f_00[i]*c1 + f_01[i]*c0;
1415     o[INDEX2(i,numComp,1)] = f_00[i]*c0 + f_01[i]*c1;
1416     } /* end of component loop i */
1417     } /* end of k1 loop */
1418     } /* end of face 0 */
1419     if (m_faceOffset[1] > -1) {
1420     #pragma omp for nowait
1421     for (index_t k1=0; k1 < m_NE1; ++k1) {
1422 caltinay 3769 const double* f_10 = in.getSampleDataRO(INDEX2(m_N0-1,k1, m_N0));
1423     const double* f_11 = in.getSampleDataRO(INDEX2(m_N0-1,k1+1, m_N0));
1424 caltinay 3724 double* o = out.getSampleDataRW(m_faceOffset[1]+k1);
1425     for (index_t i=0; i < numComp; ++i) {
1426     o[INDEX2(i,numComp,0)] = f_10[i]*c1 + f_11[i]*c0;
1427     o[INDEX2(i,numComp,1)] = f_10[i]*c0 + f_11[i]*c1;
1428     } /* end of component loop i */
1429     } /* end of k1 loop */
1430     } /* end of face 1 */
1431     if (m_faceOffset[2] > -1) {
1432     #pragma omp for nowait
1433     for (index_t k0=0; k0 < m_NE0; ++k0) {
1434 caltinay 3769 const double* f_10 = in.getSampleDataRO(INDEX2(k0+1,0, m_N0));
1435     const double* f_00 = in.getSampleDataRO(INDEX2(k0,0, m_N0));
1436 caltinay 3724 double* o = out.getSampleDataRW(m_faceOffset[2]+k0);
1437     for (index_t i=0; i < numComp; ++i) {
1438     o[INDEX2(i,numComp,0)] = f_00[i]*c1 + f_10[i]*c0;
1439     o[INDEX2(i,numComp,1)] = f_00[i]*c0 + f_10[i]*c1;
1440     } /* end of component loop i */
1441     } /* end of k0 loop */
1442     } /* end of face 2 */
1443     if (m_faceOffset[3] > -1) {
1444     #pragma omp for nowait
1445     for (index_t k0=0; k0 < m_NE0; ++k0) {
1446 caltinay 3769 const double* f_11 = in.getSampleDataRO(INDEX2(k0+1,m_N1-1, m_N0));
1447     const double* f_01 = in.getSampleDataRO(INDEX2(k0,m_N1-1, m_N0));
1448 caltinay 3724 double* o = out.getSampleDataRW(m_faceOffset[3]+k0);
1449     for (index_t i=0; i < numComp; ++i) {
1450     o[INDEX2(i,numComp,0)] = f_01[i]*c1 + f_11[i]*c0;
1451     o[INDEX2(i,numComp,1)] = f_01[i]*c0 + f_11[i]*c1;
1452     } /* end of component loop i */
1453     } /* end of k0 loop */
1454     } /* end of face 3 */
1455     } // end of parallel section
1456 caltinay 3711 }
1457 caltinay 3702 }
1458    
1459 caltinay 3748 //protected
1460     void Rectangle::assemblePDESingle(Paso_SystemMatrix* mat,
1461     escript::Data& rhs, const escript::Data& A, const escript::Data& B,
1462     const escript::Data& C, const escript::Data& D,
1463 caltinay 3769 const escript::Data& X, const escript::Data& Y) const
1464 caltinay 3748 {
1465     const double h0 = m_l0/m_gNE0;
1466     const double h1 = m_l1/m_gNE1;
1467     const double w0 = -0.1555021169820365539*h1/h0;
1468     const double w1 = 0.041666666666666666667;
1469 caltinay 3764 const double w2 = -0.15550211698203655390;
1470     const double w3 = 0.041666666666666666667*h0/h1;
1471     const double w4 = 0.15550211698203655390;
1472     const double w5 = -0.041666666666666666667;
1473     const double w6 = -0.01116454968463011277*h1/h0;
1474     const double w7 = 0.011164549684630112770;
1475     const double w8 = -0.011164549684630112770;
1476     const double w9 = -0.041666666666666666667*h1/h0;
1477 caltinay 3748 const double w10 = -0.041666666666666666667*h0/h1;
1478     const double w11 = 0.1555021169820365539*h1/h0;
1479     const double w12 = 0.1555021169820365539*h0/h1;
1480     const double w13 = 0.01116454968463011277*h0/h1;
1481     const double w14 = 0.01116454968463011277*h1/h0;
1482     const double w15 = 0.041666666666666666667*h1/h0;
1483     const double w16 = -0.01116454968463011277*h0/h1;
1484     const double w17 = -0.1555021169820365539*h0/h1;
1485     const double w18 = -0.33333333333333333333*h1/h0;
1486 caltinay 3769 const double w19 = 0.25;
1487     const double w20 = -0.25;
1488 caltinay 3748 const double w21 = 0.16666666666666666667*h0/h1;
1489     const double w22 = -0.16666666666666666667*h1/h0;
1490     const double w23 = -0.16666666666666666667*h0/h1;
1491     const double w24 = 0.33333333333333333333*h1/h0;
1492     const double w25 = 0.33333333333333333333*h0/h1;
1493     const double w26 = 0.16666666666666666667*h1/h0;
1494     const double w27 = -0.33333333333333333333*h0/h1;
1495     const double w28 = -0.032861463941450536761*h1;
1496     const double w29 = -0.032861463941450536761*h0;
1497     const double w30 = -0.12264065304058601714*h1;
1498     const double w31 = -0.0023593469594139828636*h1;
1499     const double w32 = -0.008805202725216129906*h0;
1500     const double w33 = -0.008805202725216129906*h1;
1501     const double w34 = 0.032861463941450536761*h1;
1502     const double w35 = 0.008805202725216129906*h1;
1503     const double w36 = 0.008805202725216129906*h0;
1504     const double w37 = 0.0023593469594139828636*h1;
1505     const double w38 = 0.12264065304058601714*h1;
1506     const double w39 = 0.032861463941450536761*h0;
1507     const double w40 = -0.12264065304058601714*h0;
1508     const double w41 = -0.0023593469594139828636*h0;
1509     const double w42 = 0.0023593469594139828636*h0;
1510     const double w43 = 0.12264065304058601714*h0;
1511     const double w44 = -0.16666666666666666667*h1;
1512     const double w45 = -0.083333333333333333333*h0;
1513     const double w46 = 0.083333333333333333333*h1;
1514     const double w47 = 0.16666666666666666667*h1;
1515     const double w48 = 0.083333333333333333333*h0;
1516     const double w49 = -0.16666666666666666667*h0;
1517     const double w50 = 0.16666666666666666667*h0;
1518     const double w51 = -0.083333333333333333333*h1;
1519     const double w52 = 0.025917019497006092316*h0*h1;
1520     const double w53 = 0.0018607582807716854616*h0*h1;
1521     const double w54 = 0.0069444444444444444444*h0*h1;
1522     const double w55 = 0.09672363354357992482*h0*h1;
1523     const double w56 = 0.00049858867864229740201*h0*h1;
1524     const double w57 = 0.055555555555555555556*h0*h1;
1525     const double w58 = 0.027777777777777777778*h0*h1;
1526     const double w59 = 0.11111111111111111111*h0*h1;
1527     const double w60 = -0.19716878364870322056*h1;
1528     const double w61 = -0.19716878364870322056*h0;
1529     const double w62 = -0.052831216351296779436*h0;
1530     const double w63 = -0.052831216351296779436*h1;
1531     const double w64 = 0.19716878364870322056*h1;
1532     const double w65 = 0.052831216351296779436*h1;
1533     const double w66 = 0.19716878364870322056*h0;
1534     const double w67 = 0.052831216351296779436*h0;
1535     const double w68 = -0.5*h1;
1536     const double w69 = -0.5*h0;
1537     const double w70 = 0.5*h1;
1538     const double w71 = 0.5*h0;
1539     const double w72 = 0.1555021169820365539*h0*h1;
1540     const double w73 = 0.041666666666666666667*h0*h1;
1541     const double w74 = 0.01116454968463011277*h0*h1;
1542     const double w75 = 0.25*h0*h1;
1543    
1544     rhs.requireWrite();
1545     #pragma omp parallel
1546     {
1547 caltinay 3754 for (index_t k1_0=0; k1_0<2; k1_0++) { // colouring
1548 caltinay 3748 #pragma omp for
1549     for (index_t k1=k1_0; k1<m_NE1; k1+=2) {
1550     for (index_t k0=0; k0<m_NE0; ++k0) {
1551     bool add_EM_S=false;
1552     bool add_EM_F=false;
1553     vector<double> EM_S(4*4, 0);
1554     vector<double> EM_F(4, 0);
1555     const index_t e = k0 + m_NE0*k1;
1556     ///////////////
1557     // process A //
1558     ///////////////
1559     if (!A.isEmpty()) {
1560     add_EM_S=true;
1561     const double* A_p=const_cast<escript::Data*>(&A)->getSampleDataRO(e);
1562     if (A.actsExpanded()) {
1563 caltinay 3769 const double A_00_0 = A_p[INDEX3(0,0,0,2,2)];
1564     const double A_10_0 = A_p[INDEX3(1,0,0,2,2)];
1565     const double A_01_0 = A_p[INDEX3(0,1,0,2,2)];
1566     const double A_11_0 = A_p[INDEX3(1,1,0,2,2)];
1567     const double A_00_1 = A_p[INDEX3(0,0,1,2,2)];
1568     const double A_10_1 = A_p[INDEX3(1,0,1,2,2)];
1569     const double A_01_1 = A_p[INDEX3(0,1,1,2,2)];
1570     const double A_11_1 = A_p[INDEX3(1,1,1,2,2)];
1571     const double A_00_2 = A_p[INDEX3(0,0,2,2,2)];
1572     const double A_10_2 = A_p[INDEX3(1,0,2,2,2)];
1573     const double A_01_2 = A_p[INDEX3(0,1,2,2,2)];
1574     const double A_11_2 = A_p[INDEX3(1,1,2,2,2)];
1575     const double A_00_3 = A_p[INDEX3(0,0,3,2,2)];
1576     const double A_10_3 = A_p[INDEX3(1,0,3,2,2)];
1577     const double A_01_3 = A_p[INDEX3(0,1,3,2,2)];
1578     const double A_11_3 = A_p[INDEX3(1,1,3,2,2)];
1579     const double tmp0_0 = A_01_0 + A_01_3;
1580     const double tmp1_0 = A_00_0 + A_00_1;
1581     const double tmp2_0 = A_11_0 + A_11_1 + A_11_2 + A_11_3;
1582     const double tmp3_0 = A_00_2 + A_00_3;
1583     const double tmp4_0 = A_10_1 + A_10_2;
1584     const double tmp5_0 = A_00_0 + A_00_1 + A_00_2 + A_00_3;
1585     const double tmp6_0 = A_01_3 + A_10_0;
1586     const double tmp7_0 = A_01_0 + A_10_3;
1587     const double tmp8_0 = A_01_1 + A_01_2 + A_10_1 + A_10_2;
1588     const double tmp9_0 = A_01_0 + A_10_0;
1589     const double tmp12_0 = A_11_0 + A_11_2;
1590     const double tmp10_0 = A_01_3 + A_10_3;
1591     const double tmp14_0 = A_01_0 + A_01_3 + A_10_0 + A_10_3;
1592     const double tmp13_0 = A_01_2 + A_10_1;
1593     const double tmp11_0 = A_11_1 + A_11_3;
1594     const double tmp18_0 = A_01_1 + A_10_1;
1595     const double tmp15_0 = A_01_1 + A_10_2;
1596     const double tmp16_0 = A_10_0 + A_10_3;
1597     const double tmp17_0 = A_01_1 + A_01_2;
1598     const double tmp19_0 = A_01_2 + A_10_2;
1599     const double tmp0_1 = A_10_3*w8;
1600     const double tmp1_1 = tmp0_0*w1;
1601     const double tmp2_1 = A_01_1*w4;
1602     const double tmp3_1 = tmp1_0*w0;
1603     const double tmp4_1 = A_01_2*w7;
1604     const double tmp5_1 = tmp2_0*w3;
1605     const double tmp6_1 = tmp3_0*w6;
1606     const double tmp7_1 = A_10_0*w2;
1607     const double tmp8_1 = tmp4_0*w5;
1608     const double tmp9_1 = tmp2_0*w10;
1609     const double tmp14_1 = A_10_0*w8;
1610     const double tmp23_1 = tmp3_0*w14;
1611     const double tmp35_1 = A_01_0*w8;
1612     const double tmp54_1 = tmp13_0*w8;
1613     const double tmp20_1 = tmp9_0*w4;
1614     const double tmp25_1 = tmp12_0*w12;
1615     const double tmp44_1 = tmp7_0*w7;
1616     const double tmp26_1 = tmp10_0*w4;
1617     const double tmp52_1 = tmp18_0*w8;
1618     const double tmp48_1 = A_10_1*w7;
1619     const double tmp46_1 = A_01_3*w8;
1620     const double tmp50_1 = A_01_0*w2;
1621     const double tmp56_1 = tmp19_0*w8;
1622     const double tmp19_1 = A_10_3*w2;
1623     const double tmp47_1 = A_10_2*w4;
1624     const double tmp16_1 = tmp3_0*w0;
1625     const double tmp18_1 = tmp1_0*w6;
1626     const double tmp31_1 = tmp11_0*w12;
1627     const double tmp55_1 = tmp15_0*w2;
1628     const double tmp39_1 = A_10_2*w7;
1629     const double tmp11_1 = tmp6_0*w7;
1630     const double tmp40_1 = tmp11_0*w17;
1631     const double tmp34_1 = tmp15_0*w8;
1632     const double tmp33_1 = tmp14_0*w5;
1633     const double tmp24_1 = tmp11_0*w13;
1634     const double tmp43_1 = tmp17_0*w5;
1635     const double tmp15_1 = A_01_2*w4;
1636     const double tmp53_1 = tmp19_0*w2;
1637     const double tmp27_1 = tmp3_0*w11;
1638     const double tmp32_1 = tmp13_0*w2;
1639     const double tmp10_1 = tmp5_0*w9;
1640     const double tmp37_1 = A_10_1*w4;
1641     const double tmp38_1 = tmp5_0*w15;
1642     const double tmp17_1 = A_01_1*w7;
1643     const double tmp12_1 = tmp7_0*w4;
1644     const double tmp22_1 = tmp10_0*w7;
1645     const double tmp57_1 = tmp18_0*w2;
1646     const double tmp28_1 = tmp9_0*w7;
1647     const double tmp29_1 = tmp1_0*w14;
1648     const double tmp51_1 = tmp11_0*w16;
1649     const double tmp42_1 = tmp12_0*w16;
1650     const double tmp49_1 = tmp12_0*w17;
1651     const double tmp21_1 = tmp1_0*w11;
1652     const double tmp45_1 = tmp6_0*w4;
1653     const double tmp13_1 = tmp8_0*w1;
1654     const double tmp36_1 = tmp16_0*w1;
1655     const double tmp41_1 = A_01_3*w2;
1656     const double tmp30_1 = tmp12_0*w13;
1657 caltinay 3748 EM_S[INDEX2(0,0,4)]+=tmp13_1 + tmp20_1 + tmp21_1 + tmp22_1 + tmp23_1 + tmp24_1 + tmp25_1;
1658 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;
1659     EM_S[INDEX2(2,0,4)]+=tmp0_1 + tmp15_1 + tmp17_1 + tmp1_1 + tmp38_1 + tmp49_1 + tmp51_1 + tmp7_1 + tmp8_1;
1660 caltinay 3748 EM_S[INDEX2(3,0,4)]+=tmp10_1 + tmp32_1 + tmp33_1 + tmp34_1 + tmp9_1;
1661 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;
1662     EM_S[INDEX2(1,1,4)]+=tmp21_1 + tmp23_1 + tmp30_1 + tmp31_1 + tmp33_1 + tmp56_1 + tmp57_1;
1663     EM_S[INDEX2(2,1,4)]+=tmp10_1 + tmp13_1 + tmp44_1 + tmp45_1 + tmp9_1;
1664 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;
1665     EM_S[INDEX2(0,2,4)]+=tmp36_1 + tmp38_1 + tmp43_1 + tmp46_1 + tmp47_1 + tmp48_1 + tmp49_1 + tmp50_1 + tmp51_1;
1666 caltinay 3764 EM_S[INDEX2(1,2,4)]+=tmp10_1 + tmp11_1 + tmp12_1 + tmp13_1 + tmp9_1;
1667     EM_S[INDEX2(2,2,4)]+=tmp24_1 + tmp25_1 + tmp27_1 + tmp29_1 + tmp33_1 + tmp52_1 + tmp53_1;
1668     EM_S[INDEX2(3,2,4)]+=tmp14_1 + tmp15_1 + tmp16_1 + tmp17_1 + tmp18_1 + tmp19_1 + tmp1_1 + tmp5_1 + tmp8_1;
1669     EM_S[INDEX2(0,3,4)]+=tmp10_1 + tmp33_1 + tmp54_1 + tmp55_1 + tmp9_1;
1670 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;
1671     EM_S[INDEX2(2,3,4)]+=tmp16_1 + tmp18_1 + tmp35_1 + tmp36_1 + tmp41_1 + tmp43_1 + tmp47_1 + tmp48_1 + tmp5_1;
1672 caltinay 3764 EM_S[INDEX2(3,3,4)]+=tmp13_1 + tmp26_1 + tmp27_1 + tmp28_1 + tmp29_1 + tmp30_1 + tmp31_1;
1673     } else { // constant data
1674 caltinay 3769 const double A_00 = A_p[INDEX2(0,0,2)];
1675     const double A_10 = A_p[INDEX2(1,0,2)];
1676     const double A_01 = A_p[INDEX2(0,1,2)];
1677     const double A_11 = A_p[INDEX2(1,1,2)];
1678     const double tmp0_0 = A_01 + A_10;
1679     const double tmp0_1 = A_00*w18;
1680     const double tmp1_1 = A_01*w19;
1681     const double tmp2_1 = A_10*w20;
1682     const double tmp3_1 = A_11*w21;
1683     const double tmp4_1 = A_00*w22;
1684     const double tmp5_1 = tmp0_0*w19;
1685     const double tmp6_1 = A_11*w23;
1686     const double tmp7_1 = A_11*w25;
1687     const double tmp8_1 = A_00*w24;
1688     const double tmp9_1 = tmp0_0*w20;
1689     const double tmp10_1 = A_01*w20;
1690     const double tmp11_1 = A_11*w27;
1691     const double tmp12_1 = A_00*w26;
1692     const double tmp13_1 = A_10*w19;
1693 caltinay 3748 EM_S[INDEX2(0,0,4)]+=tmp5_1 + tmp7_1 + tmp8_1;
1694 caltinay 3764 EM_S[INDEX2(1,0,4)]+=tmp0_1 + tmp10_1 + tmp13_1 + tmp3_1;
1695     EM_S[INDEX2(2,0,4)]+=tmp11_1 + tmp12_1 + tmp1_1 + tmp2_1;
1696 caltinay 3748 EM_S[INDEX2(3,0,4)]+=tmp4_1 + tmp6_1 + tmp9_1;
1697 caltinay 3764 EM_S[INDEX2(0,1,4)]+=tmp0_1 + tmp1_1 + tmp2_1 + tmp3_1;
1698     EM_S[INDEX2(1,1,4)]+=tmp7_1 + tmp8_1 + tmp9_1;
1699     EM_S[INDEX2(2,1,4)]+=tmp4_1 + tmp5_1 + tmp6_1;
1700 caltinay 3748 EM_S[INDEX2(3,1,4)]+=tmp10_1 + tmp11_1 + tmp12_1 + tmp13_1;
1701     EM_S[INDEX2(0,2,4)]+=tmp10_1 + tmp11_1 + tmp12_1 + tmp13_1;
1702 caltinay 3764 EM_S[INDEX2(1,2,4)]+=tmp4_1 + tmp5_1 + tmp6_1;
1703     EM_S[INDEX2(2,2,4)]+=tmp7_1 + tmp8_1 + tmp9_1;
1704     EM_S[INDEX2(3,2,4)]+=tmp0_1 + tmp1_1 + tmp2_1 + tmp3_1;
1705     EM_S[INDEX2(0,3,4)]+=tmp4_1 + tmp6_1 + tmp9_1;
1706 caltinay 3748 EM_S[INDEX2(1,3,4)]+=tmp11_1 + tmp12_1 + tmp1_1 + tmp2_1;
1707     EM_S[INDEX2(2,3,4)]+=tmp0_1 + tmp10_1 + tmp13_1 + tmp3_1;
1708 caltinay 3764 EM_S[INDEX2(3,3,4)]+=tmp5_1 + tmp7_1 + tmp8_1;
1709 caltinay 3748 }
1710     }
1711     ///////////////
1712     // process B //
1713     ///////////////
1714     if (!B.isEmpty()) {
1715     add_EM_S=true;
1716     const double* B_p=const_cast<escript::Data*>(&B)->getSampleDataRO(e);
1717     if (B.actsExpanded()) {
1718 caltinay 3769 const double B_0_0 = B_p[INDEX2(0,0,2)];
1719     const double B_1_0 = B_p[INDEX2(1,0,2)];
1720     const double B_0_1 = B_p[INDEX2(0,1,2)];
1721     const double B_1_1 = B_p[INDEX2(1,1,2)];
1722     const double B_0_2 = B_p[INDEX2(0,2,2)];
1723     const double B_1_2 = B_p[INDEX2(1,2,2)];
1724     const double B_0_3 = B_p[INDEX2(0,3,2)];
1725     const double B_1_3 = B_p[INDEX2(1,3,2)];
1726     const double tmp0_0 = B_1_0 + B_1_1;
1727     const double tmp1_0 = B_1_2 + B_1_3;
1728     const double tmp2_0 = B_0_1 + B_0_3;
1729     const double tmp3_0 = B_0_0 + B_0_2;
1730     const double tmp63_1 = B_1_1*w42;
1731     const double tmp79_1 = B_1_1*w40;
1732     const double tmp37_1 = tmp3_0*w35;
1733     const double tmp8_1 = tmp0_0*w32;
1734     const double tmp71_1 = B_0_1*w34;
1735     const double tmp19_1 = B_0_3*w31;
1736     const double tmp15_1 = B_0_3*w34;
1737     const double tmp9_1 = tmp3_0*w34;
1738     const double tmp35_1 = B_1_0*w36;
1739     const double tmp66_1 = B_0_3*w28;
1740     const double tmp28_1 = B_1_0*w42;
1741     const double tmp22_1 = B_1_0*w40;
1742     const double tmp16_1 = B_1_2*w29;
1743     const double tmp6_1 = tmp2_0*w35;
1744     const double tmp55_1 = B_1_3*w40;
1745     const double tmp50_1 = B_1_3*w42;
1746     const double tmp7_1 = tmp1_0*w29;
1747     const double tmp1_1 = tmp1_0*w32;
1748     const double tmp57_1 = B_0_3*w30;
1749     const double tmp18_1 = B_1_1*w32;
1750     const double tmp53_1 = B_1_0*w41;
1751     const double tmp61_1 = B_1_3*w36;
1752     const double tmp27_1 = B_0_3*w38;
1753     const double tmp64_1 = B_0_2*w30;
1754     const double tmp76_1 = B_0_1*w38;
1755     const double tmp39_1 = tmp2_0*w34;
1756     const double tmp62_1 = B_0_1*w31;
1757     const double tmp56_1 = B_0_0*w31;
1758     const double tmp49_1 = B_1_1*w36;
1759     const double tmp2_1 = B_0_2*w31;
1760     const double tmp23_1 = B_0_2*w33;
1761     const double tmp38_1 = B_1_1*w43;
1762     const double tmp74_1 = B_1_2*w41;
1763     const double tmp43_1 = B_1_1*w41;
1764     const double tmp58_1 = B_0_2*w28;
1765     const double tmp67_1 = B_0_0*w33;
1766     const double tmp33_1 = tmp0_0*w39;
1767     const double tmp4_1 = B_0_0*w28;
1768     const double tmp20_1 = B_0_0*w30;
1769     const double tmp13_1 = B_0_2*w38;
1770     const double tmp65_1 = B_1_2*w43;
1771     const double tmp0_1 = tmp0_0*w29;
1772     const double tmp41_1 = tmp3_0*w33;
1773     const double tmp73_1 = B_0_2*w37;
1774     const double tmp69_1 = B_0_0*w38;
1775     const double tmp48_1 = B_1_2*w39;
1776     const double tmp59_1 = B_0_1*w33;
1777     const double tmp17_1 = B_1_3*w41;
1778     const double tmp5_1 = B_0_3*w33;
1779     const double tmp3_1 = B_0_1*w30;
1780     const double tmp21_1 = B_0_1*w28;
1781     const double tmp42_1 = B_1_0*w29;
1782     const double tmp54_1 = B_1_2*w32;
1783     const double tmp60_1 = B_1_0*w39;
1784     const double tmp32_1 = tmp1_0*w36;
1785     const double tmp10_1 = B_0_1*w37;
1786     const double tmp14_1 = B_0_0*w35;
1787     const double tmp29_1 = B_0_1*w35;
1788     const double tmp26_1 = B_1_2*w36;
1789     const double tmp30_1 = B_1_3*w43;
1790     const double tmp70_1 = B_0_2*w35;
1791     const double tmp34_1 = B_1_3*w39;
1792     const double tmp51_1 = B_1_0*w43;
1793     const double tmp31_1 = B_0_2*w34;
1794     const double tmp45_1 = tmp3_0*w28;
1795     const double tmp11_1 = tmp1_0*w39;
1796     const double tmp52_1 = B_1_1*w29;
1797     const double tmp44_1 = B_1_3*w32;
1798     const double tmp25_1 = B_1_1*w39;
1799     const double tmp47_1 = tmp2_0*w33;
1800     const double tmp72_1 = B_1_3*w29;
1801     const double tmp40_1 = tmp2_0*w28;
1802     const double tmp46_1 = B_1_2*w40;
1803     const double tmp36_1 = B_1_2*w42;
1804     const double tmp24_1 = B_0_0*w37;
1805     const double tmp77_1 = B_0_3*w35;
1806     const double tmp68_1 = B_0_3*w37;
1807     const double tmp78_1 = B_0_0*w34;
1808     const double tmp12_1 = tmp0_0*w36;
1809     const double tmp75_1 = B_1_0*w32;
1810 caltinay 3748 EM_S[INDEX2(0,0,4)]+=tmp16_1 + tmp17_1 + tmp18_1 + tmp19_1 + tmp20_1 + tmp21_1 + tmp22_1 + tmp23_1;
1811 caltinay 3764 EM_S[INDEX2(1,0,4)]+=tmp0_1 + tmp1_1 + tmp68_1 + tmp69_1 + tmp70_1 + tmp71_1;
1812     EM_S[INDEX2(2,0,4)]+=tmp45_1 + tmp47_1 + tmp48_1 + tmp49_1 + tmp50_1 + tmp51_1;
1813 caltinay 3748 EM_S[INDEX2(3,0,4)]+=tmp32_1 + tmp33_1 + tmp6_1 + tmp9_1;
1814 caltinay 3764 EM_S[INDEX2(0,1,4)]+=tmp0_1 + tmp1_1 + tmp2_1 + tmp3_1 + tmp4_1 + tmp5_1;
1815     EM_S[INDEX2(1,1,4)]+=tmp72_1 + tmp73_1 + tmp74_1 + tmp75_1 + tmp76_1 + tmp77_1 + tmp78_1 + tmp79_1;
1816     EM_S[INDEX2(2,1,4)]+=tmp32_1 + tmp33_1 + tmp40_1 + tmp41_1;
1817 caltinay 3748 EM_S[INDEX2(3,1,4)]+=tmp34_1 + tmp35_1 + tmp36_1 + tmp37_1 + tmp38_1 + tmp39_1;
1818     EM_S[INDEX2(0,2,4)]+=tmp42_1 + tmp43_1 + tmp44_1 + tmp45_1 + tmp46_1 + tmp47_1;
1819 caltinay 3764 EM_S[INDEX2(1,2,4)]+=tmp6_1 + tmp7_1 + tmp8_1 + tmp9_1;
1820     EM_S[INDEX2(2,2,4)]+=tmp60_1 + tmp61_1 + tmp62_1 + tmp63_1 + tmp64_1 + tmp65_1 + tmp66_1 + tmp67_1;
1821     EM_S[INDEX2(3,2,4)]+=tmp10_1 + tmp11_1 + tmp12_1 + tmp13_1 + tmp14_1 + tmp15_1;
1822     EM_S[INDEX2(0,3,4)]+=tmp40_1 + tmp41_1 + tmp7_1 + tmp8_1;
1823 caltinay 3748 EM_S[INDEX2(1,3,4)]+=tmp37_1 + tmp39_1 + tmp52_1 + tmp53_1 + tmp54_1 + tmp55_1;
1824     EM_S[INDEX2(2,3,4)]+=tmp11_1 + tmp12_1 + tmp56_1 + tmp57_1 + tmp58_1 + tmp59_1;
1825 caltinay 3764 EM_S[INDEX2(3,3,4)]+=tmp24_1 + tmp25_1 + tmp26_1 + tmp27_1 + tmp28_1 + tmp29_1 + tmp30_1 + tmp31_1;
1826     } else { // constant data
1827 caltinay 3769 const double B_0 = B_p[0];
1828     const double B_1 = B_p[1];
1829     const double tmp0_1 = B_0*w44;
1830     const double tmp1_1 = B_1*w45;
1831     const double tmp2_1 = B_0*w46;
1832     const double tmp3_1 = B_0*w47;
1833     const double tmp4_1 = B_1*w48;
1834     const double tmp5_1 = B_1*w49;
1835     const double tmp6_1 = B_1*w50;
1836     const double tmp7_1 = B_0*w51;
1837 caltinay 3748 EM_S[INDEX2(0,0,4)]+=tmp0_1 + tmp5_1;
1838 caltinay 3764 EM_S[INDEX2(1,0,4)]+=tmp1_1 + tmp3_1;
1839   &nbs