/[escript]/branches/ripleygmg_from_3668/ripley/src/Rectangle.cpp
ViewVC logotype

Contents of /branches/ripleygmg_from_3668/ripley/src/Rectangle.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3704 - (show annotations)
Mon Dec 5 01:59:08 2011 UTC (8 years ago) by caltinay
File size: 35855 byte(s)
Interpolation on faces.

1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2011 by University of Queensland
5 * Earth Systems Science Computational Center (ESSCC)
6 * http://www.uq.edu.au/esscc
7 *
8 * Primary Business: Queensland, Australia
9 * Licensed under the Open Software License version 3.0
10 * http://www.opensource.org/licenses/osl-3.0.php
11 *
12 *******************************************************/
13
14 #include <ripley/Rectangle.h>
15 extern "C" {
16 #include "paso/SystemMatrixPattern.h"
17 }
18
19 #if USE_SILO
20 #include <silo.h>
21 #ifdef ESYS_MPI
22 #include <pmpio.h>
23 #endif
24 #endif
25
26 #include <iomanip>
27
28 using namespace std;
29
30 namespace ripley {
31
32 Rectangle::Rectangle(int n0, int n1, double l0, double l1, int d0, int d1) :
33 RipleyDomain(2),
34 m_gNE0(n0),
35 m_gNE1(n1),
36 m_l0(l0),
37 m_l1(l1),
38 m_NX(d0),
39 m_NY(d1)
40 {
41 // ensure number of subdivisions is valid and nodes can be distributed
42 // among number of ranks
43 if (m_NX*m_NY != m_mpiInfo->size)
44 throw RipleyException("Invalid number of spatial subdivisions");
45
46 if (n0%m_NX > 0 || n1%m_NY > 0)
47 throw RipleyException("Number of elements must be separable into number of ranks in each dimension");
48
49 // local number of elements
50 m_NE0 = n0/m_NX;
51 m_NE1 = n1/m_NY;
52 // local number of nodes (not necessarily owned)
53 m_N0 = m_NE0+1;
54 m_N1 = m_NE1+1;
55 // bottom-left node is at (offset0,offset1) in global mesh
56 m_offset0 = m_NE0*(m_mpiInfo->rank%m_NX);
57 m_offset1 = m_NE1*(m_mpiInfo->rank/m_NX);
58 populateSampleIds();
59 }
60
61 Rectangle::~Rectangle()
62 {
63 }
64
65 string Rectangle::getDescription() const
66 {
67 return "ripley::Rectangle";
68 }
69
70 bool Rectangle::operator==(const AbstractDomain& other) const
71 {
72 if (dynamic_cast<const Rectangle*>(&other))
73 return this==&other;
74
75 return false;
76 }
77
78 void Rectangle::dump(const string& fileName) const
79 {
80 #if USE_SILO
81 string fn(fileName);
82 if (fileName.length() < 6 || fileName.compare(fileName.length()-5, 5, ".silo") != 0) {
83 fn+=".silo";
84 }
85
86 const int NUM_SILO_FILES = 1;
87 const char* blockDirFmt = "/block%04d";
88 int driver=DB_HDF5;
89 string siloPath;
90 DBfile* dbfile = NULL;
91
92 #ifdef ESYS_MPI
93 PMPIO_baton_t* baton = NULL;
94 #endif
95
96 if (m_mpiInfo->size > 1) {
97 #ifdef ESYS_MPI
98 baton = PMPIO_Init(NUM_SILO_FILES, PMPIO_WRITE, m_mpiInfo->comm,
99 0x1337, PMPIO_DefaultCreate, PMPIO_DefaultOpen,
100 PMPIO_DefaultClose, (void*)&driver);
101 // try the fallback driver in case of error
102 if (!baton && driver != DB_PDB) {
103 driver = DB_PDB;
104 baton = PMPIO_Init(NUM_SILO_FILES, PMPIO_WRITE, m_mpiInfo->comm,
105 0x1338, PMPIO_DefaultCreate, PMPIO_DefaultOpen,
106 PMPIO_DefaultClose, (void*)&driver);
107 }
108 if (baton) {
109 char str[64];
110 snprintf(str, 64, blockDirFmt, PMPIO_RankInGroup(baton, m_mpiInfo->rank));
111 siloPath = str;
112 dbfile = (DBfile*) PMPIO_WaitForBaton(baton, fn.c_str(), siloPath.c_str());
113 }
114 #endif
115 } else {
116 dbfile = DBCreate(fn.c_str(), DB_CLOBBER, DB_LOCAL,
117 getDescription().c_str(), driver);
118 // try the fallback driver in case of error
119 if (!dbfile && driver != DB_PDB) {
120 driver = DB_PDB;
121 dbfile = DBCreate(fn.c_str(), DB_CLOBBER, DB_LOCAL,
122 getDescription().c_str(), driver);
123 }
124 }
125
126 if (!dbfile)
127 throw RipleyException("dump: Could not create Silo file");
128
129 /*
130 if (driver==DB_HDF5) {
131 // gzip level 1 already provides good compression with minimal
132 // performance penalty. Some tests showed that gzip levels >3 performed
133 // rather badly on escript data both in terms of time and space
134 DBSetCompression("ERRMODE=FALLBACK METHOD=GZIP LEVEL=1");
135 }
136 */
137
138 boost::scoped_ptr<double> x(new double[m_N0]);
139 boost::scoped_ptr<double> y(new double[m_N1]);
140 double* coords[2] = { x.get(), y.get() };
141 pair<double,double> xdx = getFirstCoordAndSpacing(0);
142 pair<double,double> ydy = getFirstCoordAndSpacing(1);
143 #pragma omp parallel
144 {
145 #pragma omp for
146 for (dim_t i0 = 0; i0 < m_N0; i0++) {
147 coords[0][i0]=xdx.first+i0*xdx.second;
148 }
149 #pragma omp for
150 for (dim_t i1 = 0; i1 < m_N1; i1++) {
151 coords[1][i1]=ydy.first+i1*ydy.second;
152 }
153 }
154 IndexVector dims = getNumNodesPerDim();
155
156 // write mesh
157 DBPutQuadmesh(dbfile, "mesh", NULL, coords, &dims[0], 2, DB_DOUBLE,
158 DB_COLLINEAR, NULL);
159
160 // write node ids
161 DBPutQuadvar1(dbfile, "nodeId", "mesh", (void*)&m_nodeId[0], &dims[0], 2,
162 NULL, 0, DB_INT, DB_NODECENT, NULL);
163
164 // write element ids
165 dims = getNumElementsPerDim();
166 DBPutQuadvar1(dbfile, "elementId", "mesh", (void*)&m_elementId[0],
167 &dims[0], 2, NULL, 0, DB_INT, DB_ZONECENT, NULL);
168
169 // rank 0 writes multimesh and multivar
170 if (m_mpiInfo->rank == 0) {
171 vector<string> tempstrings;
172 vector<char*> names;
173 for (dim_t i=0; i<m_mpiInfo->size; i++) {
174 stringstream path;
175 path << "/block" << setw(4) << setfill('0') << right << i << "/mesh";
176 tempstrings.push_back(path.str());
177 names.push_back((char*)tempstrings.back().c_str());
178 }
179 vector<int> types(m_mpiInfo->size, DB_QUAD_RECT);
180 DBSetDir(dbfile, "/");
181 DBPutMultimesh(dbfile, "multimesh", m_mpiInfo->size, &names[0],
182 &types[0], NULL);
183 tempstrings.clear();
184 names.clear();
185 for (dim_t i=0; i<m_mpiInfo->size; i++) {
186 stringstream path;
187 path << "/block" << setw(4) << setfill('0') << right << i << "/nodeId";
188 tempstrings.push_back(path.str());
189 names.push_back((char*)tempstrings.back().c_str());
190 }
191 types.assign(m_mpiInfo->size, DB_QUADVAR);
192 DBPutMultivar(dbfile, "nodeId", m_mpiInfo->size, &names[0],
193 &types[0], NULL);
194 tempstrings.clear();
195 names.clear();
196 for (dim_t i=0; i<m_mpiInfo->size; i++) {
197 stringstream path;
198 path << "/block" << setw(4) << setfill('0') << right << i << "/elementId";
199 tempstrings.push_back(path.str());
200 names.push_back((char*)tempstrings.back().c_str());
201 }
202 DBPutMultivar(dbfile, "elementId", m_mpiInfo->size, &names[0],
203 &types[0], NULL);
204 }
205
206 if (m_mpiInfo->size > 1) {
207 #ifdef ESYS_MPI
208 PMPIO_HandOffBaton(baton, dbfile);
209 PMPIO_Finish(baton);
210 #endif
211 } else {
212 DBClose(dbfile);
213 }
214
215 #else // USE_SILO
216 throw RipleyException("dump(): no Silo support");
217 #endif
218 }
219
220 const int* Rectangle::borrowSampleReferenceIDs(int fsType) const
221 {
222 switch (fsType) {
223 case Nodes:
224 return &m_nodeId[0];
225 case Elements:
226 return &m_elementId[0];
227 case FaceElements:
228 return &m_faceId[0];
229 default:
230 break;
231 }
232
233 stringstream msg;
234 msg << "borrowSampleReferenceIDs() not implemented for function space type "
235 << functionSpaceTypeAsString(fsType);
236 throw RipleyException(msg.str());
237 }
238
239 bool Rectangle::ownSample(int fsCode, index_t id) const
240 {
241 #ifdef ESYS_MPI
242 if (fsCode != ReducedNodes) {
243 const index_t myFirst=m_nodeDistribution[m_mpiInfo->rank];
244 const index_t myLast=m_nodeDistribution[m_mpiInfo->rank+1]-1;
245 return (m_nodeId[id]>=myFirst && m_nodeId[id]<=myLast);
246 } else {
247 stringstream msg;
248 msg << "ownSample() not implemented for "
249 << functionSpaceTypeAsString(fsCode);
250 throw RipleyException(msg.str());
251 }
252 #else
253 return true;
254 #endif
255 }
256
257 void Rectangle::setToGradient(escript::Data& out, const escript::Data& cIn) const
258 {
259 escript::Data& in = *const_cast<escript::Data*>(&cIn);
260 const dim_t numComp = in.getDataPointSize();
261 const double h0 = m_l0/m_gNE0;
262 const double h1 = m_l1/m_gNE1;
263 if (out.getFunctionSpace().getTypeCode() == Elements) {
264 /* GENERATOR SNIP_GRAD_ELEMENTS TOP */
265 const double tmp0_13 = 0.78867513459481288225/h1;
266 const double tmp0_0 = 0.78867513459481288225/h0;
267 const double tmp0_4 = 0.21132486540518711775/h0;
268 const double tmp0_10 = 0.78867513459481288225/h1;
269 const double tmp0_1 = 0.21132486540518711775/h0;
270 const double tmp0_8 = -0.21132486540518711775/h1;
271 const double tmp0_14 = 0.21132486540518711775/h1;
272 const double tmp0_5 = 0.78867513459481288225/h0;
273 const double tmp0_11 = -0.78867513459481288225/h1;
274 const double tmp0_2 = -0.21132486540518711775/h0;
275 const double tmp0_9 = 0.21132486540518711775/h1;
276 const double tmp0_15 = -0.21132486540518711775/h1;
277 const double tmp0_6 = -0.78867513459481288225/h0;
278 const double tmp0_3 = -0.78867513459481288225/h0;
279 const double tmp0_12 = -0.78867513459481288225/h1;
280 const double tmp0_7 = -0.21132486540518711775/h0;
281 #pragma omp parallel for
282 for (index_t k1 =0; k1 < m_NE1; ++k1) {
283 for (index_t k0 =0; k0 < m_NE0; ++k0) {
284 const register double* f_10 = in.getSampleDataRO(INDEX2(k0+1,k1, m_N0));
285 const register double* f_11 = in.getSampleDataRO(INDEX2(k0+1,k1+1, m_N0));
286 const register double* f_01 = in.getSampleDataRO(INDEX2(k0,k1+1, m_N0));
287 const register double* f_00 = in.getSampleDataRO(INDEX2(k0,k1, m_N0));
288 double* o = out.getSampleDataRW(INDEX2(k0,k1,m_NE0));
289 for (index_t i=0; i < numComp; ++i) {
290 o[INDEX3(i,0,0,numComp,2)] = f_00[i]*tmp0_3 + f_01[i]*tmp0_2 + f_10[i]*tmp0_0 + f_11[i]*tmp0_1;
291 o[INDEX3(i,1,0,numComp,2)] = f_00[i]*tmp0_11 + f_01[i]*tmp0_10 + f_10[i]*tmp0_8 + f_11[i]*tmp0_9;
292 o[INDEX3(i,0,1,numComp,2)] = f_00[i]*tmp0_3 + f_01[i]*tmp0_2 + f_10[i]*tmp0_0 + f_11[i]*tmp0_1;
293 o[INDEX3(i,1,1,numComp,2)] = f_00[i]*tmp0_15 + f_01[i]*tmp0_14 + f_10[i]*tmp0_12 + f_11[i]*tmp0_13;
294 o[INDEX3(i,0,2,numComp,2)] = f_00[i]*tmp0_7 + f_01[i]*tmp0_6 + f_10[i]*tmp0_4 + f_11[i]*tmp0_5;
295 o[INDEX3(i,1,2,numComp,2)] = f_00[i]*tmp0_11 + f_01[i]*tmp0_10 + f_10[i]*tmp0_8 + f_11[i]*tmp0_9;
296 o[INDEX3(i,0,3,numComp,2)] = f_00[i]*tmp0_7 + f_01[i]*tmp0_6 + f_10[i]*tmp0_4 + f_11[i]*tmp0_5;
297 o[INDEX3(i,1,3,numComp,2)] = f_00[i]*tmp0_15 + f_01[i]*tmp0_14 + f_10[i]*tmp0_12 + f_11[i]*tmp0_13;
298 } /* end of component loop i */
299 } /* end of k0 loop */
300 } /* end of k1 loop */
301 /* GENERATOR SNIP_GRAD_ELEMENTS BOTTOM */
302 } else {
303 throw RipleyException("setToGradient() not implemented");
304 }
305 }
306
307 void Rectangle::addPDEToSystem(escript::AbstractSystemMatrix& mat,
308 escript::Data& rhs, const escript::Data& A, const escript::Data& B,
309 const escript::Data& C, const escript::Data& D,
310 const escript::Data& X, const escript::Data& Y,
311 const escript::Data& d, const escript::Data& y,
312 const escript::Data& d_contact, const escript::Data& y_contact,
313 const escript::Data& d_dirac, const escript::Data& y_dirac) const
314 {
315 throw RipleyException("addPDEToSystem() not implemented");
316 }
317
318 Paso_SystemMatrixPattern* Rectangle::getPattern(bool reducedRowOrder,
319 bool reducedColOrder) const
320 {
321 if (reducedRowOrder || reducedColOrder)
322 throw RipleyException("getPattern() not implemented for reduced order");
323
324 // connector
325 RankVector neighbour;
326 IndexVector offsetInShared(1,0);
327 IndexVector sendShared, recvShared;
328 const IndexVector faces=getNumFacesPerBoundary();
329 const index_t left = (m_offset0==0 ? 0 : 1);
330 const index_t bottom = (m_offset1==0 ? 0 : 1);
331 // corner node from bottom-left
332 if (faces[0] == 0 && faces[2] == 0) {
333 neighbour.push_back(m_mpiInfo->rank-m_NX-1);
334 offsetInShared.push_back(offsetInShared.back()+1);
335 sendShared.push_back(m_nodeId[m_N0+left]);
336 recvShared.push_back(m_nodeId[0]);
337 }
338 // bottom edge
339 if (faces[2] == 0) {
340 neighbour.push_back(m_mpiInfo->rank-m_NX);
341 offsetInShared.push_back(offsetInShared.back()+m_N0-left);
342 for (dim_t i=left; i<m_N0; i++) {
343 // easy case, we know the neighbour id's
344 sendShared.push_back(m_nodeId[m_N0+i]);
345 recvShared.push_back(m_nodeId[i]);
346 }
347 }
348 // corner node from bottom-right
349 if (faces[1] == 0 && faces[2] == 0) {
350 neighbour.push_back(m_mpiInfo->rank-m_NX+1);
351 const index_t N0=(neighbour.back()%m_NX == 0 ? m_N0 : m_N0-1);
352 const index_t N1=(neighbour.back()/m_NX == 0 ? m_N1 : m_N1-1);
353 const index_t first=m_nodeDistribution[neighbour.back()];
354 offsetInShared.push_back(offsetInShared.back()+1);
355 sendShared.push_back(m_nodeId[(bottom+1)*m_N0-1]);
356 recvShared.push_back(first+N0*(N1-1));
357 }
358 // left edge
359 if (faces[0] == 0) {
360 neighbour.push_back(m_mpiInfo->rank-1);
361 offsetInShared.push_back(offsetInShared.back()+m_N1-bottom);
362 for (dim_t i=bottom; i<m_N1; i++) {
363 // easy case, we know the neighbour id's
364 sendShared.push_back(m_nodeId[i*m_N0+1]);
365 recvShared.push_back(m_nodeId[i*m_N0]);
366 }
367 }
368 // right edge
369 if (faces[1] == 0) {
370 neighbour.push_back(m_mpiInfo->rank+1);
371 const index_t rightN0=(neighbour.back()%m_NX == 0 ? m_N0 : m_N0-1);
372 const index_t first=m_nodeDistribution[neighbour.back()];
373 offsetInShared.push_back(offsetInShared.back()+m_N1-bottom);
374 for (dim_t i=bottom; i<m_N1; i++) {
375 sendShared.push_back(m_nodeId[(i+1)*m_N0-1]);
376 recvShared.push_back(first+rightN0*(i-bottom));
377 }
378 }
379 // corner node from top-left
380 if (faces[0] == 0 && faces[3] == 0) {
381 neighbour.push_back(m_mpiInfo->rank+m_NX-1);
382 const index_t N0=(neighbour.back()%m_NX == 0 ? m_N0 : m_N0-1);
383 const index_t first=m_nodeDistribution[neighbour.back()];
384 offsetInShared.push_back(offsetInShared.back()+1);
385 sendShared.push_back(m_nodeId[m_N0*(m_N1-1)+left]);
386 recvShared.push_back(first+N0-1);
387 }
388 // top edge
389 if (faces[3] == 0) {
390 neighbour.push_back(m_mpiInfo->rank+m_NX);
391 const index_t first=m_nodeDistribution[neighbour.back()];
392 offsetInShared.push_back(offsetInShared.back()+m_N0-left);
393 for (dim_t i=left; i<m_N0; i++) {
394 sendShared.push_back(m_nodeId[m_N0*(m_N1-1)+i]);
395 recvShared.push_back(first+i-left);
396 }
397 }
398 // corner node from top-right
399 if (faces[1] == 0 && faces[3] == 0) {
400 neighbour.push_back(m_mpiInfo->rank+m_NX+1);
401 const index_t first=m_nodeDistribution[neighbour.back()];
402 offsetInShared.push_back(offsetInShared.back()+1);
403 sendShared.push_back(m_nodeId[m_N0*m_N1-1]);
404 recvShared.push_back(first);
405 }
406 const int numDOF=m_nodeDistribution[m_mpiInfo->rank+1]-m_nodeDistribution[m_mpiInfo->rank];
407 /*
408 cout << "--- rcv_shcomp ---" << endl;
409 cout << "numDOF=" << numDOF << ", numNeighbors=" << neighbour.size() << endl;
410 for (size_t i=0; i<neighbour.size(); i++) {
411 cout << "neighbor[" << i << "]=" << neighbour[i]
412 << " offsetInShared[" << i+1 << "]=" << offsetInShared[i+1] << endl;
413 }
414 for (size_t i=0; i<recvShared.size(); i++) {
415 cout << "shared[" << i << "]=" << recvShared[i] << endl;
416 }
417 cout << "--- snd_shcomp ---" << endl;
418 for (size_t i=0; i<sendShared.size(); i++) {
419 cout << "shared[" << i << "]=" << sendShared[i] << endl;
420 }
421 */
422
423 Paso_SharedComponents *snd_shcomp = Paso_SharedComponents_alloc(
424 numDOF, neighbour.size(), &neighbour[0], &sendShared[0],
425 &offsetInShared[0], 1, 0, m_mpiInfo);
426 Paso_SharedComponents *rcv_shcomp = Paso_SharedComponents_alloc(
427 numDOF, neighbour.size(), &neighbour[0], &recvShared[0],
428 &offsetInShared[0], 1, 0, m_mpiInfo);
429 Paso_Connector* connector = Paso_Connector_alloc(snd_shcomp, rcv_shcomp);
430 Paso_SharedComponents_free(snd_shcomp);
431 Paso_SharedComponents_free(rcv_shcomp);
432
433 // create patterns
434 dim_t M, N;
435 IndexVector ptr(1,0);
436 IndexVector index;
437
438 // main pattern
439 for (index_t i=0; i<numDOF; i++) {
440 // always add the node itself
441 index.push_back(i);
442 int num=insertNeighbours(index, i);
443 ptr.push_back(ptr.back()+num+1);
444 }
445 M=N=ptr.size()-1;
446 // paso will manage the memory
447 index_t* indexC = MEMALLOC(index.size(),index_t);
448 index_t* ptrC = MEMALLOC(ptr.size(), index_t);
449 copy(index.begin(), index.end(), indexC);
450 copy(ptr.begin(), ptr.end(), ptrC);
451 Paso_Pattern *mainPattern = Paso_Pattern_alloc(MATRIX_FORMAT_DEFAULT,
452 M, N, ptrC, indexC);
453
454 /*
455 cout << "--- main_pattern ---" << endl;
456 cout << "M=" << M << ", N=" << N << endl;
457 for (size_t i=0; i<ptr.size(); i++) {
458 cout << "ptr[" << i << "]=" << ptr[i] << endl;
459 }
460 for (size_t i=0; i<index.size(); i++) {
461 cout << "index[" << i << "]=" << index[i] << endl;
462 }
463 */
464
465 ptr.clear();
466 index.clear();
467
468 // column & row couple patterns
469 Paso_Pattern *colCouplePattern, *rowCouplePattern;
470 generateCouplePatterns(&colCouplePattern, &rowCouplePattern);
471
472 // allocate paso distribution
473 Paso_Distribution* distribution = Paso_Distribution_alloc(m_mpiInfo,
474 const_cast<index_t*>(&m_nodeDistribution[0]), 1, 0);
475
476 Paso_SystemMatrixPattern* pattern = Paso_SystemMatrixPattern_alloc(
477 MATRIX_FORMAT_DEFAULT, distribution, distribution,
478 mainPattern, colCouplePattern, rowCouplePattern,
479 connector, connector);
480 Paso_Pattern_free(mainPattern);
481 Paso_Pattern_free(colCouplePattern);
482 Paso_Pattern_free(rowCouplePattern);
483 Paso_Distribution_free(distribution);
484 return pattern;
485 }
486
487 void Rectangle::Print_Mesh_Info(const bool full) const
488 {
489 RipleyDomain::Print_Mesh_Info(full);
490 if (full) {
491 cout << " Id Coordinates" << endl;
492 cout.precision(15);
493 cout.setf(ios::scientific, ios::floatfield);
494 pair<double,double> xdx = getFirstCoordAndSpacing(0);
495 pair<double,double> ydy = getFirstCoordAndSpacing(1);
496 for (index_t i=0; i < getNumNodes(); i++) {
497 cout << " " << setw(5) << m_nodeId[i]
498 << " " << xdx.first+(i%m_N0)*xdx.second
499 << " " << ydy.first+(i/m_N0)*ydy.second << endl;
500 }
501 }
502 }
503
504 IndexVector Rectangle::getNumNodesPerDim() const
505 {
506 IndexVector ret;
507 ret.push_back(m_N0);
508 ret.push_back(m_N1);
509 return ret;
510 }
511
512 IndexVector Rectangle::getNumElementsPerDim() const
513 {
514 IndexVector ret;
515 ret.push_back(m_NE0);
516 ret.push_back(m_NE1);
517 return ret;
518 }
519
520 IndexVector Rectangle::getNumFacesPerBoundary() const
521 {
522 IndexVector ret(4, 0);
523 //left
524 if (m_offset0==0)
525 ret[0]=m_NE1;
526 //right
527 if (m_mpiInfo->rank%m_NX==m_NX-1)
528 ret[1]=m_NE1;
529 //bottom
530 if (m_offset1==0)
531 ret[2]=m_NE0;
532 //top
533 if (m_mpiInfo->rank/m_NX==m_NY-1)
534 ret[3]=m_NE0;
535 return ret;
536 }
537
538 pair<double,double> Rectangle::getFirstCoordAndSpacing(dim_t dim) const
539 {
540 if (dim==0) {
541 return pair<double,double>((m_l0*m_offset0)/m_gNE0, m_l0/m_gNE0);
542 } else if (dim==1) {
543 return pair<double,double>((m_l1*m_offset1)/m_gNE1, m_l1/m_gNE1);
544 }
545 throw RipleyException("getFirstCoordAndSpacing(): invalid argument");
546 }
547
548 //protected
549 dim_t Rectangle::getNumFaceElements() const
550 {
551 const IndexVector faces = getNumFacesPerBoundary();
552 dim_t n=0;
553 for (size_t i=0; i<faces.size(); i++)
554 n+=faces[i];
555 return n;
556 }
557
558 //protected
559 void Rectangle::assembleCoordinates(escript::Data& arg) const
560 {
561 escriptDataC x = arg.getDataC();
562 int numDim = m_numDim;
563 if (!isDataPointShapeEqual(&x, 1, &numDim))
564 throw RipleyException("setToX: Invalid Data object shape");
565 if (!numSamplesEqual(&x, 1, getNumNodes()))
566 throw RipleyException("setToX: Illegal number of samples in Data object");
567
568 pair<double,double> xdx = getFirstCoordAndSpacing(0);
569 pair<double,double> ydy = getFirstCoordAndSpacing(1);
570 arg.requireWrite();
571 #pragma omp parallel for
572 for (dim_t i1 = 0; i1 < m_N1; i1++) {
573 for (dim_t i0 = 0; i0 < m_N0; i0++) {
574 double* point = arg.getSampleDataRW(i0+m_N0*i1);
575 point[0] = xdx.first+i0*xdx.second;
576 point[1] = ydy.first+i1*ydy.second;
577 }
578 }
579 }
580
581 //private
582 void Rectangle::populateSampleIds()
583 {
584 // identifiers are ordered from left to right, bottom to top on each rank,
585 // except for the shared nodes which are owned by the rank below / to the
586 // left of the current rank
587
588 // build node distribution vector first.
589 // m_nodeDistribution[i] is the first node id on rank i, that is
590 // rank i owns m_nodeDistribution[i+1]-nodeDistribution[i] nodes
591 m_nodeDistribution.assign(m_mpiInfo->size+1, 0);
592 m_nodeDistribution[1]=getNumNodes();
593 for (dim_t k=1; k<m_mpiInfo->size-1; k++) {
594 const index_t x=k%m_NX;
595 const index_t y=k/m_NX;
596 index_t numNodes=getNumNodes();
597 if (x>0)
598 numNodes-=m_N1;
599 if (y>0)
600 numNodes-=m_N0;
601 if (x>0 && y>0)
602 numNodes++; // subtracted corner twice -> fix that
603 m_nodeDistribution[k+1]=m_nodeDistribution[k]+numNodes;
604 }
605 m_nodeDistribution[m_mpiInfo->size]=getNumDataPointsGlobal();
606
607 m_nodeId.resize(getNumNodes());
608
609 // the bottom row and left column are not owned by this rank so the
610 // identifiers need to be computed accordingly
611 const index_t left = (m_offset0==0 ? 0 : 1);
612 const index_t bottom = (m_offset1==0 ? 0 : 1);
613 if (left>0) {
614 const int neighbour=m_mpiInfo->rank-1;
615 const index_t leftN0=(neighbour%m_NX == 0 ? m_N0 : m_N0-1);
616 #pragma omp parallel for
617 for (dim_t i1=bottom; i1<m_N1; i1++) {
618 m_nodeId[i1*m_N0]=m_nodeDistribution[neighbour]
619 + (i1-bottom+1)*leftN0-1;
620 }
621 }
622 if (bottom>0) {
623 const int neighbour=m_mpiInfo->rank-m_NX;
624 const index_t bottomN0=(neighbour%m_NX == 0 ? m_N0 : m_N0-1);
625 const index_t bottomN1=(neighbour/m_NX == 0 ? m_N1 : m_N1-1);
626 #pragma omp parallel for
627 for (dim_t i0=left; i0<m_N0; i0++) {
628 m_nodeId[i0]=m_nodeDistribution[neighbour]
629 + (bottomN1-1)*bottomN0 + i0 - left;
630 }
631 }
632 if (left>0 && bottom>0) {
633 const int neighbour=m_mpiInfo->rank-m_NX-1;
634 const index_t N0=(neighbour%m_NX == 0 ? m_N0 : m_N0-1);
635 const index_t N1=(neighbour/m_NX == 0 ? m_N1 : m_N1-1);
636 m_nodeId[0]=m_nodeDistribution[neighbour]+N0*N1-1;
637 }
638
639 // the rest of the id's are contiguous
640 const index_t firstId=m_nodeDistribution[m_mpiInfo->rank];
641 #pragma omp parallel for
642 for (dim_t i1=bottom; i1<m_N1; i1++) {
643 for (dim_t i0=left; i0<m_N0; i0++) {
644 m_nodeId[i0+i1*m_N0] = firstId+i0-left+(i1-bottom)*(m_N0-left);
645 }
646 }
647
648 // element id's
649 m_elementId.resize(getNumElements());
650 #pragma omp parallel for
651 for (dim_t k=0; k<getNumElements(); k++) {
652 m_elementId[k]=k;
653 }
654
655 // face element id's
656 m_faceId.resize(getNumFaceElements());
657 #pragma omp parallel for
658 for (dim_t k=0; k<getNumFaceElements(); k++) {
659 m_faceId[k]=k;
660 }
661
662 // generate face offset vector
663 const IndexVector facesPerEdge = getNumFacesPerBoundary();
664 m_faceOffset.assign(facesPerEdge.size(), -1);
665 index_t offset=0;
666 for (size_t i=0; i<facesPerEdge.size(); i++) {
667 if (facesPerEdge[i]>0) {
668 m_faceOffset[i]=offset;
669 offset+=facesPerEdge[i];
670 }
671 }
672 }
673
674 //private
675 int Rectangle::insertNeighbours(IndexVector& index, index_t node) const
676 {
677 const dim_t myN0 = (m_offset0==0 ? m_N0 : m_N0-1);
678 const dim_t myN1 = (m_offset1==0 ? m_N1 : m_N1-1);
679 const int x=node%myN0;
680 const int y=node/myN0;
681 int num=0;
682 if (y>0) {
683 if (x>0) {
684 // bottom-left
685 index.push_back(node-myN0-1);
686 num++;
687 }
688 // bottom
689 index.push_back(node-myN0);
690 num++;
691 if (x<myN0-1) {
692 // bottom-right
693 index.push_back(node-myN0+1);
694 num++;
695 }
696 }
697 if (x<myN0-1) {
698 // right
699 index.push_back(node+1);
700 num++;
701 if (y<myN1-1) {
702 // top-right
703 index.push_back(node+myN0+1);
704 num++;
705 }
706 }
707 if (y<myN1-1) {
708 // top
709 index.push_back(node+myN0);
710 num++;
711 if (x>0) {
712 // top-left
713 index.push_back(node+myN0-1);
714 num++;
715 }
716 }
717 if (x>0) {
718 // left
719 index.push_back(node-1);
720 num++;
721 }
722
723 return num;
724 }
725
726 //private
727 void Rectangle::generateCouplePatterns(Paso_Pattern** colPattern, Paso_Pattern** rowPattern) const
728 {
729 IndexVector ptr(1,0);
730 IndexVector index;
731 const dim_t myN0 = (m_offset0==0 ? m_N0 : m_N0-1);
732 const dim_t myN1 = (m_offset1==0 ? m_N1 : m_N1-1);
733 const IndexVector faces=getNumFacesPerBoundary();
734
735 // bottom edge
736 dim_t n=0;
737 if (faces[0] == 0) {
738 index.push_back(2*(myN0+myN1+1));
739 index.push_back(2*(myN0+myN1+1)+1);
740 n+=2;
741 if (faces[2] == 0) {
742 index.push_back(0);
743 index.push_back(1);
744 index.push_back(2);
745 n+=3;
746 }
747 } else if (faces[2] == 0) {
748 index.push_back(1);
749 index.push_back(2);
750 n+=2;
751 }
752 // n=neighbours of bottom-left corner node
753 ptr.push_back(ptr.back()+n);
754 n=0;
755 if (faces[2] == 0) {
756 for (dim_t i=1; i<myN0-1; i++) {
757 index.push_back(i);
758 index.push_back(i+1);
759 index.push_back(i+2);
760 ptr.push_back(ptr.back()+3);
761 }
762 index.push_back(myN0-1);
763 index.push_back(myN0);
764 n+=2;
765 if (faces[1] == 0) {
766 index.push_back(myN0+1);
767 index.push_back(myN0+2);
768 index.push_back(myN0+3);
769 n+=3;
770 }
771 } else {
772 for (dim_t i=1; i<myN0-1; i++) {
773 ptr.push_back(ptr.back());
774 }
775 if (faces[1] == 0) {
776 index.push_back(myN0+2);
777 index.push_back(myN0+3);
778 n+=2;
779 }
780 }
781 // n=neighbours of bottom-right corner node
782 ptr.push_back(ptr.back()+n);
783
784 // 2nd row to 2nd last row
785 for (dim_t i=1; i<myN1-1; i++) {
786 // left edge
787 if (faces[0] == 0) {
788 index.push_back(2*(myN0+myN1+2)-i);
789 index.push_back(2*(myN0+myN1+2)-i-1);
790 index.push_back(2*(myN0+myN1+2)-i-2);
791 ptr.push_back(ptr.back()+3);
792 } else {
793 ptr.push_back(ptr.back());
794 }
795 for (dim_t j=1; j<myN0-1; j++) {
796 ptr.push_back(ptr.back());
797 }
798 // right edge
799 if (faces[1] == 0) {
800 index.push_back(myN0+i+1);
801 index.push_back(myN0+i+2);
802 index.push_back(myN0+i+3);
803 ptr.push_back(ptr.back()+3);
804 } else {
805 ptr.push_back(ptr.back());
806 }
807 }
808
809 // top edge
810 n=0;
811 if (faces[0] == 0) {
812 index.push_back(2*myN0+myN1+5);
813 index.push_back(2*myN0+myN1+4);
814 n+=2;
815 if (faces[3] == 0) {
816 index.push_back(2*myN0+myN1+3);
817 index.push_back(2*myN0+myN1+2);
818 index.push_back(2*myN0+myN1+1);
819 n+=3;
820 }
821 } else if (faces[3] == 0) {
822 index.push_back(2*myN0+myN1+2);
823 index.push_back(2*myN0+myN1+1);
824 n+=2;
825 }
826 // n=neighbours of top-left corner node
827 ptr.push_back(ptr.back()+n);
828 n=0;
829 if (faces[3] == 0) {
830 for (dim_t i=1; i<myN0-1; i++) {
831 index.push_back(2*myN0+myN1+i+1);
832 index.push_back(2*myN0+myN1+i);
833 index.push_back(2*myN0+myN1+i-1);
834 ptr.push_back(ptr.back()+3);
835 }
836 index.push_back(myN0+myN1+4);
837 index.push_back(myN0+myN1+3);
838 n+=2;
839 if (faces[1] == 0) {
840 index.push_back(myN0+myN1+2);
841 index.push_back(myN0+myN1+1);
842 index.push_back(myN0+myN1);
843 n+=3;
844 }
845 } else {
846 for (dim_t i=1; i<myN0-1; i++) {
847 ptr.push_back(ptr.back());
848 }
849 if (faces[1] == 0) {
850 index.push_back(myN0+myN1+1);
851 index.push_back(myN0+myN1);
852 n+=2;
853 }
854 }
855 // n=neighbours of top-right corner node
856 ptr.push_back(ptr.back()+n);
857
858 dim_t M=ptr.size()-1;
859 map<index_t,index_t> idMap;
860 dim_t N=0;
861 for (IndexVector::iterator it=index.begin(); it!=index.end(); it++) {
862 if (idMap.find(*it)==idMap.end()) {
863 idMap[*it]=N;
864 *it=N++;
865 } else {
866 *it=idMap[*it];
867 }
868 }
869
870 /*
871 cout << "--- colCouple_pattern ---" << endl;
872 cout << "M=" << M << ", N=" << N << endl;
873 for (size_t i=0; i<ptr.size(); i++) {
874 cout << "ptr[" << i << "]=" << ptr[i] << endl;
875 }
876 for (size_t i=0; i<index.size(); i++) {
877 cout << "index[" << i << "]=" << index[i] << endl;
878 }
879 */
880
881 // now build the row couple pattern
882 IndexVector ptr2(1,0);
883 IndexVector index2;
884 for (dim_t id=0; id<N; id++) {
885 n=0;
886 for (dim_t i=0; i<M; i++) {
887 for (dim_t j=ptr[i]; j<ptr[i+1]; j++) {
888 if (index[j]==id) {
889 index2.push_back(i);
890 n++;
891 break;
892 }
893 }
894 }
895 ptr2.push_back(ptr2.back()+n);
896 }
897
898 /*
899 cout << "--- rowCouple_pattern ---" << endl;
900 cout << "M=" << N << ", N=" << M << endl;
901 for (size_t i=0; i<ptr2.size(); i++) {
902 cout << "ptr[" << i << "]=" << ptr2[i] << endl;
903 }
904 for (size_t i=0; i<index2.size(); i++) {
905 cout << "index[" << i << "]=" << index2[i] << endl;
906 }
907 */
908
909 // paso will manage the memory
910 index_t* indexC = MEMALLOC(index.size(), index_t);
911 index_t* ptrC = MEMALLOC(ptr.size(), index_t);
912 copy(index.begin(), index.end(), indexC);
913 copy(ptr.begin(), ptr.end(), ptrC);
914 *colPattern=Paso_Pattern_alloc(MATRIX_FORMAT_DEFAULT, M, N, ptrC, indexC);
915
916 // paso will manage the memory
917 indexC = MEMALLOC(index2.size(), index_t);
918 ptrC = MEMALLOC(ptr2.size(), index_t);
919 copy(index2.begin(), index2.end(), indexC);
920 copy(ptr2.begin(), ptr2.end(), ptrC);
921 *rowPattern=Paso_Pattern_alloc(MATRIX_FORMAT_DEFAULT, N, M, ptrC, indexC);
922 }
923
924 //protected
925 void Rectangle::interpolateNodesOnElements(escript::Data& out, escript::Data& in) const
926 {
927 const dim_t numComp = in.getDataPointSize();
928 /* GENERATOR SNIP_INTERPOLATE_ELEMENTS TOP */
929 const double tmp0_2 = 0.62200846792814621559;
930 const double tmp0_1 = 0.044658198738520451079;
931 const double tmp0_0 = 0.16666666666666666667;
932 #pragma omp parallel for
933 for (index_t k1=0; k1 < m_NE1; ++k1) {
934 for (index_t k0=0; k0 < m_NE0; ++k0) {
935 const register double* f_10 = in.getSampleDataRO(INDEX2(k0+1,k1, m_N0));
936 const register double* f_11 = in.getSampleDataRO(INDEX2(k0+1,k1+1, m_N0));
937 const register double* f_01 = in.getSampleDataRO(INDEX2(k0,k1+1, m_N0));
938 const register double* f_00 = in.getSampleDataRO(INDEX2(k0,k1, m_N0));
939 double* o = out.getSampleDataRW(INDEX2(k0,k1,m_NE0));
940 for (index_t i=0; i < numComp; ++i) {
941 o[INDEX2(i,numComp,0)] = f_00[i]*tmp0_2 + f_11[i]*tmp0_1 + tmp0_0*(f_01[i] + f_10[i]);
942 o[INDEX2(i,numComp,1)] = f_01[i]*tmp0_1 + f_10[i]*tmp0_2 + tmp0_0*(f_00[i] + f_11[i]);
943 o[INDEX2(i,numComp,2)] = f_01[i]*tmp0_2 + f_10[i]*tmp0_1 + tmp0_0*(f_00[i] + f_11[i]);
944 o[INDEX2(i,numComp,3)] = f_00[i]*tmp0_1 + f_11[i]*tmp0_2 + tmp0_0*(f_01[i] + f_10[i]);
945 } /* end of component loop i */
946 } /* end of k0 loop */
947 } /* end of k1 loop */
948 /* GENERATOR SNIP_INTERPOLATE_ELEMENTS BOTTOM */
949 }
950
951 //protected
952 void Rectangle::interpolateNodesOnFaces(escript::Data& out, escript::Data& in) const
953 {
954 const dim_t numComp = in.getDataPointSize();
955 /* GENERATOR SNIP_INTERPOLATE_FACES TOP */
956 if (m_faceOffset[0] > -1) {
957 const index_t k0 = 0;
958 const double tmp0_1 = 0.78867513459481288225;
959 const double tmp0_0 = 0.21132486540518711775;
960 #pragma omp parallel for
961 for (index_t k1=0; k1 < m_NE1; ++k1) {
962 const register double* f_01 = in.getSampleDataRO(INDEX2(0,k1+1, m_N0));
963 const register double* f_00 = in.getSampleDataRO(INDEX2(0,k1, m_N0));
964 double* o = out.getSampleDataRW(m_faceOffset[0]+INDEX2(k0,k1,m_NE0));
965 for (index_t i=0; i < numComp; ++i) {
966 o[INDEX2(i,numComp,0)] = f_00[i]*tmp0_1 + f_01[i]*tmp0_0;
967 o[INDEX2(i,numComp,1)] = f_00[i]*tmp0_0 + f_01[i]*tmp0_1;
968 } /* end of component loop i */
969 } /* end of k1 loop */
970 } /* end of face 0 */
971 if (m_faceOffset[1] > -1) {
972 const index_t k0 = 0;
973 const double tmp0_1 = 0.21132486540518711775;
974 const double tmp0_0 = 0.78867513459481288225;
975 #pragma omp parallel for
976 for (index_t k1=0; k1 < m_NE1; ++k1) {
977 const register double* f_10 = in.getSampleDataRO(INDEX2(m_N0-1,k1, m_N0));
978 const register double* f_11 = in.getSampleDataRO(INDEX2(m_N0-1,k1+1, m_N0));
979 double* o = out.getSampleDataRW(m_faceOffset[1]+INDEX2(k0,k1,m_NE0));
980 for (index_t i=0; i < numComp; ++i) {
981 o[INDEX2(i,numComp,0)] = f_10[i]*tmp0_0 + f_11[i]*tmp0_1;
982 o[INDEX2(i,numComp,1)] = f_10[i]*tmp0_1 + f_11[i]*tmp0_0;
983 } /* end of component loop i */
984 } /* end of k1 loop */
985 } /* end of face 1 */
986 if (m_faceOffset[2] > -1) {
987 const index_t k1 = 0;
988 const double tmp0_1 = 0.78867513459481288225;
989 const double tmp0_0 = 0.21132486540518711775;
990 #pragma omp parallel for
991 for (index_t k0=0; k0 < m_NE0; ++k0) {
992 const register double* f_10 = in.getSampleDataRO(INDEX2(k0+1,0, m_N0));
993 const register double* f_00 = in.getSampleDataRO(INDEX2(k0,0, m_N0));
994 double* o = out.getSampleDataRW(m_faceOffset[2]+INDEX2(k0,k1,m_NE0));
995 for (index_t i=0; i < numComp; ++i) {
996 o[INDEX2(i,numComp,0)] = f_00[i]*tmp0_1 + f_10[i]*tmp0_0;
997 o[INDEX2(i,numComp,1)] = f_00[i]*tmp0_0 + f_10[i]*tmp0_1;
998 } /* end of component loop i */
999 } /* end of k0 loop */
1000 } /* end of face 2 */
1001 if (m_faceOffset[3] > -1) {
1002 const index_t k1 = 0;
1003 const double tmp0_1 = 0.78867513459481288225;
1004 const double tmp0_0 = 0.21132486540518711775;
1005 #pragma omp parallel for
1006 for (index_t k0=0; k0 < m_NE0; ++k0) {
1007 const register double* f_11 = in.getSampleDataRO(INDEX2(k0+1,m_N1-1, m_N0));
1008 const register double* f_01 = in.getSampleDataRO(INDEX2(k0,m_N1-1, m_N0));
1009 double* o = out.getSampleDataRW(m_faceOffset[3]+INDEX2(k0,k1,m_NE0));
1010 for (index_t i=0; i < numComp; ++i) {
1011 o[INDEX2(i,numComp,0)] = f_01[i]*tmp0_1 + f_11[i]*tmp0_0;
1012 o[INDEX2(i,numComp,1)] = f_01[i]*tmp0_0 + f_11[i]*tmp0_1;
1013 } /* end of component loop i */
1014 } /* end of k0 loop */
1015 } /* end of face 3 */
1016 /* GENERATOR SNIP_INTERPOLATE_FACES BOTTOM */
1017 }
1018
1019 } // end of namespace ripley
1020

  ViewVC Help
Powered by ViewVC 1.1.26