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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3971 - (hide annotations)
Wed Sep 19 02:55:35 2012 UTC (7 years, 2 months ago) by caltinay
File size: 234247 byte(s)
Implemented binary grid reader in ripley which is now used in ER Mapper source.
Interface/details likely to change but need to get this running now.

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