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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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