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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3785 - (show annotations)
Wed Jan 25 04:00:33 2012 UTC (7 years, 8 months ago) by caltinay
Original Path: branches/ripleygmg_from_3668/ripley/src/Rectangle.cpp
File size: 230696 byte(s)
Fixed early deallocation of matrix pattern.

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