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

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

Parent Directory Parent Directory | Revision Log Revision Log


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