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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4526 - (show annotations)
Mon Sep 2 06:34:25 2013 UTC (6 years, 1 month ago) by jfenwick
File size: 201966 byte(s)
Single rank rectangle randoms.

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