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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4286 - (show annotations)
Thu Mar 7 04:28:11 2013 UTC (6 years, 5 months ago) by caltinay
File size: 239005 byte(s)
Assorted spelling fixes.

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