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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3755 - (hide annotations)
Thu Jan 5 06:51:31 2012 UTC (7 years, 9 months ago) by caltinay
Original Path: branches/ripleygmg_from_3668/ripley/src/Rectangle.cpp
File size: 109320 byte(s)
Made element IDs globally unique so ownSample can work (to be implemented).
Fixed couple block in 3D so PDEs seem to work now with MPI :-)

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 caltinay 3755 for (dim_t i1=0; i1<m_NE1; i1++) {
1122     for (dim_t i0=0; i0<m_NE0; i0++) {
1123     m_elementId[i0+i1*m_NE0]=(m_offset1+i1)*m_gNE0+m_offset0+i0;
1124     }
1125     }
1126 caltinay 3753
1127     // face elements
1128     #pragma omp for
1129     for (dim_t k=0; k<getNumFaceElements(); k++)
1130     m_faceId[k]=k;
1131     } // end parallel section
1132    
1133 caltinay 3735 m_nodeTags.assign(getNumNodes(), 0);
1134     updateTagsInUse(Nodes);
1135 caltinay 3697
1136 caltinay 3735 m_elementTags.assign(getNumElements(), 0);
1137     updateTagsInUse(Elements);
1138 caltinay 3697
1139 caltinay 3722 // generate face offset vector and set face tags
1140 caltinay 3704 const IndexVector facesPerEdge = getNumFacesPerBoundary();
1141 caltinay 3722 const index_t LEFT=1, RIGHT=2, BOTTOM=10, TOP=20;
1142     const index_t faceTag[] = { LEFT, RIGHT, BOTTOM, TOP };
1143 caltinay 3704 m_faceOffset.assign(facesPerEdge.size(), -1);
1144 caltinay 3722 m_faceTags.clear();
1145 caltinay 3704 index_t offset=0;
1146     for (size_t i=0; i<facesPerEdge.size(); i++) {
1147     if (facesPerEdge[i]>0) {
1148     m_faceOffset[i]=offset;
1149     offset+=facesPerEdge[i];
1150 caltinay 3722 m_faceTags.insert(m_faceTags.end(), facesPerEdge[i], faceTag[i]);
1151 caltinay 3704 }
1152     }
1153 caltinay 3722 setTagMap("left", LEFT);
1154     setTagMap("right", RIGHT);
1155     setTagMap("bottom", BOTTOM);
1156     setTagMap("top", TOP);
1157     updateTagsInUse(FaceElements);
1158 caltinay 3691 }
1159    
1160 caltinay 3699 //private
1161     int Rectangle::insertNeighbours(IndexVector& index, index_t node) const
1162     {
1163 caltinay 3754 const index_t nDOF0 = (m_gNE0+1)/m_NX;
1164     const index_t nDOF1 = (m_gNE1+1)/m_NY;
1165     const int x=node%nDOF0;
1166     const int y=node/nDOF0;
1167 caltinay 3699 int num=0;
1168 caltinay 3754 // loop through potential neighbours and add to index if positions are
1169     // within bounds
1170     for (int i1=-1; i1<2; i1++) {
1171     for (int i0=-1; i0<2; i0++) {
1172     // skip node itself
1173     if (i0==0 && i1==0)
1174     continue;
1175     // location of neighbour node
1176     const int nx=x+i0;
1177     const int ny=y+i1;
1178     if (nx>=0 && ny>=0 && nx<nDOF0 && ny<nDOF1) {
1179     index.push_back(ny*nDOF0+nx);
1180     num++;
1181     }
1182 caltinay 3699 }
1183     }
1184 caltinay 3754
1185     return num;
1186     }
1187    
1188     //protected
1189     void Rectangle::nodesToDOF(escript::Data& out, escript::Data& in) const
1190     {
1191     const dim_t numComp = in.getDataPointSize();
1192     out.requireWrite();
1193    
1194     const index_t left = (m_offset0==0 ? 0 : 1);
1195     const index_t bottom = (m_offset1==0 ? 0 : 1);
1196 caltinay 3755 const index_t nDOF0 = (m_gNE0+1)/m_NX;
1197     const index_t nDOF1 = (m_gNE1+1)/m_NY;
1198     #pragma omp parallel for
1199     for (index_t i=0; i<nDOF1; i++) {
1200     for (index_t j=0; j<nDOF0; j++) {
1201     const index_t n=j+left+(i+bottom)*m_N0;
1202     const double* src=in.getSampleDataRO(n);
1203     copy(src, src+numComp, out.getSampleDataRW(j+i*nDOF0));
1204     }
1205     }
1206     }
1207    
1208     //protected
1209     void Rectangle::dofToNodes(escript::Data& out, escript::Data& in) const
1210     {
1211     const dim_t numComp = in.getDataPointSize();
1212     out.requireWrite();
1213    
1214     //TODO: use coupler to get the rest of the values
1215    
1216     const index_t left = (m_offset0==0 ? 0 : 1);
1217     const index_t bottom = (m_offset1==0 ? 0 : 1);
1218     const index_t nDOF0 = (m_gNE0+1)/m_NX;
1219     const index_t nDOF1 = (m_gNE1+1)/m_NY;
1220     #pragma omp parallel for
1221     for (index_t i=0; i<nDOF1; i++) {
1222     for (index_t j=0; j<nDOF0; j++) {
1223     const double* src=in.getSampleDataRO(j+i*nDOF0);
1224     const index_t n=j+left+(i+bottom)*m_N0;
1225 caltinay 3754 copy(src, src+numComp, out.getSampleDataRW(n));
1226 caltinay 3699 }
1227     }
1228 caltinay 3754 }
1229    
1230     //protected
1231     void Rectangle::addToSystemMatrix(Paso_SystemMatrix* mat,
1232     const IndexVector& nodes_Eq, dim_t num_Eq, const IndexVector& nodes_Sol,
1233     dim_t num_Sol, const vector<double>& array) const
1234     {
1235     const dim_t numMyCols = mat->pattern->mainPattern->numInput;
1236     const dim_t numMyRows = mat->pattern->mainPattern->numOutput;
1237    
1238     const index_t* mainBlock_ptr = mat->mainBlock->pattern->ptr;
1239     const index_t* mainBlock_index = mat->mainBlock->pattern->index;
1240     double* mainBlock_val = mat->mainBlock->val;
1241     const index_t* col_coupleBlock_ptr = mat->col_coupleBlock->pattern->ptr;
1242     const index_t* col_coupleBlock_index = mat->col_coupleBlock->pattern->index;
1243     double* col_coupleBlock_val = mat->col_coupleBlock->val;
1244     const index_t* row_coupleBlock_ptr = mat->row_coupleBlock->pattern->ptr;
1245     const index_t* row_coupleBlock_index = mat->row_coupleBlock->pattern->index;
1246     double* row_coupleBlock_val = mat->row_coupleBlock->val;
1247    
1248     for (dim_t k_Eq = 0; k_Eq < nodes_Eq.size(); ++k_Eq) {
1249     // down columns of array
1250     const dim_t j_Eq = nodes_Eq[k_Eq];
1251     const dim_t i_row = j_Eq;
1252     //printf("row:%d\n", i_row);
1253     // only look at the matrix rows stored on this processor
1254     if (i_row < numMyRows) {
1255     for (dim_t k_Sol = 0; k_Sol < nodes_Sol.size(); ++k_Sol) {
1256     const dim_t i_col = nodes_Sol[k_Sol];
1257     if (i_col < numMyCols) {
1258     for (dim_t k = mainBlock_ptr[i_row]; k < mainBlock_ptr[i_row + 1]; ++k) {
1259     if (mainBlock_index[k] == i_col) {
1260     mainBlock_val[k] += array[INDEX2(k_Eq, k_Sol, nodes_Eq.size())];
1261     break;
1262     }
1263     }
1264     } else {
1265     for (dim_t k = col_coupleBlock_ptr[i_row]; k < col_coupleBlock_ptr[i_row + 1]; ++k) {
1266     //cout << "col:" << i_col-numMyCols << " colIdx:" << col_coupleBlock_index[k] << endl;
1267     if (col_coupleBlock_index[k] == i_col - numMyCols) {
1268     col_coupleBlock_val[k] += array[INDEX2(k_Eq, k_Sol, nodes_Eq.size())];
1269     break;
1270     }
1271     }
1272     }
1273     }
1274     } else {
1275     for (dim_t k_Sol = 0; k_Sol < nodes_Sol.size(); ++k_Sol) {
1276     // across rows of array
1277     const dim_t i_col = nodes_Sol[k_Sol];
1278     if (i_col < numMyCols) {
1279     for (dim_t k = row_coupleBlock_ptr[i_row - numMyRows];
1280     k < row_coupleBlock_ptr[i_row - numMyRows + 1]; ++k)
1281     {
1282     //cout << "col:" << i_col << " rowIdx:" << row_coupleBlock_index[k] << endl;
1283     if (row_coupleBlock_index[k] == i_col) {
1284     row_coupleBlock_val[k] += array[INDEX2(k_Eq, k_Sol, nodes_Eq.size())];
1285     break;
1286     }
1287     }
1288     }
1289     }
1290 caltinay 3699 }
1291     }
1292     }
1293    
1294 caltinay 3702 //protected
1295 caltinay 3711 void Rectangle::interpolateNodesOnElements(escript::Data& out,
1296     escript::Data& in, bool reduced) const
1297 caltinay 3702 {
1298     const dim_t numComp = in.getDataPointSize();
1299 caltinay 3711 if (reduced) {
1300 caltinay 3724 /*** GENERATOR SNIP_INTERPOLATE_REDUCED_ELEMENTS TOP */
1301     const double c0 = .25;
1302 caltinay 3702 #pragma omp parallel for
1303 caltinay 3711 for (index_t k1=0; k1 < m_NE1; ++k1) {
1304     for (index_t k0=0; k0 < m_NE0; ++k0) {
1305     const register double* f_11 = in.getSampleDataRO(INDEX2(k0+1,k1+1, m_N0));
1306     const register double* f_10 = in.getSampleDataRO(INDEX2(k0+1,k1, m_N0));
1307     const register double* f_00 = in.getSampleDataRO(INDEX2(k0,k1, m_N0));
1308     const register double* f_01 = in.getSampleDataRO(INDEX2(k0,k1+1, m_N0));
1309     double* o = out.getSampleDataRW(INDEX2(k0,k1,m_NE0));
1310     for (index_t i=0; i < numComp; ++i) {
1311 caltinay 3724 o[INDEX2(i,numComp,0)] = c0*(f_00[i] + f_01[i] + f_10[i] + f_11[i]);
1312 caltinay 3711 } /* end of component loop i */
1313     } /* end of k0 loop */
1314     } /* end of k1 loop */
1315     /* GENERATOR SNIP_INTERPOLATE_REDUCED_ELEMENTS BOTTOM */
1316     } else {
1317 caltinay 3724 /*** GENERATOR SNIP_INTERPOLATE_ELEMENTS TOP */
1318     const double c0 = .16666666666666666667;
1319     const double c1 = .044658198738520451079;
1320     const double c2 = .62200846792814621559;
1321 caltinay 3711 #pragma omp parallel for
1322     for (index_t k1=0; k1 < m_NE1; ++k1) {
1323     for (index_t k0=0; k0 < m_NE0; ++k0) {
1324     const register double* f_10 = in.getSampleDataRO(INDEX2(k0+1,k1, m_N0));
1325     const register double* f_11 = in.getSampleDataRO(INDEX2(k0+1,k1+1, m_N0));
1326     const register double* f_01 = in.getSampleDataRO(INDEX2(k0,k1+1, m_N0));
1327     const register double* f_00 = in.getSampleDataRO(INDEX2(k0,k1, m_N0));
1328     double* o = out.getSampleDataRW(INDEX2(k0,k1,m_NE0));
1329     for (index_t i=0; i < numComp; ++i) {
1330 caltinay 3724 o[INDEX2(i,numComp,0)] = f_00[i]*c2 + f_11[i]*c1 + c0*(f_01[i] + f_10[i]);
1331     o[INDEX2(i,numComp,1)] = f_01[i]*c1 + f_10[i]*c2 + c0*(f_00[i] + f_11[i]);
1332     o[INDEX2(i,numComp,2)] = f_01[i]*c2 + f_10[i]*c1 + c0*(f_00[i] + f_11[i]);
1333     o[INDEX2(i,numComp,3)] = f_00[i]*c1 + f_11[i]*c2 + c0*(f_01[i] + f_10[i]);
1334 caltinay 3711 } /* end of component loop i */
1335     } /* end of k0 loop */
1336     } /* end of k1 loop */
1337     /* GENERATOR SNIP_INTERPOLATE_ELEMENTS BOTTOM */
1338     }
1339 caltinay 3702 }
1340    
1341     //protected
1342 caltinay 3711 void Rectangle::interpolateNodesOnFaces(escript::Data& out, escript::Data& in,
1343     bool reduced) const
1344 caltinay 3702 {
1345 caltinay 3704 const dim_t numComp = in.getDataPointSize();
1346 caltinay 3711 if (reduced) {
1347 caltinay 3724 const double c0 = .5;
1348     #pragma omp parallel
1349     {
1350     /*** GENERATOR SNIP_INTERPOLATE_REDUCED_FACES TOP */
1351     if (m_faceOffset[0] > -1) {
1352     #pragma omp for nowait
1353     for (index_t k1=0; k1 < m_NE1; ++k1) {
1354     const register double* f_00 = in.getSampleDataRO(INDEX2(0,k1, m_N0));
1355     const register double* f_01 = in.getSampleDataRO(INDEX2(0,k1+1, m_N0));
1356     double* o = out.getSampleDataRW(m_faceOffset[0]+k1);
1357     for (index_t i=0; i < numComp; ++i) {
1358     o[INDEX2(i,numComp,0)] = c0*(f_00[i] + f_01[i]);
1359     } /* end of component loop i */
1360     } /* end of k1 loop */
1361     } /* end of face 0 */
1362     if (m_faceOffset[1] > -1) {
1363     #pragma omp for nowait
1364     for (index_t k1=0; k1 < m_NE1; ++k1) {
1365     const register double* f_10 = in.getSampleDataRO(INDEX2(m_N0-1,k1, m_N0));
1366     const register double* f_11 = in.getSampleDataRO(INDEX2(m_N0-1,k1+1, m_N0));
1367     double* o = out.getSampleDataRW(m_faceOffset[1]+k1);
1368     for (index_t i=0; i < numComp; ++i) {
1369     o[INDEX2(i,numComp,0)] = c0*(f_10[i] + f_11[i]);
1370     } /* end of component loop i */
1371     } /* end of k1 loop */
1372     } /* end of face 1 */
1373     if (m_faceOffset[2] > -1) {
1374     #pragma omp for nowait
1375     for (index_t k0=0; k0 < m_NE0; ++k0) {
1376     const register double* f_10 = in.getSampleDataRO(INDEX2(k0+1,0, m_N0));
1377     const register double* f_00 = in.getSampleDataRO(INDEX2(k0,0, m_N0));
1378     double* o = out.getSampleDataRW(m_faceOffset[2]+k0);
1379     for (index_t i=0; i < numComp; ++i) {
1380     o[INDEX2(i,numComp,0)] = c0*(f_00[i] + f_10[i]);
1381     } /* end of component loop i */
1382     } /* end of k0 loop */
1383     } /* end of face 2 */
1384     if (m_faceOffset[3] > -1) {
1385     #pragma omp for nowait
1386     for (index_t k0=0; k0 < m_NE0; ++k0) {
1387     const register double* f_01 = in.getSampleDataRO(INDEX2(k0,m_N1-1, m_N0));
1388     const register double* f_11 = in.getSampleDataRO(INDEX2(k0+1,m_N1-1, m_N0));
1389     double* o = out.getSampleDataRW(m_faceOffset[3]+k0);
1390     for (index_t i=0; i < numComp; ++i) {
1391     o[INDEX2(i,numComp,0)] = c0*(f_01[i] + f_11[i]);
1392     } /* end of component loop i */
1393     } /* end of k0 loop */
1394     } /* end of face 3 */
1395     /* GENERATOR SNIP_INTERPOLATE_REDUCED_FACES BOTTOM */
1396     } // end of parallel section
1397 caltinay 3711 } else {
1398 caltinay 3724 const double c0 = 0.21132486540518711775;
1399     const double c1 = 0.78867513459481288225;
1400     #pragma omp parallel
1401     {
1402     /*** GENERATOR SNIP_INTERPOLATE_FACES TOP */
1403     if (m_faceOffset[0] > -1) {
1404     #pragma omp for nowait
1405     for (index_t k1=0; k1 < m_NE1; ++k1) {
1406     const register double* f_01 = in.getSampleDataRO(INDEX2(0,k1+1, m_N0));
1407     const register double* f_00 = in.getSampleDataRO(INDEX2(0,k1, m_N0));
1408     double* o = out.getSampleDataRW(m_faceOffset[0]+k1);
1409     for (index_t i=0; i < numComp; ++i) {
1410     o[INDEX2(i,numComp,0)] = f_00[i]*c1 + f_01[i]*c0;
1411     o[INDEX2(i,numComp,1)] = f_00[i]*c0 + f_01[i]*c1;
1412     } /* end of component loop i */
1413     } /* end of k1 loop */
1414     } /* end of face 0 */
1415     if (m_faceOffset[1] > -1) {
1416     #pragma omp for nowait
1417     for (index_t k1=0; k1 < m_NE1; ++k1) {
1418     const register double* f_10 = in.getSampleDataRO(INDEX2(m_N0-1,k1, m_N0));
1419     const register double* f_11 = in.getSampleDataRO(INDEX2(m_N0-1,k1+1, m_N0));
1420     double* o = out.getSampleDataRW(m_faceOffset[1]+k1);
1421     for (index_t i=0; i < numComp; ++i) {
1422     o[INDEX2(i,numComp,0)] = f_10[i]*c1 + f_11[i]*c0;
1423     o[INDEX2(i,numComp,1)] = f_10[i]*c0 + f_11[i]*c1;
1424     } /* end of component loop i */
1425     } /* end of k1 loop */
1426     } /* end of face 1 */
1427     if (m_faceOffset[2] > -1) {
1428     #pragma omp for nowait
1429     for (index_t k0=0; k0 < m_NE0; ++k0) {
1430     const register double* f_10 = in.getSampleDataRO(INDEX2(k0+1,0, m_N0));
1431     const register double* f_00 = in.getSampleDataRO(INDEX2(k0,0, m_N0));
1432     double* o = out.getSampleDataRW(m_faceOffset[2]+k0);
1433     for (index_t i=0; i < numComp; ++i) {
1434     o[INDEX2(i,numComp,0)] = f_00[i]*c1 + f_10[i]*c0;
1435     o[INDEX2(i,numComp,1)] = f_00[i]*c0 + f_10[i]*c1;
1436     } /* end of component loop i */
1437     } /* end of k0 loop */
1438     } /* end of face 2 */
1439     if (m_faceOffset[3] > -1) {
1440     #pragma omp for nowait
1441     for (index_t k0=0; k0 < m_NE0; ++k0) {
1442     const register double* f_11 = in.getSampleDataRO(INDEX2(k0+1,m_N1-1, m_N0));
1443     const register double* f_01 = in.getSampleDataRO(INDEX2(k0,m_N1-1, m_N0));
1444     double* o = out.getSampleDataRW(m_faceOffset[3]+k0);
1445     for (index_t i=0; i < numComp; ++i) {
1446     o[INDEX2(i,numComp,0)] = f_01[i]*c1 + f_11[i]*c0;
1447     o[INDEX2(i,numComp,1)] = f_01[i]*c0 + f_11[i]*c1;
1448     } /* end of component loop i */
1449     } /* end of k0 loop */
1450     } /* end of face 3 */
1451     /* GENERATOR SNIP_INTERPOLATE_FACES BOTTOM */
1452     } // end of parallel section
1453 caltinay 3711 }
1454 caltinay 3702 }
1455    
1456 caltinay 3748 //protected
1457     void Rectangle::assemblePDESingle(Paso_SystemMatrix* mat,
1458     escript::Data& rhs, const escript::Data& A, const escript::Data& B,
1459     const escript::Data& C, const escript::Data& D,
1460     const escript::Data& X, const escript::Data& Y,
1461     const escript::Data& d, const escript::Data& y) const
1462     {
1463     const double h0 = m_l0/m_gNE0;
1464     const double h1 = m_l1/m_gNE1;
1465 caltinay 3754 /*** GENERATOR SNIP_PDE_SINGLE_PRE TOP */
1466 caltinay 3748 const double w0 = -0.1555021169820365539*h1/h0;
1467     const double w1 = 0.041666666666666666667;
1468     const double w10 = -0.041666666666666666667*h0/h1;
1469     const double w11 = 0.1555021169820365539*h1/h0;
1470     const double w12 = 0.1555021169820365539*h0/h1;
1471     const double w13 = 0.01116454968463011277*h0/h1;
1472     const double w14 = 0.01116454968463011277*h1/h0;
1473     const double w15 = 0.041666666666666666667*h1/h0;
1474     const double w16 = -0.01116454968463011277*h0/h1;
1475     const double w17 = -0.1555021169820365539*h0/h1;
1476     const double w18 = -0.33333333333333333333*h1/h0;
1477     const double w19 = 0.25000000000000000000;
1478     const double w2 = -0.15550211698203655390;
1479     const double w20 = -0.25000000000000000000;
1480     const double w21 = 0.16666666666666666667*h0/h1;
1481     const double w22 = -0.16666666666666666667*h1/h0;
1482     const double w23 = -0.16666666666666666667*h0/h1;
1483     const double w24 = 0.33333333333333333333*h1/h0;
1484     const double w25 = 0.33333333333333333333*h0/h1;
1485     const double w26 = 0.16666666666666666667*h1/h0;
1486     const double w27 = -0.33333333333333333333*h0/h1;
1487     const double w28 = -0.032861463941450536761*h1;
1488     const double w29 = -0.032861463941450536761*h0;
1489     const double w3 = 0.041666666666666666667*h0/h1;
1490     const double w30 = -0.12264065304058601714*h1;
1491     const double w31 = -0.0023593469594139828636*h1;
1492     const double w32 = -0.008805202725216129906*h0;
1493     const double w33 = -0.008805202725216129906*h1;
1494     const double w34 = 0.032861463941450536761*h1;
1495     const double w35 = 0.008805202725216129906*h1;
1496     const double w36 = 0.008805202725216129906*h0;
1497     const double w37 = 0.0023593469594139828636*h1;
1498     const double w38 = 0.12264065304058601714*h1;
1499     const double w39 = 0.032861463941450536761*h0;
1500     const double w4 = 0.15550211698203655390;
1501     const double w40 = -0.12264065304058601714*h0;
1502     const double w41 = -0.0023593469594139828636*h0;
1503     const double w42 = 0.0023593469594139828636*h0;
1504     const double w43 = 0.12264065304058601714*h0;
1505     const double w44 = -0.16666666666666666667*h1;
1506     const double w45 = -0.083333333333333333333*h0;
1507     const double w46 = 0.083333333333333333333*h1;
1508     const double w47 = 0.16666666666666666667*h1;
1509     const double w48 = 0.083333333333333333333*h0;
1510     const double w49 = -0.16666666666666666667*h0;
1511     const double w5 = -0.041666666666666666667;
1512     const double w50 = 0.16666666666666666667*h0;
1513     const double w51 = -0.083333333333333333333*h1;
1514     const double w52 = 0.025917019497006092316*h0*h1;
1515     const double w53 = 0.0018607582807716854616*h0*h1;
1516     const double w54 = 0.0069444444444444444444*h0*h1;
1517     const double w55 = 0.09672363354357992482*h0*h1;
1518     const double w56 = 0.00049858867864229740201*h0*h1;
1519     const double w57 = 0.055555555555555555556*h0*h1;
1520     const double w58 = 0.027777777777777777778*h0*h1;
1521     const double w59 = 0.11111111111111111111*h0*h1;
1522     const double w6 = -0.01116454968463011277*h1/h0;
1523     const double w60 = -0.19716878364870322056*h1;
1524     const double w61 = -0.19716878364870322056*h0;
1525     const double w62 = -0.052831216351296779436*h0;
1526     const double w63 = -0.052831216351296779436*h1;
1527     const double w64 = 0.19716878364870322056*h1;
1528     const double w65 = 0.052831216351296779436*h1;
1529     const double w66 = 0.19716878364870322056*h0;
1530     const double w67 = 0.052831216351296779436*h0;
1531     const double w68 = -0.5*h1;
1532     const double w69 = -0.5*h0;
1533     const double w7 = 0.011164549684630112770;
1534     const double w70 = 0.5*h1;
1535     const double w71 = 0.5*h0;
1536     const double w72 = 0.1555021169820365539*h0*h1;
1537     const double w73 = 0.041666666666666666667*h0*h1;
1538     const double w74 = 0.01116454968463011277*h0*h1;
1539     const double w75 = 0.25*h0*h1;
1540     const double w8 = -0.011164549684630112770;
1541     const double w9 = -0.041666666666666666667*h1/h0;
1542     /* GENERATOR SNIP_PDE_SINGLE_PRE BOTTOM */
1543    
1544     rhs.requireWrite();
1545     #pragma omp parallel
1546     {
1547 caltinay 3754 for (index_t k1_0=0; k1_0<2; k1_0++) { // colouring
1548 caltinay 3748 #pragma omp for
1549     for (index_t k1=k1_0; k1<m_NE1; k1+=2) {
1550     for (index_t k0=0; k0<m_NE0; ++k0) {
1551     bool add_EM_S=false;
1552     bool add_EM_F=false;
1553     vector<double> EM_S(4*4, 0);
1554     vector<double> EM_F(4, 0);
1555     const index_t e = k0 + m_NE0*k1;
1556 caltinay 3754 /*** GENERATOR SNIP_PDE_SINGLE TOP */
1557 caltinay 3748 ///////////////
1558     // process A //
1559     ///////////////
1560     if (!A.isEmpty()) {
1561     add_EM_S=true;
1562     const double* A_p=const_cast<escript::Data*>(&A)->getSampleDataRO(e);
1563     if (A.actsExpanded()) {
1564     const register double A_00_0 = A_p[INDEX3(0,0,0,2,2)];
1565     const register double A_01_0 = A_p[INDEX3(0,1,0,2,2)];
1566     const register double A_10_0 = A_p[INDEX3(1,0,0,2,2)];
1567     const register double A_11_0 = A_p[INDEX3(1,1,0,2,2)];
1568     const register double A_00_1 = A_p[INDEX3(0,0,1,2,2)];
1569     const register double A_01_1 = A_p[INDEX3(0,1,1,2,2)];
1570     const register double A_10_1 = A_p[INDEX3(1,0,1,2,2)];
1571     const register double A_11_1 = A_p[INDEX3(1,1,1,2,2)];
1572     const register double A_00_2 = A_p[INDEX3(0,0,2,2,2)];
1573     const register double A_01_2 = A_p[INDEX3(0,1,2,2,2)];
1574     const register double A_10_2 = A_p[INDEX3(1,0,2,2,2)];
1575     const register double A_11_2 = A_p[INDEX3(1,1,2,2,2)];
1576     const register double A_00_3 = A_p[INDEX3(0,0,3,2,2)];
1577     const register double A_01_3 = A_p[INDEX3(0,1,3,2,2)];
1578     const register double A_10_3 = A_p[INDEX3(1,0,3,2,2)];
1579     const register double A_11_3 = A_p[INDEX3(1,1,3,2,2)];
1580     const register double tmp4_0 = A_10_1 + A_10_2;
1581     const register double tmp12_0 = A_11_0 + A_11_2;
1582     const register double tmp2_0 = A_11_0 + A_11_1 + A_11_2 + A_11_3;
1583     const register double tmp10_0 = A_01_3 + A_10_3;
1584     const register double tmp14_0 = A_01_0 + A_01_3 + A_10_0 + A_10_3;
1585     const register double tmp0_0 = A_01_0 + A_01_3;
1586     const register double tmp13_0 = A_01_2 + A_10_1;
1587     const register double tmp3_0 = A_00_2 + A_00_3;
1588     const register double tmp11_0 = A_11_1 + A_11_3;
1589     const register double tmp18_0 = A_01_1 + A_10_1;
1590     const register double tmp1_0 = A_00_0 + A_00_1;
1591     const register double tmp15_0 = A_01_1 + A_10_2;
1592     const register double tmp5_0 = A_00_0 + A_00_1 + A_00_2 + A_00_3;
1593     const register double tmp16_0 = A_10_0 + A_10_3;
1594     const register double tmp6_0 = A_01_3 + A_10_0;
1595     const register double tmp17_0 = A_01_1 + A_01_2;
1596     const register double tmp9_0 = A_01_0 + A_10_0;
1597     const register double tmp7_0 = A_01_0 + A_10_3;
1598     const register double tmp8_0 = A_01_1 + A_01_2 + A_10_1 + A_10_2;
1599     const register double tmp19_0 = A_01_2 + A_10_2;
1600     const register double tmp14_1 = A_10_0*w8;
1601     const register double tmp23_1 = tmp3_0*w14;
1602     const register double tmp35_1 = A_01_0*w8;
1603     const register double tmp54_1 = tmp13_0*w8;
1604     const register double tmp20_1 = tmp9_0*w4;
1605     const register double tmp25_1 = tmp12_0*w12;
1606     const register double tmp2_1 = A_01_1*w4;
1607     const register double tmp44_1 = tmp7_0*w7;
1608     const register double tmp26_1 = tmp10_0*w4;
1609     const register double tmp52_1 = tmp18_0*w8;
1610     const register double tmp48_1 = A_10_1*w7;
1611     const register double tmp46_1 = A_01_3*w8;
1612     const register double tmp50_1 = A_01_0*w2;
1613     const register double tmp8_1 = tmp4_0*w5;
1614     const register double tmp56_1 = tmp19_0*w8;
1615     const register double tmp9_1 = tmp2_0*w10;
1616     const register double tmp19_1 = A_10_3*w2;
1617     const register double tmp47_1 = A_10_2*w4;
1618     const register double tmp16_1 = tmp3_0*w0;
1619     const register double tmp18_1 = tmp1_0*w6;
1620     const register double tmp31_1 = tmp11_0*w12;
1621     const register double tmp55_1 = tmp15_0*w2;
1622     const register double tmp39_1 = A_10_2*w7;
1623     const register double tmp11_1 = tmp6_0*w7;
1624     const register double tmp40_1 = tmp11_0*w17;
1625     const register double tmp34_1 = tmp15_0*w8;
1626     const register double tmp33_1 = tmp14_0*w5;
1627     const register double tmp24_1 = tmp11_0*w13;
1628     const register double tmp3_1 = tmp1_0*w0;
1629     const register double tmp5_1 = tmp2_0*w3;
1630     const register double tmp43_1 = tmp17_0*w5;
1631     const register double tmp15_1 = A_01_2*w4;
1632     const register double tmp53_1 = tmp19_0*w2;
1633     const register double tmp27_1 = tmp3_0*w11;
1634     const register double tmp32_1 = tmp13_0*w2;
1635     const register double tmp10_1 = tmp5_0*w9;
1636     const register double tmp37_1 = A_10_1*w4;
1637     const register double tmp38_1 = tmp5_0*w15;
1638     const register double tmp17_1 = A_01_1*w7;
1639     const register double tmp12_1 = tmp7_0*w4;
1640     const register double tmp22_1 = tmp10_0*w7;
1641     const register double tmp57_1 = tmp18_0*w2;
1642     const register double tmp28_1 = tmp9_0*w7;
1643     const register double tmp29_1 = tmp1_0*w14;
1644     const register double tmp51_1 = tmp11_0*w16;
1645     const register double tmp42_1 = tmp12_0*w16;
1646     const register double tmp49_1 = tmp12_0*w17;
1647     const register double tmp21_1 = tmp1_0*w11;
1648     const register double tmp1_1 = tmp0_0*w1;
1649     const register double tmp45_1 = tmp6_0*w4;
1650     const register double tmp7_1 = A_10_0*w2;
1651     const register double tmp6_1 = tmp3_0*w6;
1652     const register double tmp13_1 = tmp8_0*w1;
1653     const register double tmp36_1 = tmp16_0*w1;
1654     const register double tmp41_1 = A_01_3*w2;
1655     const register double tmp30_1 = tmp12_0*w13;
1656     const register double tmp4_1 = A_01_2*w7;
1657     const register double tmp0_1 = A_10_3*w8;
1658     EM_S[INDEX2(0,1,4)]+=tmp0_1 + tmp1_1 + tmp2_1 + tmp3_1 + tmp4_1 + tmp5_1 + tmp6_1 + tmp7_1 + tmp8_1;
1659     EM_S[INDEX2(1,2,4)]+=tmp10_1 + tmp11_1 + tmp12_1 + tmp13_1 + tmp9_1;
1660     EM_S[INDEX2(3,2,4)]+=tmp14_1 + tmp15_1 + tmp16_1 + tmp17_1 + tmp18_1 + tmp19_1 + tmp1_1 + tmp5_1 + tmp8_1;
1661     EM_S[INDEX2(0,0,4)]+=tmp13_1 + tmp20_1 + tmp21_1 + tmp22_1 + tmp23_1 + tmp24_1 + tmp25_1;
1662     EM_S[INDEX2(3,3,4)]+=tmp13_1 + tmp26_1 + tmp27_1 + tmp28_1 + tmp29_1 + tmp30_1 + tmp31_1;
1663     EM_S[INDEX2(3,0,4)]+=tmp10_1 + tmp32_1 + tmp33_1 + tmp34_1 + tmp9_1;
1664     EM_S[INDEX2(3,1,4)]+=tmp35_1 + tmp36_1 + tmp37_1 + tmp38_1 + tmp39_1 + tmp40_1 + tmp41_1 + tmp42_1 + tmp43_1;
1665     EM_S[INDEX2(2,1,4)]+=tmp10_1 + tmp13_1 + tmp44_1 + tmp45_1 + tmp9_1;
1666     EM_S[INDEX2(0,2,4)]+=tmp36_1 + tmp38_1 + tmp43_1 + tmp46_1 + tmp47_1 + tmp48_1 + tmp49_1 + tmp50_1 + tmp51_1;
1667     EM_S[INDEX2(2,0,4)]+=tmp0_1 + tmp15_1 + tmp17_1 + tmp1_1 + tmp38_1 + tmp49_1 + tmp51_1 + tmp7_1 + tmp8_1;
1668     EM_S[INDEX2(1,3,4)]+=tmp14_1 + tmp19_1 + tmp1_1 + tmp2_1 + tmp38_1 + tmp40_1 + tmp42_1 + tmp4_1 + tmp8_1;
1669     EM_S[INDEX2(2,3,4)]+=tmp16_1 + tmp18_1 + tmp35_1 + tmp36_1 + tmp41_1 + tmp43_1 + tmp47_1 + tmp48_1 + tmp5_1;
1670     EM_S[INDEX2(2,2,4)]+=tmp24_1 + tmp25_1 + tmp27_1 + tmp29_1 + tmp33_1 + tmp52_1 + tmp53_1;
1671     EM_S[INDEX2(1,0,4)]+=tmp36_1 + tmp37_1 + tmp39_1 + tmp3_1 + tmp43_1 + tmp46_1 + tmp50_1 + tmp5_1 + tmp6_1;
1672     EM_S[INDEX2(0,3,4)]+=tmp10_1 + tmp33_1 + tmp54_1 + tmp55_1 + tmp9_1;
1673     EM_S[INDEX2(1,1,4)]+=tmp21_1 + tmp23_1 + tmp30_1 + tmp31_1 + tmp33_1 + tmp56_1 + tmp57_1;
1674     } else { /* constant data */
1675     const register double A_00 = A_p[INDEX2(0,0,2)];
1676     const register double A_01 = A_p[INDEX2(0,1,2)];
1677     const register double A_10 = A_p[INDEX2(1,0,2)];
1678     const register double A_11 = A_p[INDEX2(1,1,2)];
1679     const register double tmp0_0 = A_01 + A_10;
1680     const register double tmp0_1 = A_00*w18;
1681     const register double tmp10_1 = A_01*w20;
1682     const register double tmp12_1 = A_00*w26;
1683     const register double tmp4_1 = A_00*w22;
1684     const register double tmp8_1 = A_00*w24;
1685     const register double tmp13_1 = A_10*w19;
1686     const register double tmp9_1 = tmp0_0*w20;
1687     const register double tmp3_1 = A_11*w21;
1688     const register double tmp11_1 = A_11*w27;
1689     const register double tmp1_1 = A_01*w19;
1690     const register double tmp6_1 = A_11*w23;
1691     const register double tmp7_1 = A_11*w25;
1692     const register double tmp2_1 = A_10*w20;
1693     const register double tmp5_1 = tmp0_0*w19;
1694     EM_S[INDEX2(0,1,4)]+=tmp0_1 + tmp1_1 + tmp2_1 + tmp3_1;
1695     EM_S[INDEX2(1,2,4)]+=tmp4_1 + tmp5_1 + tmp6_1;
1696     EM_S[INDEX2(3,2,4)]+=tmp0_1 + tmp1_1 + tmp2_1 + tmp3_1;
1697     EM_S[INDEX2(0,0,4)]+=tmp5_1 + tmp7_1 + tmp8_1;
1698     EM_S[INDEX2(3,3,4)]+=tmp5_1 + tmp7_1 + tmp8_1;
1699     EM_S[INDEX2(3,0,4)]+=tmp4_1 + tmp6_1 + tmp9_1;
1700     EM_S[INDEX2(3,1,4)]+=tmp10_1 + tmp11_1 + tmp12_1 + tmp13_1;
1701     EM_S[INDEX2(2,1,4)]+=tmp4_1 + tmp5_1 + tmp6_1;
1702     EM_S[INDEX2(0,2,4)]+=tmp10_1 + tmp11_1 + tmp12_1 + tmp13_1;
1703     EM_S[INDEX2(2,0,4)]+=tmp11_1 + tmp12_1 + tmp1_1 + tmp2_1;
1704     EM_S[INDEX2(1,3,4)]+=tmp11_1 + tmp12_1 + tmp1_1 + tmp2_1;
1705     EM_S[INDEX2(2,3,4)]+=tmp0_1 + tmp10_1 + tmp13_1 + tmp3_1;
1706     EM_S[INDEX2(2,2,4)]+=tmp7_1 + tmp8_1 + tmp9_1;
1707     EM_S[INDEX2(1,0,4)]+=tmp0_1 + tmp10_1 + tmp13_1 + tmp3_1;
1708     EM_S[INDEX2(0,3,4)]+=tmp4_1 + tmp6_1 + tmp9_1;
1709     EM_S[INDEX2(1,1,4)]+=tmp7_1 + tmp8_1 + tmp9_1;
1710     }
1711     }
1712     ///////////////
1713     // process B //
1714     ///////////////
1715     if (!B.isEmpty()) {
1716     add_EM_S=true;
1717     const double* B_p=const_cast<escript::Data*>(&B)->getSampleDataRO(e);
1718     if (B.actsExpanded()) {
1719     const register double B_0_0 = B_p[INDEX2(0,0,2)];
1720     const register double B_1_0 = B_p[INDEX2(1,0,2)];
1721     const register double B_0_1 = B_p[INDEX2(0,1,2)];
1722     const register double B_1_1 = B_p[INDEX2(1,1,2)];
1723     const register double B_0_2 = B_p[INDEX2(0,2,2)];
1724     const register double B_1_2 = B_p[INDEX2(1,2,2)];
1725     const register double B_0_3 = B_p[INDEX2(0,3,2)];
1726     const register double B_1_3 = B_p[INDEX2(1,3,2)];
1727     const register double tmp3_0 = B_0_0 + B_0_2;
1728     const register double tmp1_0 = B_1_2 + B_1_3;
1729     const register double tmp2_0 = B_0_1 + B_0_3;
1730     const register double tmp0_0 = B_1_0 + B_1_1;
1731     const register double tmp63_1 = B_1_1*w42;
1732     const register double tmp79_1 = B_1_1*w40;
1733     const register double tmp37_1 = tmp3_0*w35;
1734     const register double tmp8_1 = tmp0_0*w32;
1735     const register double tmp71_1 = B_0_1*w34;
1736     const register double tmp19_1 = B_0_3*w31;
1737     const register double tmp15_1 = B_0_3*w34;
1738     const register double tmp9_1 = tmp3_0*w34;
1739     const register double tmp35_1 = B_1_0*w36;
1740     const register double tmp66_1 = B_0_3*w28;
1741     const register double tmp28_1 = B_1_0*w42;
1742     const register double tmp22_1 = B_1_0*w40;
1743     const register double tmp16_1 = B_1_2*w29;
1744     const register double tmp6_1 = tmp2_0*w35;
1745     const register double tmp55_1 = B_1_3*w40;
1746     const register double tmp50_1 = B_1_3*w42;
1747     const register double tmp7_1 = tmp1_0*w29;
1748     const register double tmp1_1 = tmp1_0*w32;
1749     const register double tmp57_1 = B_0_3*w30;
1750     const register double tmp18_1 = B_1_1*w32;
1751     const register double tmp53_1 = B_1_0*w41;
1752     const register double tmp61_1 = B_1_3*w36;
1753     const register double tmp27_1 = B_0_3*w38;
1754     const register double tmp64_1 = B_0_2*w30;
1755     const register double tmp76_1 = B_0_1*w38;
1756     const register double tmp39_1 = tmp2_0*w34;
1757     const register double tmp62_1 = B_0_1*w31;
1758     const register double tmp56_1 = B_0_0*w31;
1759     const register double tmp49_1 = B_1_1*w36;
1760     const register double tmp2_1 = B_0_2*w31;
1761     const register double tmp23_1 = B_0_2*w33;
1762     const register double tmp38_1 = B_1_1*w43;
1763     const register double tmp74_1 = B_1_2*w41;
1764     const register double tmp43_1 = B_1_1*w41;
1765     const register double tmp58_1 = B_0_2*w28;
1766     const register double tmp67_1 = B_0_0*w33;
1767     const register double tmp33_1 = tmp0_0*w39;
1768     const register double tmp4_1 = B_0_0*w28;
1769     const register double tmp20_1 = B_0_0*w30;
1770     const register double tmp13_1 = B_0_2*w38;
1771     const register double tmp65_1 = B_1_2*w43;
1772     const register double tmp0_1 = tmp0_0*w29;
1773     const register double tmp41_1 = tmp3_0*w33;
1774     const register double tmp73_1 = B_0_2*w37;
1775     const register double tmp69_1 = B_0_0*w38;
1776     const register double tmp48_1 = B_1_2*w39;
1777     const register double tmp59_1 = B_0_1*w33;
1778     const register double tmp17_1 = B_1_3*w41;
1779     const register double tmp5_1 = B_0_3*w33;
1780     const register double tmp3_1 = B_0_1*w30;
1781     const register double tmp21_1 = B_0_1*w28;
1782     const register double tmp42_1 = B_1_0*w29;
1783     const register double tmp54_1 = B_1_2*w32;
1784     const register double tmp60_1 = B_1_0*w39;
1785     const register double tmp32_1 = tmp1_0*w36;
1786     const register double tmp10_1 = B_0_1*w37;
1787     const register double tmp14_1 = B_0_0*w35;
1788     const register double tmp29_1 = B_0_1*w35;
1789     const register double tmp26_1 = B_1_2*w36;
1790     const register double tmp30_1 = B_1_3*w43;
1791     const register double tmp70_1 = B_0_2*w35;
1792     const register double tmp34_1 = B_1_3*w39;
1793     const register double tmp51_1 = B_1_0*w43;
1794     const register double tmp31_1 = B_0_2*w34;
1795     const register double tmp45_1 = tmp3_0*w28;
1796     const register double tmp11_1 = tmp1_0*w39;
1797     const register double tmp52_1 = B_1_1*w29;
1798     const register double tmp44_1 = B_1_3*w32;
1799     const register double tmp25_1 = B_1_1*w39;
1800     const register double tmp47_1 = tmp2_0*w33;
1801     const register double tmp72_1 = B_1_3*w29;
1802     const register double tmp40_1 = tmp2_0*w28;
1803     const register double tmp46_1 = B_1_2*w40;
1804     const register double tmp36_1 = B_1_2*w42;
1805     const register double tmp24_1 = B_0_0*w37;
1806     const register double tmp77_1 = B_0_3*w35;
1807     const register double tmp68_1 = B_0_3*w37;
1808     const register double tmp78_1 = B_0_0*w34;
1809     const register double tmp12_1 = tmp0_0*w36;
1810     const register double tmp75_1 = B_1_0*w32;
1811     EM_S[INDEX2(0,1,4)]+=tmp0_1 + tmp1_1 + tmp2_1 + tmp3_1 + tmp4_1 + tmp5_1;
1812     EM_S[INDEX2(1,2,4)]+=tmp6_1 + tmp7_1 + tmp8_1 + tmp9_1;
1813     EM_S[INDEX2(3,2,4)]+=tmp10_1 + tmp11_1 + tmp12_1 + tmp13_1 + tmp14_1 + tmp15_1;
1814     EM_S[INDEX2(0,0,4)]+=tmp16_1 + tmp17_1 + tmp18_1 + tmp19_1 + tmp20_1 + tmp21_1 + tmp22_1 + tmp23_1;
1815     EM_S[INDEX2(3,3,4)]+=tmp24_1 + tmp25_1 + tmp26_1 + tmp27_1 + tmp28_1 + tmp29_1 + tmp30_1 + tmp31_1;
1816     EM_S[INDEX2(3,0,4)]+=tmp32_1 + tmp33_1 + tmp6_1 + tmp9_1;
1817     EM_S[INDEX2(3,1,4)]+=tmp34_1 + tmp35_1 + tmp36_1 + tmp37_1 + tmp38_1 + tmp39_1;
1818     EM_S[INDEX2(2,1,4)]+=tmp32_1 + tmp33_1 + tmp40_1 + tmp41_1;
1819     EM_S[INDEX2(0,2,4)]+=tmp42_1 + tmp43_1 + tmp44_1 + tmp45_1 + tmp46_1 + tmp47_1;
1820     EM_S[INDEX2(2,0,4)]+=tmp45_1 + tmp47_1 + tmp48_1 + tmp49_1 + tmp50_1 + tmp51_1;
1821     EM_S[INDEX2(1,3,4)]+=tmp37_1 + tmp39_1 + tmp52_1 + tmp53_1 + tmp54_1 + tmp55_1;
1822     EM_S[INDEX2(2,3,4)]+=tmp11_1 + tmp12_1 + tmp56_1 + tmp57_1 + tmp58_1 + tmp59_1;
1823     EM_S[INDEX2(2,2,4)]+=tmp60_1 + tmp61_1 + tmp62_1 + tmp63_1 + tmp64_1 + tmp65_1 + tmp66_1 + tmp67_1;
1824     EM_S[INDEX2(1,0,4)]+=tmp0_1 + tmp1_1 + tmp68_1 + tmp69_1 + tmp70_1 + tmp71_1;
1825     EM_S[INDEX2(0,3,4)]+=tmp40_1 + tmp41_1 + tmp7_1 + tmp8_1;
1826     EM_S[INDEX2(1,1,4)]+=tmp72_1 + tmp73_1 + tmp74_1 + tmp75_1 + tmp76_1 + tmp77_1 + tmp78_1 + tmp79_1;
1827     } else { /* constant data */
1828     const register double B_0 = B_p[0];
1829     const register double B_1 = B_p[1];
1830     const register double tmp6_1 = B_1*w50;
1831     const register double tmp1_1 = B_1*w45;
1832     const register double tmp5_1 = B_1*w49;
1833     const register double tmp4_1 = B_1*w48;
1834     const register double tmp0_1 = B_0*w44;
1835     const register double tmp2_1 = B_0*w46;
1836     const register double tmp7_1 = B_0*w51;
1837     const register double tmp3_1 = B_0*w47;
1838     EM_S[INDEX2(0,1,4)]+=tmp0_1 + tmp1_1;
1839     EM_S[INDEX2(1,2,4)]+=tmp1_1 + tmp2_1;
1840     EM_S[INDEX2(3,2,4)]+=tmp3_1 + tmp4_1;
1841     EM_S[INDEX2(0,0,4)]+=tmp0_1 + tmp5_1;
1842     EM_S[INDEX2(3,3,4)]+=tmp3_1 + tmp6_1;
1843     EM_S[INDEX2(3,0,4)]+=tmp2_1 + tmp4_1;
1844     EM_S[INDEX2(3,1,4)]+=tmp2_1 + tmp6_1;
1845     EM_S