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

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

Parent Directory Parent Directory | Revision Log Revision Log


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