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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4529 - (show annotations)
Fri Oct 25 01:23:27 2013 UTC (6 years ago) by caltinay
File size: 202103 byte(s)
Ripley can now read grids in non-native byte order. This is in preparation for
Gocad Voxet import.

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