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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3943 - (show annotations)
Thu Aug 9 04:43:24 2012 UTC (7 years, 2 months ago) by caltinay
File size: 231736 byte(s)
Added heuristics for number of domain subdivisions to ripley so ripley domains
can now be created without specifying the values manually (but a warning will
be printed).

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