/[escript]/trunk/esys2/bruce/src/Bruce/Bruce.cpp
ViewVC logotype

Contents of /trunk/esys2/bruce/src/Bruce/Bruce.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 150 - (show annotations)
Thu Sep 15 03:44:45 2005 UTC (15 years ago) by jgs
File size: 14030 byte(s)
Merge of development branch dev-02 back to main trunk on 2005-09-15

1 // $Id$
2 /*
3 ******************************************************************************
4 * *
5 * COPYRIGHT ACcESS 2005 - All Rights Reserved *
6 * *
7 * This software is the property of ACcESS. No part of this code *
8 * may be copied in any form or by any means without the expressed written *
9 * consent of ACcESS. Copying, use or modification of this software *
10 * by any unauthorised person is illegal unless that person has a software *
11 * license agreement with ACcESS. *
12 * *
13 ******************************************************************************
14 */
15
16 #include "bruce/Bruce/Bruce.h"
17 #include "bruce/Bruce/BruceException.h"
18
19 using namespace std;
20 using namespace escript;
21
22 namespace bruce {
23
24 const int Bruce::ContinuousFunction=0;
25 const int Bruce::Function=1;
26
27 Bruce::FunctionSpaceNamesMapType Bruce::m_functionSpaceTypeNames;
28
29 Bruce::Bruce()
30 {
31 setFunctionSpaceTypeNames();
32 }
33
34 Bruce::Bruce(DimVec v0, DimVec v1, DimVec v2,
35 int n0, int n1, int n2,
36 DimVec origin):
37 m_v0(v0), m_v1(v1), m_v2(v2),
38 m_n0(n0), m_n1(n1), m_n2(n2),
39 m_origin(origin)
40 {
41 if (!checkParameters()) {
42 stringstream temp;
43 temp << "Error - Invalid parameters supplied to constructor.";
44 throw BruceException(temp.str());
45 }
46 setFunctionSpaceTypeNames();
47 }
48
49 Bruce::Bruce(const Bruce& other):
50 m_v0(other.m_v0), m_v1(other.m_v1), m_v2(other.m_v2),
51 m_n0(other.m_n0), m_n1(other.m_n1), m_n2(other.m_n2),
52 m_origin(other.m_origin)
53 {
54 setFunctionSpaceTypeNames();
55 }
56
57 Bruce::~Bruce()
58 {
59 m_n0=-1;
60 m_n1=-1;
61 m_n2=-1;
62 m_origin.clear();
63 m_v0.clear();
64 m_v1.clear();
65 m_v2.clear();
66 }
67
68 void
69 Bruce::setFunctionSpaceTypeNames()
70 {
71 m_functionSpaceTypeNames.insert
72 (FunctionSpaceNamesMapType::value_type(Function,"Bruce_Function"));
73 m_functionSpaceTypeNames.insert
74 (FunctionSpaceNamesMapType::value_type(ContinuousFunction,"Bruce_ContinuousFunction"));
75 }
76
77 bool
78 Bruce::checkParameters()
79 {
80 if (m_origin.size()>3) {
81 return false;
82 }
83
84 if (m_n0<1) {
85 m_n0=1;
86 }
87 if (m_n1<1) {
88 m_n1=1;
89 }
90 if (m_n2<1) {
91 m_n2=1;
92 }
93
94 //
95 // domains in 3d space
96 if (m_origin.size()==3) {
97
98 if (m_v0.size()==0) {
99
100 // 0d domain in 3d space
101 if ( (m_v1.size()!=0) || (m_v2.size()!=0) ) {
102 return false;
103 }
104
105 m_v0.clear();
106 m_v0.push_back(0);
107 m_v0.push_back(0);
108 m_v0.push_back(0);
109 m_v1.clear();
110 m_v1.push_back(0);
111 m_v1.push_back(0);
112 m_v1.push_back(0);
113 m_v2.clear();
114 m_v2.push_back(0);
115 m_v2.push_back(0);
116 m_v2.push_back(0);
117
118 m_n0=1;
119 m_n1=1;
120 m_n2=1;
121
122 } else if (m_v1.size()==0) {
123
124 // 1d domain in 3d space
125 if ( (m_v0.size()!=3) || (m_v2.size()!=0) ) {
126 return false;
127 }
128
129 m_v1.clear();
130 m_v1.push_back(0);
131 m_v1.push_back(0);
132 m_v1.push_back(0);
133 m_v2.clear();
134 m_v2.push_back(0);
135 m_v2.push_back(0);
136 m_v2.push_back(0);
137
138 m_n1=1;
139 m_n2=1;
140
141 } else if (m_v2.size()==0) {
142
143 // 2d domain in 3d space
144 if ( (m_v0.size()!=3) || (m_v1.size()!=3) ) {
145 return false;
146 }
147
148 m_v2.clear();
149 m_v2.push_back(0);
150 m_v2.push_back(0);
151 m_v2.push_back(0);
152
153 m_n2=1;
154
155 } else {
156
157 // 3d domain in 3d space
158 if ( (m_v0.size()!=3) || (m_v1.size()!=3) || (m_v2.size()!=3) ) {
159 return false;
160 }
161
162 }
163
164 }
165
166 //
167 // domains in 2d space
168 if (m_origin.size()==2) {
169
170 if (m_v0.size()==0) {
171
172 // 0d domain in 2d space
173 if (m_v1.size()!=0) {
174 return false;
175 }
176
177 m_v0.clear();
178 m_v0.push_back(0);
179 m_v0.push_back(0);
180 m_v1.clear();
181 m_v1.push_back(0);
182 m_v1.push_back(0);
183 m_v2.clear();
184
185 m_n0=1;
186 m_n1=1;
187 m_n2=1;
188
189 } else if (m_v1.size()==0) {
190
191 // 1d domain in 2d space
192 if (m_v0.size()!=2) {
193 return false;
194 }
195
196 m_v1.clear();
197 m_v1.push_back(0);
198 m_v1.push_back(0);
199 m_v2.clear();
200
201 m_n1=1;
202 m_n2=1;
203
204 } else {
205
206 // 2d domain in 2d space
207 if ( (m_v0.size()!=2) || (m_v1.size()!=2) ) {
208 return false;
209 }
210
211 m_v2.clear();
212
213 m_n2=1;
214
215 }
216
217 }
218
219 //
220 // domains in 1d space
221 if (m_origin.size()==1) {
222
223 if (m_v0.size()==0) {
224
225 // 0d domain in 1d space
226 m_v0.clear();
227 m_v0.push_back(0);
228 m_v1.clear();
229 m_v2.clear();
230
231 m_n0=1;
232 m_n1=1;
233 m_n2=1;
234
235 } else {
236
237 // 1d domain in 1d space
238 if (m_v0.size()!=1) {
239 return false;
240 }
241
242 m_v1.clear();
243 m_v2.clear();
244
245 m_n1=1;
246 m_n2=1;
247
248 }
249
250 }
251
252 //
253 // domains in 0d space
254 if (m_origin.size()==0) {
255
256 // point (0d) domain in 0d space
257 m_v0.clear();
258 m_v1.clear();
259 m_v2.clear();
260
261 m_n0=1;
262 m_n1=1;
263 m_n2=1;
264
265 }
266
267 return true;
268 }
269
270 std::string
271 Bruce::getDescription() const
272 {
273 return "Bruce";
274 }
275
276 bool
277 Bruce::isValidFunctionSpaceType(int functionSpaceCode) const
278 {
279 FunctionSpaceNamesMapType::iterator loc;
280 loc=m_functionSpaceTypeNames.find(functionSpaceCode);
281 return (loc!=m_functionSpaceTypeNames.end());
282 }
283
284 std::string
285 Bruce::functionSpaceTypeAsString(int functionSpaceCode) const
286 {
287 FunctionSpaceNamesMapType::iterator loc;
288 loc=m_functionSpaceTypeNames.find(functionSpaceCode);
289 if (loc==m_functionSpaceTypeNames.end()) {
290 stringstream temp;
291 temp << "Error - Invalid function space type code.";
292 throw BruceException(temp.str());
293 } else {
294 return loc->second;
295 }
296 }
297
298 int
299 Bruce::getContinuousFunctionCode() const
300 {
301 return ContinuousFunction;
302 }
303
304 int
305 Bruce::getFunctionCode() const
306 {
307 return Function;
308 }
309
310 int
311 Bruce::getDim() const
312 {
313 return m_origin.size();
314 }
315
316 pair<int,int>
317 Bruce::getDataShape(int functionSpaceCode) const
318 {
319 int numDataPointsPerSample=1;
320 int numSamples;
321
322 switch (functionSpaceCode) {
323
324 //
325 // Continuous functions
326 case(ContinuousFunction):
327
328 numSamples = m_n0 * m_n1 * m_n2;
329 break;
330
331 //
332 // Functions
333 case(Function):
334
335 // 0d domains
336 if (getDim()==0) {
337
338 numSamples = 0;
339
340 // 1d domains
341 } else if (getDim()==1) {
342
343 if (isZero(m_v0)) {
344 numSamples = 0;
345 } else {
346 numSamples = m_n0-1;
347 }
348
349 // 2d domains
350 } else if (getDim()==2) {
351
352 if (isZero(m_v0)) {
353 numSamples = 0;
354 } else if (isZero(m_v1)) {
355 numSamples = m_n0-1;
356 } else {
357 numSamples = (m_n0-1) * (m_n1-1);
358 }
359
360 // 3d domains
361 } else {
362
363 if (isZero(m_v0)) {
364 numSamples = 0;
365 } else if (isZero(m_v1)) {
366 numSamples = m_n0-1;
367 } else if (isZero(m_v2)) {
368 numSamples = (m_n0-1) * (m_n1-1);
369 } else {
370 numSamples = (m_n0-1) * (m_n1-1) * (m_n2-1);
371 }
372
373 }
374
375 break;
376
377 default:
378 stringstream temp;
379 temp << "Error - Invalid function space type: "
380 << functionSpaceCode << " for domain: " << getDescription();
381 throw BruceException(temp.str());
382 break;
383
384 }
385
386 return pair<int,int>(numDataPointsPerSample,numSamples);
387 }
388
389 Data
390 Bruce::getX() const
391 {
392 FunctionSpace tempFunc = continuousFunction(asAbstractContinuousDomain());
393 return tempFunc.getX();
394 }
395
396 void
397 Bruce::setToX(escript::Data& out) const
398 {
399
400 //
401 // determine functionSpace type expected by supplied Data object
402 int functionSpaceCode = out.getFunctionSpace().getTypeCode();
403
404 //
405 // ensure supplied Data object has a matching number of data-points
406 // for this Bruce domain object
407 std::pair<int,int> domainShape = getDataShape(functionSpaceCode);
408 if(domainShape.first!=out.getNumDataPointsPerSample() ||
409 domainShape.second!=out.getNumSamples()) {
410 stringstream temp;
411 temp << "Error - Incompatible number of data-points Data object supplied to Bruce::setToX";
412 throw BruceException(temp.str());
413 }
414
415 int dim = getDim();
416 int numSamples = domainShape.second;
417
418 //
419 // ensure shape of data-points in supplied Data object matches the
420 // shape needed to store the coordinates of each data-point in this
421 // Bruce domain
422 std::vector<int> dataShape = out.getDataPointShape();
423 if (dataShape.size()!=1 || dataShape[0]!=dim) {
424 stringstream temp;
425 temp << "Error - Incompatible shape Data object supplied to Bruce::setToX";
426 throw BruceException(temp.str());
427 }
428
429 if (functionSpaceCode==ContinuousFunction) {
430
431 //
432 // calculate the spatial coordinates of data-points
433 // located on the nodes of this Bruce domain
434
435 if (dim==0) {
436
437 // Bruce domains in 0d space
438
439 int sampleNo=0;
440 DataAbstract::ValueType::value_type* sampleData = out.getSampleData(sampleNo);
441 //sampleData[0] = m_origin[0];
442 sampleNo++;
443 if (sampleNo!=numSamples) {
444 stringstream temp;
445 temp << "Bruce::setToX: Didn't iterate across correct number of samples.";
446 throw BruceException(temp.str());
447 }
448
449 } else if (dim==1) {
450
451 // Bruce domains in 1d space
452
453 int sampleNo=0;
454 for (int i=0; i<m_n0; i++) {
455 DataAbstract::ValueType::value_type* sampleData = out.getSampleData(sampleNo);
456 sampleData[0] = m_origin[0] + m_v0[0]*i;
457 sampleNo++;
458 }
459 if (sampleNo!=numSamples) {
460 stringstream temp;
461 temp << "Bruce::setToX: Didn't iterate across correct number of samples.";
462 throw BruceException(temp.str());
463 }
464
465 } else if (dim==2) {
466
467 // Bruce domains in 2d space
468
469 int sampleNo=0;
470 for (int i=0; i<m_n0; i++) {
471 for (int j=0; j<m_n1; j++) {
472 DataAbstract::ValueType::value_type* sampleData = out.getSampleData(sampleNo);
473 for (int d=0; d<dim; d++) {
474 sampleData[d] = m_origin[d] + m_v0[d]*i + m_v1[d]*j;
475 }
476 sampleNo++;
477 }
478 }
479 if (sampleNo!=numSamples) {
480 stringstream temp;
481 temp << "Bruce::setToX: Didn't iterate across correct number of samples.";
482 throw BruceException(temp.str());
483 }
484
485 } else if (dim==3) {
486
487 // Bruce domains in 3d space
488
489 int sampleNo=0;
490 for (int i=0; i<m_n0; i++) {
491 for (int j=0; j<m_n1; j++) {
492 for (int k=0; k<m_n2; k++) {
493 DataAbstract::ValueType::value_type* sampleData = out.getSampleData(sampleNo);
494 for (int d=0; d<dim; d++) {
495 sampleData[d] = m_origin[d] + m_v0[d]*i + m_v1[d]*j + m_v2[d]*k;
496 }
497 sampleNo++;
498 }
499 }
500 }
501 if (sampleNo!=numSamples) {
502 stringstream temp;
503 temp << "Bruce::setToX: Didn't iterate across correct number of samples.";
504 throw BruceException(temp.str());
505 }
506
507 }
508
509 } else if (functionSpaceCode==Function) {
510
511 //
512 // calculate the spatial coordinates of data-points
513 // located on the cell centres of this Bruce domain
514
515 for (int i=0; i<numSamples; i++) {
516 DataAbstract::ValueType::value_type* sampleData = out.getSampleData(i);
517 for (int j=0; j<dim; j++) {
518 //cout << "====> " << sampleData[j] << endl;
519 }
520 }
521
522 } else {
523 stringstream temp;
524 temp << "Error - Invalid function space type: "
525 << functionSpaceCode << " for domain: " << getDescription();
526 throw BruceException(temp.str());
527 }
528
529 }
530
531 Data
532 Bruce::getSize() const
533 {
534 FunctionSpace tempFunc = function(asAbstractContinuousDomain());
535 return tempFunc.getSize();
536 }
537
538 void
539 Bruce::setToSize(escript::Data& out) const
540 {
541
542 int functionSpaceCode = out.getFunctionSpace().getTypeCode();
543
544 std::pair<int,int> domainShape = getDataShape(functionSpaceCode);
545 if(domainShape.first!=out.getNumDataPointsPerSample() ||
546 domainShape.second!=out.getNumSamples()) {
547 stringstream temp;
548 temp << "Error - Incompatible Data object supplied to Bruce::setToX";
549 throw BruceException(temp.str());
550 }
551
552 std::vector<int> dataShape = out.getDataPointShape();
553 if (dataShape.size()!=0) {
554 stringstream temp;
555 temp << "Error - Incompatible Data object supplied to Bruce::setToSize";
556 throw BruceException(temp.str());
557 }
558
559 int numSamples = domainShape.second;
560
561 if (functionSpaceCode==ContinuousFunction) {
562
563 cout << "ContinuousFunction" << endl;
564
565 for (int i=0; i<numSamples; i++) {
566 DataAbstract::ValueType::value_type* sampleData = out.getSampleData(i);
567 //cout << "====> " << sampleData[0] << endl;
568 }
569
570 } else if (functionSpaceCode==Function) {
571
572 cout << "Function" << endl;
573
574 for (int i=0; i<numSamples; i++) {
575 DataAbstract::ValueType::value_type* sampleData = out.getSampleData(i);
576 //cout << "====> " << sampleData[0] << endl;
577 }
578
579 } else {
580 stringstream temp;
581 temp << "Error - Invalid function space type: "
582 << functionSpaceCode << " for domain: " << getDescription();
583 throw BruceException(temp.str());
584 }
585
586 }
587
588 bool
589 Bruce::operator==(const AbstractDomain& other) const
590 {
591 const Bruce* temp=dynamic_cast<const Bruce*>(&other);
592 if (temp!=0) {
593 if ((m_v0 != temp->m_v0) || (m_v1 != temp->m_v1) || (m_v2 != temp->m_v2)) {
594 return false;
595 }
596 if ((m_n0 != temp->m_n0) || (m_n1 != temp->m_n1) || (m_n2 != temp->m_n2)) {
597 return false;
598 }
599 if (m_origin != temp->m_origin) {
600 return false;
601 }
602 return true;
603 } else {
604 return false;
605 }
606 }
607
608 bool
609 Bruce::operator!=(const AbstractDomain& other) const
610 {
611 return !(operator==(other));
612 }
613
614 bool
615 Bruce::isZero(DimVec vec) const
616 {
617 for (int i=0; i<vec.size(); i++) {
618 if (vec[i] != 0) {
619 return false;
620 }
621 }
622 return true;
623 }
624
625 } // end of namespace

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26