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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4618 - (show annotations)
Wed Jan 15 04:35:19 2014 UTC (5 years, 10 months ago) by caltinay
File size: 210108 byte(s)
Implemented reverse reading of grid data from netcdf files.
Fixes #44.

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