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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3754 - (hide annotations)
Wed Jan 4 07:07:37 2012 UTC (7 years, 10 months ago) by caltinay
Original Path: branches/ripleygmg_from_3668/ripley/src/Rectangle.cpp
File size: 108520 byte(s)
checkpoint. 3D single PDEs almost working

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