/[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 3702 - (show annotations)
Fri Dec 2 06:12:32 2011 UTC (8 years ago) by caltinay
File size: 32253 byte(s)
Gradient for Rectangle elements.

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 // elements
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 elements
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
663 //private
664 int Rectangle::insertNeighbours(IndexVector& index, index_t node) const
665 {
666 const dim_t myN0 = (m_offset0==0 ? m_N0 : m_N0-1);
667 const dim_t myN1 = (m_offset1==0 ? m_N1 : m_N1-1);
668 const int x=node%myN0;
669 const int y=node/myN0;
670 int num=0;
671 if (y>0) {
672 if (x>0) {
673 // bottom-left
674 index.push_back(node-myN0-1);
675 num++;
676 }
677 // bottom
678 index.push_back(node-myN0);
679 num++;
680 if (x<myN0-1) {
681 // bottom-right
682 index.push_back(node-myN0+1);
683 num++;
684 }
685 }
686 if (x<myN0-1) {
687 // right
688 index.push_back(node+1);
689 num++;
690 if (y<myN1-1) {
691 // top-right
692 index.push_back(node+myN0+1);
693 num++;
694 }
695 }
696 if (y<myN1-1) {
697 // top
698 index.push_back(node+myN0);
699 num++;
700 if (x>0) {
701 // top-left
702 index.push_back(node+myN0-1);
703 num++;
704 }
705 }
706 if (x>0) {
707 // left
708 index.push_back(node-1);
709 num++;
710 }
711
712 return num;
713 }
714
715 //private
716 void Rectangle::generateCouplePatterns(Paso_Pattern** colPattern, Paso_Pattern** rowPattern) const
717 {
718 IndexVector ptr(1,0);
719 IndexVector index;
720 const dim_t myN0 = (m_offset0==0 ? m_N0 : m_N0-1);
721 const dim_t myN1 = (m_offset1==0 ? m_N1 : m_N1-1);
722 const IndexVector faces=getNumFacesPerBoundary();
723
724 // bottom edge
725 dim_t n=0;
726 if (faces[0] == 0) {
727 index.push_back(2*(myN0+myN1+1));
728 index.push_back(2*(myN0+myN1+1)+1);
729 n+=2;
730 if (faces[2] == 0) {
731 index.push_back(0);
732 index.push_back(1);
733 index.push_back(2);
734 n+=3;
735 }
736 } else if (faces[2] == 0) {
737 index.push_back(1);
738 index.push_back(2);
739 n+=2;
740 }
741 // n=neighbours of bottom-left corner node
742 ptr.push_back(ptr.back()+n);
743 n=0;
744 if (faces[2] == 0) {
745 for (dim_t i=1; i<myN0-1; i++) {
746 index.push_back(i);
747 index.push_back(i+1);
748 index.push_back(i+2);
749 ptr.push_back(ptr.back()+3);
750 }
751 index.push_back(myN0-1);
752 index.push_back(myN0);
753 n+=2;
754 if (faces[1] == 0) {
755 index.push_back(myN0+1);
756 index.push_back(myN0+2);
757 index.push_back(myN0+3);
758 n+=3;
759 }
760 } else {
761 for (dim_t i=1; i<myN0-1; i++) {
762 ptr.push_back(ptr.back());
763 }
764 if (faces[1] == 0) {
765 index.push_back(myN0+2);
766 index.push_back(myN0+3);
767 n+=2;
768 }
769 }
770 // n=neighbours of bottom-right corner node
771 ptr.push_back(ptr.back()+n);
772
773 // 2nd row to 2nd last row
774 for (dim_t i=1; i<myN1-1; i++) {
775 // left edge
776 if (faces[0] == 0) {
777 index.push_back(2*(myN0+myN1+2)-i);
778 index.push_back(2*(myN0+myN1+2)-i-1);
779 index.push_back(2*(myN0+myN1+2)-i-2);
780 ptr.push_back(ptr.back()+3);
781 } else {
782 ptr.push_back(ptr.back());
783 }
784 for (dim_t j=1; j<myN0-1; j++) {
785 ptr.push_back(ptr.back());
786 }
787 // right edge
788 if (faces[1] == 0) {
789 index.push_back(myN0+i+1);
790 index.push_back(myN0+i+2);
791 index.push_back(myN0+i+3);
792 ptr.push_back(ptr.back()+3);
793 } else {
794 ptr.push_back(ptr.back());
795 }
796 }
797
798 // top edge
799 n=0;
800 if (faces[0] == 0) {
801 index.push_back(2*myN0+myN1+5);
802 index.push_back(2*myN0+myN1+4);
803 n+=2;
804 if (faces[3] == 0) {
805 index.push_back(2*myN0+myN1+3);
806 index.push_back(2*myN0+myN1+2);
807 index.push_back(2*myN0+myN1+1);
808 n+=3;
809 }
810 } else if (faces[3] == 0) {
811 index.push_back(2*myN0+myN1+2);
812 index.push_back(2*myN0+myN1+1);
813 n+=2;
814 }
815 // n=neighbours of top-left corner node
816 ptr.push_back(ptr.back()+n);
817 n=0;
818 if (faces[3] == 0) {
819 for (dim_t i=1; i<myN0-1; i++) {
820 index.push_back(2*myN0+myN1+i+1);
821 index.push_back(2*myN0+myN1+i);
822 index.push_back(2*myN0+myN1+i-1);
823 ptr.push_back(ptr.back()+3);
824 }
825 index.push_back(myN0+myN1+4);
826 index.push_back(myN0+myN1+3);
827 n+=2;
828 if (faces[1] == 0) {
829 index.push_back(myN0+myN1+2);
830 index.push_back(myN0+myN1+1);
831 index.push_back(myN0+myN1);
832 n+=3;
833 }
834 } else {
835 for (dim_t i=1; i<myN0-1; i++) {
836 ptr.push_back(ptr.back());
837 }
838 if (faces[1] == 0) {
839 index.push_back(myN0+myN1+1);
840 index.push_back(myN0+myN1);
841 n+=2;
842 }
843 }
844 // n=neighbours of top-right corner node
845 ptr.push_back(ptr.back()+n);
846
847 dim_t M=ptr.size()-1;
848 map<index_t,index_t> idMap;
849 dim_t N=0;
850 for (IndexVector::iterator it=index.begin(); it!=index.end(); it++) {
851 if (idMap.find(*it)==idMap.end()) {
852 idMap[*it]=N;
853 *it=N++;
854 } else {
855 *it=idMap[*it];
856 }
857 }
858
859 /*
860 cout << "--- colCouple_pattern ---" << endl;
861 cout << "M=" << M << ", N=" << N << endl;
862 for (size_t i=0; i<ptr.size(); i++) {
863 cout << "ptr[" << i << "]=" << ptr[i] << endl;
864 }
865 for (size_t i=0; i<index.size(); i++) {
866 cout << "index[" << i << "]=" << index[i] << endl;
867 }
868 */
869
870 // now build the row couple pattern
871 IndexVector ptr2(1,0);
872 IndexVector index2;
873 for (dim_t id=0; id<N; id++) {
874 n=0;
875 for (dim_t i=0; i<M; i++) {
876 for (dim_t j=ptr[i]; j<ptr[i+1]; j++) {
877 if (index[j]==id) {
878 index2.push_back(i);
879 n++;
880 break;
881 }
882 }
883 }
884 ptr2.push_back(ptr2.back()+n);
885 }
886
887 /*
888 cout << "--- rowCouple_pattern ---" << endl;
889 cout << "M=" << N << ", N=" << M << endl;
890 for (size_t i=0; i<ptr2.size(); i++) {
891 cout << "ptr[" << i << "]=" << ptr2[i] << endl;
892 }
893 for (size_t i=0; i<index2.size(); i++) {
894 cout << "index[" << i << "]=" << index2[i] << endl;
895 }
896 */
897
898 // paso will manage the memory
899 index_t* indexC = MEMALLOC(index.size(), index_t);
900 index_t* ptrC = MEMALLOC(ptr.size(), index_t);
901 copy(index.begin(), index.end(), indexC);
902 copy(ptr.begin(), ptr.end(), ptrC);
903 *colPattern=Paso_Pattern_alloc(MATRIX_FORMAT_DEFAULT, M, N, ptrC, indexC);
904
905 // paso will manage the memory
906 indexC = MEMALLOC(index2.size(), index_t);
907 ptrC = MEMALLOC(ptr2.size(), index_t);
908 copy(index2.begin(), index2.end(), indexC);
909 copy(ptr2.begin(), ptr2.end(), ptrC);
910 *rowPattern=Paso_Pattern_alloc(MATRIX_FORMAT_DEFAULT, N, M, ptrC, indexC);
911 }
912
913 //protected
914 void Rectangle::interpolateNodesOnElements(escript::Data& out, escript::Data& in) const
915 {
916 const dim_t numComp = in.getDataPointSize();
917 /* GENERATOR SNIP_INTERPOLATE_ELEMENTS TOP */
918 const double tmp0_2 = 0.62200846792814621559;
919 const double tmp0_1 = 0.044658198738520451079;
920 const double tmp0_0 = 0.16666666666666666667;
921 #pragma omp parallel for
922 for (index_t k1=0; k1 < m_NE1; ++k1) {
923 for (index_t k0=0; k0 < m_NE0; ++k0) {
924 const register double* f_10 = in.getSampleDataRO(INDEX2(k0+1,k1, m_N0));
925 const register double* f_11 = in.getSampleDataRO(INDEX2(k0+1,k1+1, m_N0));
926 const register double* f_01 = in.getSampleDataRO(INDEX2(k0,k1+1, m_N0));
927 const register double* f_00 = in.getSampleDataRO(INDEX2(k0,k1, m_N0));
928 double* o = out.getSampleDataRW(INDEX2(k0,k1,m_NE0));
929 for (index_t i=0; i < numComp; ++i) {
930 o[INDEX2(i,numComp,0)] = f_00[i]*tmp0_2 + f_11[i]*tmp0_1 + tmp0_0*(f_01[i] + f_10[i]);
931 o[INDEX2(i,numComp,1)] = f_01[i]*tmp0_1 + f_10[i]*tmp0_2 + tmp0_0*(f_00[i] + f_11[i]);
932 o[INDEX2(i,numComp,2)] = f_01[i]*tmp0_2 + f_10[i]*tmp0_1 + tmp0_0*(f_00[i] + f_11[i]);
933 o[INDEX2(i,numComp,3)] = f_00[i]*tmp0_1 + f_11[i]*tmp0_2 + tmp0_0*(f_01[i] + f_10[i]);
934 } /* end of component loop i */
935 } /* end of k0 loop */
936 } /* end of k1 loop */
937 /* GENERATOR SNIP_INTERPOLATE_ELEMENTS BOTTOM */
938 }
939
940 //protected
941 void Rectangle::interpolateNodesOnFaces(escript::Data& out, escript::Data& in) const
942 {
943 throw RipleyException("interpolateNodesOnFaces() not implemented");
944 }
945
946 } // end of namespace ripley
947

  ViewVC Help
Powered by ViewVC 1.1.26