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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4353 - (show annotations)
Fri Apr 5 00:14:35 2013 UTC (6 years, 6 months ago) by caltinay
File size: 243040 byte(s)
For some reason std::isnan<float> is not available on NCI so changed this
to ::isnan. Added options file for NCI vayu.

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