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

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

Parent Directory Parent Directory | Revision Log Revision Log


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