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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4495 - (show annotations)
Fri Jul 5 02:19:47 2013 UTC (6 years, 4 months ago) by caltinay
File size: 198456 byte(s)
Added support for more input data types in ER Mapper files.
Tests will follow soon.

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