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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3756 - (hide annotations)
Fri Jan 6 02:35:19 2012 UTC (7 years, 11 months ago) by caltinay
File size: 104546 byte(s)
Fixed interpolation from DOF to nodes and moved common code to the base class.

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