/[escript]/branches/schroedinger/escript/src/Data.cpp
ViewVC logotype

Contents of /branches/schroedinger/escript/src/Data.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1903 - (show annotations)
Wed Oct 22 06:11:01 2008 UTC (11 years, 9 months ago) by jfenwick
File size: 81982 byte(s)
More fixes to doxygen for scons.
Added += and other forms of +

1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2008 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
15 #include "Data.h"
16
17 #include "DataExpanded.h"
18 #include "DataConstant.h"
19 #include "DataTagged.h"
20 #include "DataEmpty.h"
21 #include "DataLazy.h"
22 #include "FunctionSpaceFactory.h"
23 #include "AbstractContinuousDomain.h"
24 #include "UnaryFuncs.h"
25 #include "FunctionSpaceException.h"
26
27 extern "C" {
28 #include "escript/blocktimer.h"
29 }
30
31 #include <fstream>
32 #include <algorithm>
33 #include <vector>
34 #include <functional>
35
36 #include <boost/python/dict.hpp>
37 #include <boost/python/extract.hpp>
38 #include <boost/python/long.hpp>
39
40 using namespace std;
41 using namespace boost::python;
42 using namespace boost;
43 using namespace escript;
44
45 // ensure the current object is not a DataLazy
46 #define FORCERESOLVE if (isLazy()) {resolve();}
47
48 Data::Data()
49 {
50 //
51 // Default data is type DataEmpty
52 DataAbstract* temp=new DataEmpty();
53 m_data=temp->getPtr();
54 m_protected=false;
55 }
56
57 Data::Data(double value,
58 const tuple& shape,
59 const FunctionSpace& what,
60 bool expanded)
61 {
62 DataTypes::ShapeType dataPointShape;
63 for (int i = 0; i < shape.attr("__len__")(); ++i) {
64 dataPointShape.push_back(extract<const int>(shape[i]));
65 }
66
67 int len = DataTypes::noValues(dataPointShape);
68 DataVector temp_data(len,value,len);
69 initialise(temp_data, dataPointShape, what, expanded);
70 m_protected=false;
71 }
72
73 Data::Data(double value,
74 const DataTypes::ShapeType& dataPointShape,
75 const FunctionSpace& what,
76 bool expanded)
77 {
78 int len = DataTypes::noValues(dataPointShape);
79
80 DataVector temp_data(len,value,len);
81 // DataArrayView temp_dataView(temp_data, dataPointShape);
82
83 // initialise(temp_dataView, what, expanded);
84 initialise(temp_data, dataPointShape, what, expanded);
85
86 m_protected=false;
87 }
88
89 Data::Data(const Data& inData)
90 {
91 m_data=inData.m_data;
92 m_protected=inData.isProtected();
93 }
94
95
96 Data::Data(const Data& inData,
97 const DataTypes::RegionType& region)
98 {
99 //
100 // Create Data which is a slice of another Data
101 DataAbstract* tmp = inData.m_data->getSlice(region);
102 m_data=DataAbstract_ptr(tmp);
103 m_protected=false;
104 }
105
106 Data::Data(const Data& inData,
107 const FunctionSpace& functionspace)
108 {
109 if (inData.isEmpty())
110 {
111 throw DataException("Error - will not interpolate for instances of DataEmpty.");
112 }
113 if (inData.isLazy())
114 {
115 throw DataException("Error - will not interpolate for instances of DataLazy - yet.");
116 }
117 if (inData.getFunctionSpace()==functionspace) {
118 m_data=inData.m_data;
119 }
120 else
121 { // this cast is safe for ref count because the class holds a reference
122 const DataReady* dr=dynamic_cast<const DataReady*>(inData.m_data.get());
123 if (inData.isConstant()) { // for a constant function, we just need to use the new function space
124 if (!inData.probeInterpolation(functionspace))
125 { // Even though this is constant, we still need to check whether interpolation is allowed
126 throw FunctionSpaceException("Call to probeInterpolation returned false for DataConstant.");
127 }
128 DataConstant* dc=new DataConstant(functionspace,inData.m_data->getShape(),dr->getVector());
129 m_data=DataAbstract_ptr(dc);
130 } else {
131 Data tmp(0,inData.getDataPointShape(),functionspace,true);
132 // Note: Must use a reference or pointer to a derived object
133 // in order to get polymorphic behaviour. Shouldn't really
134 // be able to create an instance of AbstractDomain but that was done
135 // as a boost:python work around which may no longer be required.
136 /*const AbstractDomain& inDataDomain=inData.getDomain();*/
137 const_Domain_ptr inDataDomain=inData.getDomain();
138 if (inDataDomain==functionspace.getDomain()) {
139 inDataDomain->interpolateOnDomain(tmp,inData);
140 } else {
141 inDataDomain->interpolateACross(tmp,inData);
142 }
143 m_data=tmp.m_data;
144 }
145 }
146 m_protected=false;
147 }
148
149 Data::Data(DataAbstract* underlyingdata)
150 {
151 // m_data=shared_ptr<DataAbstract>(underlyingdata);
152 m_data=underlyingdata->getPtr();
153 m_protected=false;
154 }
155
156 Data::Data(DataAbstract_ptr underlyingdata)
157 {
158 m_data=underlyingdata;
159 m_protected=false;
160 }
161
162
163 Data::Data(const numeric::array& value,
164 const FunctionSpace& what,
165 bool expanded)
166 {
167 initialise(value,what,expanded);
168 m_protected=false;
169 }
170 /*
171 Data::Data(const DataArrayView& value,
172 const FunctionSpace& what,
173 bool expanded)
174 {
175 initialise(value,what,expanded);
176 m_protected=false;
177 }*/
178
179 Data::Data(const DataTypes::ValueType& value,
180 const DataTypes::ShapeType& shape,
181 const FunctionSpace& what,
182 bool expanded)
183 {
184 initialise(value,shape,what,expanded);
185 m_protected=false;
186 }
187
188
189 Data::Data(const object& value,
190 const FunctionSpace& what,
191 bool expanded)
192 {
193 numeric::array asNumArray(value);
194 initialise(asNumArray,what,expanded);
195 m_protected=false;
196 }
197
198
199 Data::Data(const object& value,
200 const Data& other)
201 {
202 numeric::array asNumArray(value);
203
204 // extract the shape of the numarray
205 DataTypes::ShapeType tempShape=DataTypes::shapeFromNumArray(asNumArray);
206 // /* for (int i=0; i < asNumArray.getrank(); i++) {
207 // tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
208 // }*/
209 // // get the space for the data vector
210 // int len = DataTypes::noValues(tempShape);
211 // DataVector temp_data(len, 0.0, len);
212 // /* DataArrayView temp_dataView(temp_data, tempShape);
213 // temp_dataView.copy(asNumArray);*/
214 // temp_data.copyFromNumArray(asNumArray);
215
216 //
217 // Create DataConstant using the given value and all other parameters
218 // copied from other. If value is a rank 0 object this Data
219 // will assume the point data shape of other.
220
221 if (DataTypes::getRank(tempShape)/*temp_dataView.getRank()*/==0) {
222
223
224 // get the space for the data vector
225 int len1 = DataTypes::noValues(tempShape);
226 DataVector temp_data(len1, 0.0, len1);
227 temp_data.copyFromNumArray(asNumArray);
228
229 int len = DataTypes::noValues(other.getDataPointShape());
230
231 DataVector temp2_data(len, temp_data[0]/*temp_dataView()*/, len);
232 //DataArrayView temp2_dataView(temp2_data, other.getPointDataView().getShape());
233 // initialise(temp2_dataView, other.getFunctionSpace(), false);
234
235 DataConstant* t=new DataConstant(other.getFunctionSpace(),other.getDataPointShape(),temp2_data);
236 // boost::shared_ptr<DataAbstract> sp(t);
237 // m_data=sp;
238 m_data=DataAbstract_ptr(t);
239
240 } else {
241 //
242 // Create a DataConstant with the same sample shape as other
243 // initialise(temp_dataView, other.getFunctionSpace(), false);
244 DataConstant* t=new DataConstant(asNumArray,other.getFunctionSpace());
245 // boost::shared_ptr<DataAbstract> sp(t);
246 // m_data=sp;
247 m_data=DataAbstract_ptr(t);
248 }
249 m_protected=false;
250 }
251
252 Data::~Data()
253 {
254
255 }
256
257
258
259 void
260 Data::initialise(const boost::python::numeric::array& value,
261 const FunctionSpace& what,
262 bool expanded)
263 {
264 //
265 // Construct a Data object of the appropriate type.
266 // Construct the object first as there seems to be a bug which causes
267 // undefined behaviour if an exception is thrown during construction
268 // within the shared_ptr constructor.
269 if (expanded) {
270 DataAbstract* temp=new DataExpanded(value, what);
271 // boost::shared_ptr<DataAbstract> temp_data(temp);
272 // m_data=temp_data;
273 m_data=temp->getPtr();
274 } else {
275 DataAbstract* temp=new DataConstant(value, what);
276 // boost::shared_ptr<DataAbstract> temp_data(temp);
277 // m_data=temp_data;
278 m_data=temp->getPtr();
279 }
280 }
281
282
283 void
284 Data::initialise(const DataTypes::ValueType& value,
285 const DataTypes::ShapeType& shape,
286 const FunctionSpace& what,
287 bool expanded)
288 {
289 //
290 // Construct a Data object of the appropriate type.
291 // Construct the object first as there seems to be a bug which causes
292 // undefined behaviour if an exception is thrown during construction
293 // within the shared_ptr constructor.
294 if (expanded) {
295 DataAbstract* temp=new DataExpanded(what, shape, value);
296 // boost::shared_ptr<DataAbstract> temp_data(temp);
297 // m_data=temp_data;
298 m_data=temp->getPtr();
299 } else {
300 DataAbstract* temp=new DataConstant(what, shape, value);
301 // boost::shared_ptr<DataAbstract> temp_data(temp);
302 // m_data=temp_data;
303 m_data=temp->getPtr();
304 }
305 }
306
307
308 // void
309 // Data::CompareDebug(const Data& rd)
310 // {
311 // using namespace std;
312 // bool mismatch=false;
313 // std::cout << "Comparing left and right" << endl;
314 // const DataTagged* left=dynamic_cast<DataTagged*>(m_data.get());
315 // const DataTagged* right=dynamic_cast<DataTagged*>(rd.m_data.get());
316 //
317 // if (left==0)
318 // {
319 // cout << "left arg is not a DataTagged\n";
320 // return;
321 // }
322 //
323 // if (right==0)
324 // {
325 // cout << "right arg is not a DataTagged\n";
326 // return;
327 // }
328 // cout << "Num elements=" << left->getVector().size() << ":" << right->getVector().size() << std::endl;
329 // cout << "Shapes ";
330 // if (left->getShape()==right->getShape())
331 // {
332 // cout << "ok\n";
333 // }
334 // else
335 // {
336 // cout << "Problem: shapes do not match\n";
337 // mismatch=true;
338 // }
339 // int lim=left->getVector().size();
340 // if (right->getVector().size()) lim=right->getVector().size();
341 // for (int i=0;i<lim;++i)
342 // {
343 // if (left->getVector()[i]!=right->getVector()[i])
344 // {
345 // cout << "[" << i << "] value mismatch " << left->getVector()[i] << ":" << right->getVector()[i] << endl;
346 // mismatch=true;
347 // }
348 // }
349 //
350 // // still need to check the tag map
351 // // also need to watch what is happening to function spaces, are they copied or what?
352 //
353 // const DataTagged::DataMapType& mapleft=left->getTagLookup();
354 // const DataTagged::DataMapType& mapright=right->getTagLookup();
355 //
356 // if (mapleft.size()!=mapright.size())
357 // {
358 // cout << "Maps are different sizes " << mapleft.size() << ":" << mapright.size() << endl;
359 // mismatch=true;
360 // cout << "Left map\n";
361 // DataTagged::DataMapType::const_iterator i,j;
362 // for (i=mapleft.begin();i!=mapleft.end();++i) {
363 // cout << "(" << i->first << "=>" << i->second << ")\n";
364 // }
365 // cout << "Right map\n";
366 // for (i=mapright.begin();i!=mapright.end();++i) {
367 // cout << "(" << i->first << "=>" << i->second << ")\n";
368 // }
369 // cout << "End map\n";
370 //
371 // }
372 //
373 // DataTagged::DataMapType::const_iterator i,j;
374 // for (i=mapleft.begin(),j=mapright.begin();i!=mapleft.end() && j!=mapright.end();++i,++j) {
375 // if ((i->first!=j->first) || (i->second!=j->second))
376 // {
377 // cout << "(" << i->first << "=>" << i->second << ")";
378 // cout << ":(" << j->first << "=>" << j->second << ") ";
379 // mismatch=true;
380 // }
381 // }
382 // if (mismatch)
383 // {
384 // cout << "#Mismatch\n";
385 // }
386 // }
387
388 escriptDataC
389 Data::getDataC()
390 {
391 escriptDataC temp;
392 temp.m_dataPtr=(void*)this;
393 return temp;
394 }
395
396 escriptDataC
397 Data::getDataC() const
398 {
399 escriptDataC temp;
400 temp.m_dataPtr=(void*)this;
401 return temp;
402 }
403
404 const boost::python::tuple
405 Data::getShapeTuple() const
406 {
407 const DataTypes::ShapeType& shape=getDataPointShape();
408 switch(getDataPointRank()) {
409 case 0:
410 return make_tuple();
411 case 1:
412 return make_tuple(long_(shape[0]));
413 case 2:
414 return make_tuple(long_(shape[0]),long_(shape[1]));
415 case 3:
416 return make_tuple(long_(shape[0]),long_(shape[1]),long_(shape[2]));
417 case 4:
418 return make_tuple(long_(shape[0]),long_(shape[1]),long_(shape[2]),long_(shape[3]));
419 default:
420 throw DataException("Error - illegal Data rank.");
421 }
422 }
423
424
425 // The different name is needed because boost has trouble with overloaded functions.
426 // It can't work out what type the function is based soley on its name.
427 // There are ways to fix this involving creating function pointer variables for each form
428 // but there doesn't seem to be a need given that the methods have the same name from the python point of view
429 Data*
430 Data::copySelf()
431 {
432 DataAbstract* temp=m_data->deepCopy();
433 return new Data(temp);
434 }
435
436 void
437 Data::copy(const Data& other)
438 {
439 DataAbstract* temp=other.m_data->deepCopy();
440 DataAbstract_ptr p=temp->getPtr();
441 m_data=p;
442 }
443
444
445 Data
446 Data::delay()
447 {
448 DataLazy* dl=new DataLazy(m_data);
449 return Data(dl);
450 }
451
452 void
453 Data::setToZero()
454 {
455 if (isEmpty())
456 {
457 throw DataException("Error - Operations not permitted on instances of DataEmpty.");
458 }
459 {
460 DataExpanded* temp=dynamic_cast<DataExpanded*>(m_data.get());
461 if (temp!=0) {
462 temp->setToZero();
463 return;
464 }
465 }
466 {
467 DataTagged* temp=dynamic_cast<DataTagged*>(m_data.get());
468 if (temp!=0) {
469 temp->setToZero();
470 return;
471 }
472 }
473 {
474 DataConstant* temp=dynamic_cast<DataConstant*>(m_data.get());
475 if (temp!=0) {
476 temp->setToZero();
477 return;
478 }
479 }
480 throw DataException("Error - Data can not be set to zero.");
481 }
482
483
484 // This implementation of this operation should not need any special treatment of LazyData.
485 void
486 Data::copyWithMask(const Data& other,
487 const Data& mask)
488 {
489 if (other.isEmpty() || mask.isEmpty())
490 {
491 throw DataException("Error - copyWithMask not permitted using instances of DataEmpty.");
492 }
493 Data mask1;
494 Data mask2;
495 mask1 = mask.wherePositive();
496
497 mask2.copy(mask1);
498 mask1 *= other;
499
500 mask2 *= *this;
501 mask2 = *this - mask2;
502 *this = mask1 + mask2;
503 }
504
505 bool
506 Data::isExpanded() const
507 {
508 DataExpanded* temp=dynamic_cast<DataExpanded*>(m_data.get());
509 return (temp!=0);
510 }
511
512 bool
513 Data::isTagged() const
514 {
515 DataTagged* temp=dynamic_cast<DataTagged*>(m_data.get());
516 return (temp!=0);
517 }
518
519 bool
520 Data::isEmpty() const
521 {
522 DataEmpty* temp=dynamic_cast<DataEmpty*>(m_data.get());
523 return (temp!=0);
524 }
525
526 bool
527 Data::isConstant() const
528 {
529 DataConstant* temp=dynamic_cast<DataConstant*>(m_data.get());
530 return (temp!=0);
531 }
532
533 bool
534 Data::isLazy() const
535 {
536 return m_data->isLazy();
537 }
538
539 // at the moment this is synonymous with !isLazy() but that could change
540 bool
541 Data::isReady() const
542 {
543 return (dynamic_cast<DataReady*>(m_data.get())!=0);
544 }
545
546
547 void
548 Data::setProtection()
549 {
550 m_protected=true;
551 }
552
553 bool
554 Data::isProtected() const
555 {
556 return m_protected;
557 }
558
559
560
561 void
562 Data::expand()
563 {
564 if (isConstant()) {
565 DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());
566 DataAbstract* temp=new DataExpanded(*tempDataConst);
567 // shared_ptr<DataAbstract> temp_data(temp);
568 // m_data=temp_data;
569 m_data=temp->getPtr();
570 } else if (isTagged()) {
571 DataTagged* tempDataTag=dynamic_cast<DataTagged*>(m_data.get());
572 DataAbstract* temp=new DataExpanded(*tempDataTag);
573 // shared_ptr<DataAbstract> temp_data(temp);
574 // m_data=temp_data;
575 m_data=temp->getPtr();
576 } else if (isExpanded()) {
577 //
578 // do nothing
579 } else if (isEmpty()) {
580 throw DataException("Error - Expansion of DataEmpty not possible.");
581 } else if (isLazy()) {
582 resolve();
583 expand(); // resolve might not give us expanded data
584 } else {
585 throw DataException("Error - Expansion not implemented for this Data type.");
586 }
587 }
588
589 void
590 Data::tag()
591 {
592 if (isConstant()) {
593 DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());
594 DataAbstract* temp=new DataTagged(*tempDataConst);
595 // shared_ptr<DataAbstract> temp_data(temp);
596 // m_data=temp_data;
597 m_data=temp->getPtr();
598 } else if (isTagged()) {
599 // do nothing
600 } else if (isExpanded()) {
601 throw DataException("Error - Creating tag data from DataExpanded not possible.");
602 } else if (isEmpty()) {
603 throw DataException("Error - Creating tag data from DataEmpty not possible.");
604 } else if (isLazy()) {
605 DataAbstract_ptr res=m_data->resolve();
606 if (m_data->isExpanded())
607 {
608 throw DataException("Error - data would resolve to DataExpanded, tagging is not possible.");
609 }
610 m_data=res;
611 tag();
612 } else {
613 throw DataException("Error - Tagging not implemented for this Data type.");
614 }
615 }
616
617 void
618 Data::resolve()
619 {
620 if (isLazy())
621 {
622 m_data=m_data->resolve();
623 }
624 }
625
626
627 Data
628 Data::oneOver() const
629 {
630 if (isLazy())
631 {
632 DataLazy* c=new DataLazy(borrowDataPtr(),RECIP);
633 return Data(c);
634 }
635 return C_TensorUnaryOperation(*this, bind1st(divides<double>(),1.));
636 }
637
638 Data
639 Data::wherePositive() const
640 {
641 if (isLazy())
642 {
643 DataLazy* c=new DataLazy(borrowDataPtr(),GZ);
644 return Data(c);
645 }
646 return C_TensorUnaryOperation(*this, bind2nd(greater<double>(),0.0));
647 }
648
649 Data
650 Data::whereNegative() const
651 {
652 if (isLazy())
653 {
654 DataLazy* c=new DataLazy(borrowDataPtr(),LZ);
655 return Data(c);
656 }
657 return C_TensorUnaryOperation(*this, bind2nd(less<double>(),0.0));
658 }
659
660 Data
661 Data::whereNonNegative() const
662 {
663 if (isLazy())
664 {
665 DataLazy* c=new DataLazy(borrowDataPtr(),GEZ);
666 return Data(c);
667 }
668 return C_TensorUnaryOperation(*this, bind2nd(greater_equal<double>(),0.0));
669 }
670
671 Data
672 Data::whereNonPositive() const
673 {
674 if (isLazy())
675 {
676 DataLazy* c=new DataLazy(borrowDataPtr(),LEZ);
677 return Data(c);
678 }
679 return C_TensorUnaryOperation(*this, bind2nd(less_equal<double>(),0.0));
680 }
681
682 Data
683 Data::whereZero(double tol) const
684 {
685 Data dataAbs=abs();
686 return C_TensorUnaryOperation(dataAbs, bind2nd(less_equal<double>(),tol));
687 }
688
689 Data
690 Data::whereNonZero(double tol) const
691 {
692 Data dataAbs=abs();
693 return C_TensorUnaryOperation(dataAbs, bind2nd(greater<double>(),tol));
694 }
695
696 Data
697 Data::interpolate(const FunctionSpace& functionspace) const
698 {
699 return Data(*this,functionspace);
700 }
701
702 bool
703 Data::probeInterpolation(const FunctionSpace& functionspace) const
704 {
705 if (getFunctionSpace()==functionspace) {
706 return true;
707 } else {
708 const_Domain_ptr domain=getDomain();
709 if (*domain==*functionspace.getDomain()) {
710 return domain->probeInterpolationOnDomain(getFunctionSpace().getTypeCode(),functionspace.getTypeCode());
711 } else {
712 return domain->probeInterpolationACross(getFunctionSpace().getTypeCode(),*(functionspace.getDomain()),functionspace.getTypeCode());
713 }
714 }
715 }
716
717 Data
718 Data::gradOn(const FunctionSpace& functionspace) const
719 {
720 if (isEmpty())
721 {
722 throw DataException("Error - operation not permitted on instances of DataEmpty.");
723 }
724 double blocktimer_start = blocktimer_time();
725 if (functionspace.getDomain()!=getDomain())
726 throw DataException("Error - gradient cannot be calculated on different domains.");
727 DataTypes::ShapeType grad_shape=getDataPointShape();
728 grad_shape.push_back(functionspace.getDim());
729 Data out(0.0,grad_shape,functionspace,true);
730 getDomain()->setToGradient(out,*this);
731 blocktimer_increment("grad()", blocktimer_start);
732 return out;
733 }
734
735 Data
736 Data::grad() const
737 {
738 if (isEmpty())
739 {
740 throw DataException("Error - operation not permitted on instances of DataEmpty.");
741 }
742 return gradOn(escript::function(*getDomain()));
743 }
744
745 int
746 Data::getDataPointSize() const
747 {
748 return m_data->getNoValues();
749 }
750
751 DataTypes::ValueType::size_type
752 Data::getLength() const
753 {
754 return m_data->getLength();
755 }
756
757 const
758 boost::python::numeric::array
759 Data:: getValueOfDataPoint(int dataPointNo)
760 {
761 size_t length=0;
762 int i, j, k, l;
763
764 FORCERESOLVE;
765
766 //
767 // determine the rank and shape of each data point
768 int dataPointRank = getDataPointRank();
769 const DataTypes::ShapeType& dataPointShape = getDataPointShape();
770
771 //
772 // create the numeric array to be returned
773 boost::python::numeric::array numArray(0.0);
774
775 //
776 // the shape of the returned numeric array will be the same
777 // as that of the data point
778 int arrayRank = dataPointRank;
779 const DataTypes::ShapeType& arrayShape = dataPointShape;
780
781 //
782 // resize the numeric array to the shape just calculated
783 if (arrayRank==0) {
784 numArray.resize(1);
785 }
786 if (arrayRank==1) {
787 numArray.resize(arrayShape[0]);
788 }
789 if (arrayRank==2) {
790 numArray.resize(arrayShape[0],arrayShape[1]);
791 }
792 if (arrayRank==3) {
793 numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2]);
794 }
795 if (arrayRank==4) {
796 numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
797 }
798
799 if (getNumDataPointsPerSample()>0) {
800 int sampleNo = dataPointNo/getNumDataPointsPerSample();
801 int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
802 //
803 // Check a valid sample number has been supplied
804 if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
805 throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
806 }
807
808 //
809 // Check a valid data point number has been supplied
810 if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
811 throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
812 }
813 // TODO: global error handling
814 // create a view of the data if it is stored locally
815 // DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
816 DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
817
818
819 switch( dataPointRank ){
820 case 0 :
821 numArray[0] = getDataAtOffset(offset);
822 break;
823 case 1 :
824 for( i=0; i<dataPointShape[0]; i++ )
825 numArray[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
826 break;
827 case 2 :
828 for( i=0; i<dataPointShape[0]; i++ )
829 for( j=0; j<dataPointShape[1]; j++)
830 numArray[make_tuple(i,j)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
831 break;
832 case 3 :
833 for( i=0; i<dataPointShape[0]; i++ )
834 for( j=0; j<dataPointShape[1]; j++ )
835 for( k=0; k<dataPointShape[2]; k++)
836 numArray[make_tuple(i,j,k)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
837 break;
838 case 4 :
839 for( i=0; i<dataPointShape[0]; i++ )
840 for( j=0; j<dataPointShape[1]; j++ )
841 for( k=0; k<dataPointShape[2]; k++ )
842 for( l=0; l<dataPointShape[3]; l++)
843 numArray[make_tuple(i,j,k,l)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
844 break;
845 }
846 }
847 //
848 // return the array
849 return numArray;
850
851 }
852
853 void
854 Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)
855 {
856 // this will throw if the value cannot be represented
857 boost::python::numeric::array num_array(py_object);
858 setValueOfDataPointToArray(dataPointNo,num_array);
859 }
860
861 void
862 Data::setValueOfDataPointToArray(int dataPointNo, const boost::python::numeric::array& num_array)
863 {
864 if (isProtected()) {
865 throw DataException("Error - attempt to update protected Data object.");
866 }
867 FORCERESOLVE;
868 //
869 // check rank
870 if (num_array.getrank()<getDataPointRank())
871 throw DataException("Rank of numarray does not match Data object rank");
872
873 //
874 // check shape of num_array
875 for (int i=0; i<getDataPointRank(); i++) {
876 if (extract<int>(num_array.getshape()[i])!=getDataPointShape()[i])
877 throw DataException("Shape of numarray does not match Data object rank");
878 }
879 //
880 // make sure data is expanded:
881 if (!isExpanded()) {
882 expand();
883 }
884 if (getNumDataPointsPerSample()>0) {
885 int sampleNo = dataPointNo/getNumDataPointsPerSample();
886 int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
887 m_data->copyToDataPoint(sampleNo, dataPointNoInSample,num_array);
888 } else {
889 m_data->copyToDataPoint(-1, 0,num_array);
890 }
891 }
892
893 void
894 Data::setValueOfDataPoint(int dataPointNo, const double value)
895 {
896 if (isProtected()) {
897 throw DataException("Error - attempt to update protected Data object.");
898 }
899 //
900 // make sure data is expanded:
901 FORCERESOLVE;
902 if (!isExpanded()) {
903 expand();
904 }
905 if (getNumDataPointsPerSample()>0) {
906 int sampleNo = dataPointNo/getNumDataPointsPerSample();
907 int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
908 m_data->copyToDataPoint(sampleNo, dataPointNoInSample,value);
909 } else {
910 m_data->copyToDataPoint(-1, 0,value);
911 }
912 }
913
914 const
915 boost::python::numeric::array
916 Data::getValueOfGlobalDataPoint(int procNo, int dataPointNo)
917 {
918 size_t length=0;
919 int i, j, k, l, pos;
920 FORCERESOLVE;
921 //
922 // determine the rank and shape of each data point
923 int dataPointRank = getDataPointRank();
924 const DataTypes::ShapeType& dataPointShape = getDataPointShape();
925
926 //
927 // create the numeric array to be returned
928 boost::python::numeric::array numArray(0.0);
929
930 //
931 // the shape of the returned numeric array will be the same
932 // as that of the data point
933 int arrayRank = dataPointRank;
934 const DataTypes::ShapeType& arrayShape = dataPointShape;
935
936 //
937 // resize the numeric array to the shape just calculated
938 if (arrayRank==0) {
939 numArray.resize(1);
940 }
941 if (arrayRank==1) {
942 numArray.resize(arrayShape[0]);
943 }
944 if (arrayRank==2) {
945 numArray.resize(arrayShape[0],arrayShape[1]);
946 }
947 if (arrayRank==3) {
948 numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2]);
949 }
950 if (arrayRank==4) {
951 numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
952 }
953
954 // added for the MPI communication
955 length=1;
956 for( i=0; i<arrayRank; i++ ) length *= arrayShape[i];
957 double *tmpData = new double[length];
958
959 //
960 // load the values for the data point into the numeric array.
961
962 // updated for the MPI case
963 if( get_MPIRank()==procNo ){
964 if (getNumDataPointsPerSample()>0) {
965 int sampleNo = dataPointNo/getNumDataPointsPerSample();
966 int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
967 //
968 // Check a valid sample number has been supplied
969 if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
970 throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
971 }
972
973 //
974 // Check a valid data point number has been supplied
975 if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
976 throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
977 }
978 // TODO: global error handling
979 // create a view of the data if it is stored locally
980 //DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
981 DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
982
983 // pack the data from the view into tmpData for MPI communication
984 pos=0;
985 switch( dataPointRank ){
986 case 0 :
987 tmpData[0] = getDataAtOffset(offset);
988 break;
989 case 1 :
990 for( i=0; i<dataPointShape[0]; i++ )
991 tmpData[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
992 break;
993 case 2 :
994 for( i=0; i<dataPointShape[0]; i++ )
995 for( j=0; j<dataPointShape[1]; j++, pos++ )
996 tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
997 break;
998 case 3 :
999 for( i=0; i<dataPointShape[0]; i++ )
1000 for( j=0; j<dataPointShape[1]; j++ )
1001 for( k=0; k<dataPointShape[2]; k++, pos++ )
1002 tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
1003 break;
1004 case 4 :
1005 for( i=0; i<dataPointShape[0]; i++ )
1006 for( j=0; j<dataPointShape[1]; j++ )
1007 for( k=0; k<dataPointShape[2]; k++ )
1008 for( l=0; l<dataPointShape[3]; l++, pos++ )
1009 tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
1010 break;
1011 }
1012 }
1013 }
1014 #ifdef PASO_MPI
1015 // broadcast the data to all other processes
1016 MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );
1017 #endif
1018
1019 // unpack the data
1020 switch( dataPointRank ){
1021 case 0 :
1022 numArray[0]=tmpData[0];
1023 break;
1024 case 1 :
1025 for( i=0; i<dataPointShape[0]; i++ )
1026 numArray[i]=tmpData[i];
1027 break;
1028 case 2 :
1029 for( i=0; i<dataPointShape[0]; i++ )
1030 for( j=0; j<dataPointShape[1]; j++ )
1031 numArray[make_tuple(i,j)]=tmpData[i+j*dataPointShape[0]];
1032 break;
1033 case 3 :
1034 for( i=0; i<dataPointShape[0]; i++ )
1035 for( j=0; j<dataPointShape[1]; j++ )
1036 for( k=0; k<dataPointShape[2]; k++ )
1037 numArray[make_tuple(i,j,k)]=tmpData[i+dataPointShape[0]*(j*+k*dataPointShape[1])];
1038 break;
1039 case 4 :
1040 for( i=0; i<dataPointShape[0]; i++ )
1041 for( j=0; j<dataPointShape[1]; j++ )
1042 for( k=0; k<dataPointShape[2]; k++ )
1043 for( l=0; l<dataPointShape[3]; l++ )
1044 numArray[make_tuple(i,j,k,l)]=tmpData[i+dataPointShape[0]*(j*+dataPointShape[1]*(k+l*dataPointShape[2]))];
1045 break;
1046 }
1047
1048 delete [] tmpData;
1049 //
1050 // return the loaded array
1051 return numArray;
1052 }
1053
1054
1055 boost::python::numeric::array
1056 Data::integrate_const() const
1057 {
1058 if (isLazy())
1059 {
1060 throw DataException("Error - cannot integrate for constant lazy data.");
1061 }
1062 return integrateWorker();
1063 }
1064
1065 boost::python::numeric::array
1066 Data::integrate()
1067 {
1068 if (isLazy())
1069 {
1070 expand();
1071 }
1072 return integrateWorker();
1073 }
1074
1075
1076
1077 boost::python::numeric::array
1078 Data::integrateWorker() const
1079 {
1080 int index;
1081 int rank = getDataPointRank();
1082 DataTypes::ShapeType shape = getDataPointShape();
1083 int dataPointSize = getDataPointSize();
1084
1085 //
1086 // calculate the integral values
1087 vector<double> integrals(dataPointSize);
1088 vector<double> integrals_local(dataPointSize);
1089 #ifdef PASO_MPI
1090 AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals_local,*this);
1091 // Global sum: use an array instead of a vector because elements of array are guaranteed to be contiguous in memory
1092 double *tmp = new double[dataPointSize];
1093 double *tmp_local = new double[dataPointSize];
1094 for (int i=0; i<dataPointSize; i++) { tmp_local[i] = integrals_local[i]; }
1095 MPI_Allreduce( &tmp_local[0], &tmp[0], dataPointSize, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD );
1096 for (int i=0; i<dataPointSize; i++) { integrals[i] = tmp[i]; }
1097 delete[] tmp;
1098 delete[] tmp_local;
1099 #else
1100 AbstractContinuousDomain::asAbstractContinuousDomain(*getDomain()).setToIntegrals(integrals,*this);
1101 #endif
1102
1103 //
1104 // create the numeric array to be returned
1105 // and load the array with the integral values
1106 boost::python::numeric::array bp_array(1.0);
1107 if (rank==0) {
1108 bp_array.resize(1);
1109 index = 0;
1110 bp_array[0] = integrals[index];
1111 }
1112 if (rank==1) {
1113 bp_array.resize(shape[0]);
1114 for (int i=0; i<shape[0]; i++) {
1115 index = i;
1116 bp_array[i] = integrals[index];
1117 }
1118 }
1119 if (rank==2) {
1120 bp_array.resize(shape[0],shape[1]);
1121 for (int i=0; i<shape[0]; i++) {
1122 for (int j=0; j<shape[1]; j++) {
1123 index = i + shape[0] * j;
1124 bp_array[make_tuple(i,j)] = integrals[index];
1125 }
1126 }
1127 }
1128 if (rank==3) {
1129 bp_array.resize(shape[0],shape[1],shape[2]);
1130 for (int i=0; i<shape[0]; i++) {
1131 for (int j=0; j<shape[1]; j++) {
1132 for (int k=0; k<shape[2]; k++) {
1133 index = i + shape[0] * ( j + shape[1] * k );
1134 bp_array[make_tuple(i,j,k)] = integrals[index];
1135 }
1136 }
1137 }
1138 }
1139 if (rank==4) {
1140 bp_array.resize(shape[0],shape[1],shape[2],shape[3]);
1141 for (int i=0; i<shape[0]; i++) {
1142 for (int j=0; j<shape[1]; j++) {
1143 for (int k=0; k<shape[2]; k++) {
1144 for (int l=0; l<shape[3]; l++) {
1145 index = i + shape[0] * ( j + shape[1] * ( k + shape[2] * l ) );
1146 bp_array[make_tuple(i,j,k,l)] = integrals[index];
1147 }
1148 }
1149 }
1150 }
1151 }
1152
1153 //
1154 // return the loaded array
1155 return bp_array;
1156 }
1157
1158 Data
1159 Data::sin() const
1160 {
1161 if (isLazy())
1162 {
1163 DataLazy* c=new DataLazy(borrowDataPtr(),SIN);
1164 return Data(c);
1165 }
1166 return C_TensorUnaryOperation<double (*)(double)>(*this, ::sin);
1167 }
1168
1169 Data
1170 Data::cos() const
1171 {
1172 if (isLazy())
1173 {
1174 DataLazy* c=new DataLazy(borrowDataPtr(),COS);
1175 return Data(c);
1176 }
1177 return C_TensorUnaryOperation<double (*)(double)>(*this, ::cos);
1178 }
1179
1180 Data
1181 Data::tan() const
1182 {
1183 if (isLazy())
1184 {
1185 DataLazy* c=new DataLazy(borrowDataPtr(),TAN);
1186 return Data(c);
1187 }
1188 return C_TensorUnaryOperation<double (*)(double)>(*this, ::tan);
1189 }
1190
1191 Data
1192 Data::asin() const
1193 {
1194 if (isLazy())
1195 {
1196 DataLazy* c=new DataLazy(borrowDataPtr(),ASIN);
1197 return Data(c);
1198 }
1199 return C_TensorUnaryOperation<double (*)(double)>(*this, ::asin);
1200 }
1201
1202 Data
1203 Data::acos() const
1204 {
1205 if (isLazy())
1206 {
1207 DataLazy* c=new DataLazy(borrowDataPtr(),ACOS);
1208 return Data(c);
1209 }
1210 return C_TensorUnaryOperation<double (*)(double)>(*this, ::acos);
1211 }
1212
1213
1214 Data
1215 Data::atan() const
1216 {
1217 if (isLazy())
1218 {
1219 DataLazy* c=new DataLazy(borrowDataPtr(),ATAN);
1220 return Data(c);
1221 }
1222 return C_TensorUnaryOperation<double (*)(double)>(*this, ::atan);
1223 }
1224
1225 Data
1226 Data::sinh() const
1227 {
1228 if (isLazy())
1229 {
1230 DataLazy* c=new DataLazy(borrowDataPtr(),SINH);
1231 return Data(c);
1232 }
1233 return C_TensorUnaryOperation<double (*)(double)>(*this, ::sinh);
1234 }
1235
1236 Data
1237 Data::cosh() const
1238 {
1239 if (isLazy())
1240 {
1241 DataLazy* c=new DataLazy(borrowDataPtr(),COSH);
1242 return Data(c);
1243 }
1244 return C_TensorUnaryOperation<double (*)(double)>(*this, ::cosh);
1245 }
1246
1247 Data
1248 Data::tanh() const
1249 {
1250 if (isLazy())
1251 {
1252 DataLazy* c=new DataLazy(borrowDataPtr(),TANH);
1253 return Data(c);
1254 }
1255 return C_TensorUnaryOperation<double (*)(double)>(*this, ::tanh);
1256 }
1257
1258
1259 Data
1260 Data::erf() const
1261 {
1262 #ifdef _WIN32
1263 throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");
1264 #else
1265 if (isLazy())
1266 {
1267 DataLazy* c=new DataLazy(borrowDataPtr(),ERF);
1268 return Data(c);
1269 }
1270 return C_TensorUnaryOperation(*this, ::erf);
1271 #endif
1272 }
1273
1274 Data
1275 Data::asinh() const
1276 {
1277 if (isLazy())
1278 {
1279 DataLazy* c=new DataLazy(borrowDataPtr(),ASINH);
1280 return Data(c);
1281 }
1282 #ifdef _WIN32
1283 return C_TensorUnaryOperation(*this, escript::asinh_substitute);
1284 #else
1285 return C_TensorUnaryOperation(*this, ::asinh);
1286 #endif
1287 }
1288
1289 Data
1290 Data::acosh() const
1291 {
1292 if (isLazy())
1293 {
1294 DataLazy* c=new DataLazy(borrowDataPtr(),ACOSH);
1295 return Data(c);
1296 }
1297 #ifdef _WIN32
1298 return C_TensorUnaryOperation(*this, escript::acosh_substitute);
1299 #else
1300 return C_TensorUnaryOperation(*this, ::acosh);
1301 #endif
1302 }
1303
1304 Data
1305 Data::atanh() const
1306 {
1307 if (isLazy())
1308 {
1309 DataLazy* c=new DataLazy(borrowDataPtr(),ATANH);
1310 return Data(c);
1311 }
1312 #ifdef _WIN32
1313 return C_TensorUnaryOperation(*this, escript::atanh_substitute);
1314 #else
1315 return C_TensorUnaryOperation(*this, ::atanh);
1316 #endif
1317 }
1318
1319 Data
1320 Data::log10() const
1321 { if (isLazy())
1322 {
1323 DataLazy* c=new DataLazy(borrowDataPtr(),LOG10);
1324 return Data(c);
1325 }
1326 return C_TensorUnaryOperation<double (*)(double)>(*this, ::log10);
1327 }
1328
1329 Data
1330 Data::log() const
1331 {
1332 if (isLazy())
1333 {
1334 DataLazy* c=new DataLazy(borrowDataPtr(),LOG);
1335 return Data(c);
1336 }
1337 return C_TensorUnaryOperation<double (*)(double)>(*this, ::log);
1338 }
1339
1340 Data
1341 Data::sign() const
1342 {
1343 if (isLazy())
1344 {
1345 DataLazy* c=new DataLazy(borrowDataPtr(),SIGN);
1346 return Data(c);
1347 }
1348 return C_TensorUnaryOperation(*this, escript::fsign);
1349 }
1350
1351 Data
1352 Data::abs() const
1353 {
1354 if (isLazy())
1355 {
1356 DataLazy* c=new DataLazy(borrowDataPtr(),ABS);
1357 return Data(c);
1358 }
1359 return C_TensorUnaryOperation<double (*)(double)>(*this, ::fabs);
1360 }
1361
1362 Data
1363 Data::neg() const
1364 {
1365 if (isLazy())
1366 {
1367 DataLazy* c=new DataLazy(borrowDataPtr(),NEG);
1368 return Data(c);
1369 }
1370 return C_TensorUnaryOperation(*this, negate<double>());
1371 }
1372
1373 Data
1374 Data::pos() const
1375 {
1376 // not doing lazy check here is deliberate.
1377 // since a deep copy of lazy data should be cheap, I'll just let it happen now
1378 Data result;
1379 // perform a deep copy
1380 result.copy(*this);
1381 return result;
1382 }
1383
1384 Data
1385 Data::exp() const
1386 {
1387 if (isLazy())
1388 {
1389 DataLazy* c=new DataLazy(borrowDataPtr(),EXP);
1390 return Data(c);
1391 }
1392 return C_TensorUnaryOperation<double (*)(double)>(*this, ::exp);
1393 }
1394
1395 Data
1396 Data::sqrt() const
1397 {
1398 if (isLazy())
1399 {
1400 DataLazy* c=new DataLazy(borrowDataPtr(),SQRT);
1401 return Data(c);
1402 }
1403 return C_TensorUnaryOperation<double (*)(double)>(*this, ::sqrt);
1404 }
1405
1406 double
1407 Data::Lsup_const() const
1408 {
1409 if (isLazy())
1410 {
1411 throw DataException("Error - cannot compute Lsup for constant lazy data.");
1412 }
1413 return LsupWorker();
1414 }
1415
1416 double
1417 Data::Lsup()
1418 {
1419 if (isLazy())
1420 {
1421 expand();
1422 }
1423 return LsupWorker();
1424 }
1425
1426 double
1427 Data::sup_const() const
1428 {
1429 if (isLazy())
1430 {
1431 throw DataException("Error - cannot compute sup for constant lazy data.");
1432 }
1433 return supWorker();
1434 }
1435
1436 double
1437 Data::sup()
1438 {
1439 if (isLazy())
1440 {
1441 expand();
1442 }
1443 return supWorker();
1444 }
1445
1446 double
1447 Data::inf_const() const
1448 {
1449 if (isLazy())
1450 {
1451 throw DataException("Error - cannot compute inf for constant lazy data.");
1452 }
1453 return infWorker();
1454 }
1455
1456 double
1457 Data::inf()
1458 {
1459 if (isLazy())
1460 {
1461 expand();
1462 }
1463 return infWorker();
1464 }
1465
1466 double
1467 Data::LsupWorker() const
1468 {
1469 double localValue;
1470 //
1471 // set the initial absolute maximum value to zero
1472
1473 AbsMax abs_max_func;
1474 localValue = algorithm(abs_max_func,0);
1475 #ifdef PASO_MPI
1476 double globalValue;
1477 MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1478 return globalValue;
1479 #else
1480 return localValue;
1481 #endif
1482 }
1483
1484 double
1485 Data::supWorker() const
1486 {
1487 double localValue;
1488 //
1489 // set the initial maximum value to min possible double
1490 FMax fmax_func;
1491 localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);
1492 #ifdef PASO_MPI
1493 double globalValue;
1494 MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1495 return globalValue;
1496 #else
1497 return localValue;
1498 #endif
1499 }
1500
1501 double
1502 Data::infWorker() const
1503 {
1504 double localValue;
1505 //
1506 // set the initial minimum value to max possible double
1507 FMin fmin_func;
1508 localValue = algorithm(fmin_func,numeric_limits<double>::max());
1509 #ifdef PASO_MPI
1510 double globalValue;
1511 MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );
1512 return globalValue;
1513 #else
1514 return localValue;
1515 #endif
1516 }
1517
1518 /* TODO */
1519 /* global reduction */
1520 Data
1521 Data::maxval() const
1522 {
1523 //
1524 // set the initial maximum value to min possible double
1525 FMax fmax_func;
1526 return dp_algorithm(fmax_func,numeric_limits<double>::max()*-1);
1527 }
1528
1529 Data
1530 Data::minval() const
1531 {
1532 //
1533 // set the initial minimum value to max possible double
1534 FMin fmin_func;
1535 return dp_algorithm(fmin_func,numeric_limits<double>::max());
1536 }
1537
1538 Data
1539 Data::swapaxes(const int axis0, const int axis1) const
1540 {
1541 int axis0_tmp,axis1_tmp;
1542 DataTypes::ShapeType s=getDataPointShape();
1543 DataTypes::ShapeType ev_shape;
1544 // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1545 // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1546 int rank=getDataPointRank();
1547 if (rank<2) {
1548 throw DataException("Error - Data::swapaxes argument must have at least rank 2.");
1549 }
1550 if (axis0<0 || axis0>rank-1) {
1551 throw DataException("Error - Data::swapaxes: axis0 must be between 0 and rank-1=" + rank-1);
1552 }
1553 if (axis1<0 || axis1>rank-1) {
1554 throw DataException("Error - Data::swapaxes: axis1 must be between 0 and rank-1=" + rank-1);
1555 }
1556 if (axis0 == axis1) {
1557 throw DataException("Error - Data::swapaxes: axis indices must be different.");
1558 }
1559 if (axis0 > axis1) {
1560 axis0_tmp=axis1;
1561 axis1_tmp=axis0;
1562 } else {
1563 axis0_tmp=axis0;
1564 axis1_tmp=axis1;
1565 }
1566 for (int i=0; i<rank; i++) {
1567 if (i == axis0_tmp) {
1568 ev_shape.push_back(s[axis1_tmp]);
1569 } else if (i == axis1_tmp) {
1570 ev_shape.push_back(s[axis0_tmp]);
1571 } else {
1572 ev_shape.push_back(s[i]);
1573 }
1574 }
1575 Data ev(0.,ev_shape,getFunctionSpace());
1576 ev.typeMatchRight(*this);
1577 m_data->swapaxes(ev.m_data.get(), axis0_tmp, axis1_tmp);
1578 return ev;
1579
1580 }
1581
1582 Data
1583 Data::symmetric() const
1584 {
1585 // check input
1586 DataTypes::ShapeType s=getDataPointShape();
1587 if (getDataPointRank()==2) {
1588 if(s[0] != s[1])
1589 throw DataException("Error - Data::symmetric can only be calculated for rank 2 object with equal first and second dimension.");
1590 }
1591 else if (getDataPointRank()==4) {
1592 if(!(s[0] == s[2] && s[1] == s[3]))
1593 throw DataException("Error - Data::symmetric can only be calculated for rank 4 object with dim0==dim2 and dim1==dim3.");
1594 }
1595 else {
1596 throw DataException("Error - Data::symmetric can only be calculated for rank 2 or 4 object.");
1597 }
1598 Data ev(0.,getDataPointShape(),getFunctionSpace());
1599 ev.typeMatchRight(*this);
1600 m_data->symmetric(ev.m_data.get());
1601 return ev;
1602 }
1603
1604 Data
1605 Data::nonsymmetric() const
1606 {
1607 // check input
1608 DataTypes::ShapeType s=getDataPointShape();
1609 if (getDataPointRank()==2) {
1610 if(s[0] != s[1])
1611 throw DataException("Error - Data::nonsymmetric can only be calculated for rank 2 object with equal first and second dimension.");
1612 DataTypes::ShapeType ev_shape;
1613 ev_shape.push_back(s[0]);
1614 ev_shape.push_back(s[1]);
1615 Data ev(0.,ev_shape,getFunctionSpace());
1616 ev.typeMatchRight(*this);
1617 m_data->nonsymmetric(ev.m_data.get());
1618 return ev;
1619 }
1620 else if (getDataPointRank()==4) {
1621 if(!(s[0] == s[2] && s[1] == s[3]))
1622 throw DataException("Error - Data::nonsymmetric can only be calculated for rank 4 object with dim0==dim2 and dim1==dim3.");
1623 DataTypes::ShapeType ev_shape;
1624 ev_shape.push_back(s[0]);
1625 ev_shape.push_back(s[1]);
1626 ev_shape.push_back(s[2]);
1627 ev_shape.push_back(s[3]);
1628 Data ev(0.,ev_shape,getFunctionSpace());
1629 ev.typeMatchRight(*this);
1630 m_data->nonsymmetric(ev.m_data.get());
1631 return ev;
1632 }
1633 else {
1634 throw DataException("Error - Data::nonsymmetric can only be calculated for rank 2 or 4 object.");
1635 }
1636 }
1637
1638 Data
1639 Data::trace(int axis_offset) const
1640 {
1641 DataTypes::ShapeType s=getDataPointShape();
1642 if (getDataPointRank()==2) {
1643 DataTypes::ShapeType ev_shape;
1644 Data ev(0.,ev_shape,getFunctionSpace());
1645 ev.typeMatchRight(*this);
1646 m_data->trace(ev.m_data.get(), axis_offset);
1647 return ev;
1648 }
1649 if (getDataPointRank()==3) {
1650 DataTypes::ShapeType ev_shape;
1651 if (axis_offset==0) {
1652 int s2=s[2];
1653 ev_shape.push_back(s2);
1654 }
1655 else if (axis_offset==1) {
1656 int s0=s[0];
1657 ev_shape.push_back(s0);
1658 }
1659 Data ev(0.,ev_shape,getFunctionSpace());
1660 ev.typeMatchRight(*this);
1661 m_data->trace(ev.m_data.get(), axis_offset);
1662 return ev;
1663 }
1664 if (getDataPointRank()==4) {
1665 DataTypes::ShapeType ev_shape;
1666 if (axis_offset==0) {
1667 ev_shape.push_back(s[2]);
1668 ev_shape.push_back(s[3]);
1669 }
1670 else if (axis_offset==1) {
1671 ev_shape.push_back(s[0]);
1672 ev_shape.push_back(s[3]);
1673 }
1674 else if (axis_offset==2) {
1675 ev_shape.push_back(s[0]);
1676 ev_shape.push_back(s[1]);
1677 }
1678 Data ev(0.,ev_shape,getFunctionSpace());
1679 ev.typeMatchRight(*this);
1680 m_data->trace(ev.m_data.get(), axis_offset);
1681 return ev;
1682 }
1683 else {
1684 throw DataException("Error - Data::trace can only be calculated for rank 2, 3 or 4 object.");
1685 }
1686 }
1687
1688 Data
1689 Data::transpose(int axis_offset) const
1690 {
1691 DataTypes::ShapeType s=getDataPointShape();
1692 DataTypes::ShapeType ev_shape;
1693 // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1694 // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1695 int rank=getDataPointRank();
1696 if (axis_offset<0 || axis_offset>rank) {
1697 throw DataException("Error - Data::transpose must have 0 <= axis_offset <= rank=" + rank);
1698 }
1699 for (int i=0; i<rank; i++) {
1700 int index = (axis_offset+i)%rank;
1701 ev_shape.push_back(s[index]); // Append to new shape
1702 }
1703 Data ev(0.,ev_shape,getFunctionSpace());
1704 ev.typeMatchRight(*this);
1705 m_data->transpose(ev.m_data.get(), axis_offset);
1706 return ev;
1707 }
1708
1709 Data
1710 Data::eigenvalues() const
1711 {
1712 // check input
1713 DataTypes::ShapeType s=getDataPointShape();
1714 if (getDataPointRank()!=2)
1715 throw DataException("Error - Data::eigenvalues can only be calculated for rank 2 object.");
1716 if(s[0] != s[1])
1717 throw DataException("Error - Data::eigenvalues can only be calculated for object with equal first and second dimension.");
1718 // create return
1719 DataTypes::ShapeType ev_shape(1,s[0]);
1720 Data ev(0.,ev_shape,getFunctionSpace());
1721 ev.typeMatchRight(*this);
1722 m_data->eigenvalues(ev.m_data.get());
1723 return ev;
1724 }
1725
1726 const boost::python::tuple
1727 Data::eigenvalues_and_eigenvectors(const double tol) const
1728 {
1729 DataTypes::ShapeType s=getDataPointShape();
1730 if (getDataPointRank()!=2)
1731 throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for rank 2 object.");
1732 if(s[0] != s[1])
1733 throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for object with equal first and second dimension.");
1734 // create return
1735 DataTypes::ShapeType ev_shape(1,s[0]);
1736 Data ev(0.,ev_shape,getFunctionSpace());
1737 ev.typeMatchRight(*this);
1738 DataTypes::ShapeType V_shape(2,s[0]);
1739 Data V(0.,V_shape,getFunctionSpace());
1740 V.typeMatchRight(*this);
1741 m_data->eigenvalues_and_eigenvectors(ev.m_data.get(),V.m_data.get(),tol);
1742 return make_tuple(boost::python::object(ev),boost::python::object(V));
1743 }
1744
1745 const boost::python::tuple
1746 Data::minGlobalDataPoint() const
1747 {
1748 // NB: calc_minGlobalDataPoint( had to be split off from minGlobalDataPoint( as boost::make_tuple causes an
1749 // abort (for unknown reasons) if there are openmp directives with it in the
1750 // surrounding function
1751
1752 int DataPointNo;
1753 int ProcNo;
1754 calc_minGlobalDataPoint(ProcNo,DataPointNo);
1755 return make_tuple(ProcNo,DataPointNo);
1756 }
1757
1758 void
1759 Data::calc_minGlobalDataPoint(int& ProcNo,
1760 int& DataPointNo) const
1761 {
1762 int i,j;
1763 int lowi=0,lowj=0;
1764 double min=numeric_limits<double>::max();
1765
1766 Data temp=minval();
1767
1768 int numSamples=temp.getNumSamples();
1769 int numDPPSample=temp.getNumDataPointsPerSample();
1770
1771 double next,local_min;
1772 int local_lowi,local_lowj;
1773
1774 #pragma omp parallel private(next,local_min,local_lowi,local_lowj)
1775 {
1776 local_min=min;
1777 #pragma omp for private(i,j) schedule(static)
1778 for (i=0; i<numSamples; i++) {
1779 for (j=0; j<numDPPSample; j++) {
1780 next=temp.getDataAtOffset(temp.getDataOffset(i,j));
1781 if (next<local_min) {
1782 local_min=next;
1783 local_lowi=i;
1784 local_lowj=j;
1785 }
1786 }
1787 }
1788 #pragma omp critical
1789 if (local_min<min) {
1790 min=local_min;
1791 lowi=local_lowi;
1792 lowj=local_lowj;
1793 }
1794 }
1795
1796 #ifdef PASO_MPI
1797 // determine the processor on which the minimum occurs
1798 next = temp.getDataPoint(lowi,lowj);
1799 int lowProc = 0;
1800 double *globalMins = new double[get_MPISize()+1];
1801 int error = MPI_Gather ( &next, 1, MPI_DOUBLE, globalMins, 1, MPI_DOUBLE, 0, get_MPIComm() );
1802
1803 if( get_MPIRank()==0 ){
1804 next = globalMins[lowProc];
1805 for( i=1; i<get_MPISize(); i++ )
1806 if( next>globalMins[i] ){
1807 lowProc = i;
1808 next = globalMins[i];
1809 }
1810 }
1811 MPI_Bcast( &lowProc, 1, MPI_DOUBLE, 0, get_MPIComm() );
1812
1813 delete [] globalMins;
1814 ProcNo = lowProc;
1815 #else
1816 ProcNo = 0;
1817 #endif
1818 DataPointNo = lowj + lowi * numDPPSample;
1819 }
1820
1821 void
1822 Data::saveDX(std::string fileName) const
1823 {
1824 if (isEmpty())
1825 {
1826 throw DataException("Error - Operations not permitted on instances of DataEmpty.");
1827 }
1828 boost::python::dict args;
1829 args["data"]=boost::python::object(this);
1830 getDomain()->saveDX(fileName,args);
1831 return;
1832 }
1833
1834 void
1835 Data::saveVTK(std::string fileName) const
1836 {
1837 if (isEmpty())
1838 {
1839 throw DataException("Error - Operations not permitted on instances of DataEmpty.");
1840 }
1841 boost::python::dict args;
1842 args["data"]=boost::python::object(this);
1843 getDomain()->saveVTK(fileName,args);
1844 return;
1845 }
1846
1847 Data&
1848 Data::operator+=(const Data& right)
1849 {
1850 if (isProtected()) {
1851 throw DataException("Error - attempt to update protected Data object.");
1852 }
1853 if (isLazy() || right.isLazy())
1854 {
1855 DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),ADD); // for lazy + is equivalent to +=
1856 m_data=c->getPtr();
1857 return (*this);
1858 }
1859 else
1860 {
1861 binaryOp(right,plus<double>());
1862 return (*this);
1863 }
1864 }
1865
1866 Data&
1867 Data::operator+=(const boost::python::object& right)
1868 {
1869 if (isProtected()) {
1870 throw DataException("Error - attempt to update protected Data object.");
1871 }
1872 Data tmp(right,getFunctionSpace(),false);
1873 if (isLazy())
1874 {
1875 DataLazy* c=new DataLazy(m_data,tmp.borrowDataPtr(),ADD); // for lazy + is equivalent to +=
1876 m_data=c->getPtr();
1877 return (*this);
1878 }
1879 else
1880 {
1881 binaryOp(tmp,plus<double>());
1882 return (*this);
1883 }
1884 }
1885
1886 // Hmmm, operator= makes a deep copy but the copy constructor does not?
1887 Data&
1888 Data::operator=(const Data& other)
1889 {
1890 copy(other);
1891 return (*this);
1892 }
1893
1894 Data&
1895 Data::operator-=(const Data& right)
1896 {
1897 if (isProtected()) {
1898 throw DataException("Error - attempt to update protected Data object.");
1899 }
1900 if (isLazy() || right.isLazy())
1901 {
1902 DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),SUB); // for lazy - is equivalent to -=
1903 m_data=c->getPtr();
1904 return (*this);
1905 }
1906 else
1907 {
1908 binaryOp(right,minus<double>());
1909 return (*this);
1910 }
1911 }
1912
1913 Data&
1914 Data::operator-=(const boost::python::object& right)
1915 {
1916 if (isProtected()) {
1917 throw DataException("Error - attempt to update protected Data object.");
1918 }
1919 Data tmp(right,getFunctionSpace(),false);
1920 if (isLazy())
1921 {
1922 DataLazy* c=new DataLazy(m_data,tmp.borrowDataPtr(),SUB); // for lazy - is equivalent to -=
1923 m_data=c->getPtr();
1924 return (*this);
1925 }
1926 else
1927 {
1928 binaryOp(tmp,minus<double>());
1929 return (*this);
1930 }
1931 }
1932
1933 Data&
1934 Data::operator*=(const Data& right)
1935 {
1936 if (isProtected()) {
1937 throw DataException("Error - attempt to update protected Data object.");
1938 }
1939 if (isLazy() || right.isLazy())
1940 {
1941 DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),MUL); // for lazy * is equivalent to *=
1942 m_data=c->getPtr();
1943 return (*this);
1944 }
1945 else
1946 {
1947 binaryOp(right,multiplies<double>());
1948 return (*this);
1949 }
1950 }
1951
1952 Data&
1953 Data::operator*=(const boost::python::object& right)
1954 {
1955 if (isProtected()) {
1956 throw DataException("Error - attempt to update protected Data object.");
1957 }
1958 Data tmp(right,getFunctionSpace(),false);
1959 if (isLazy())
1960 {
1961 DataLazy* c=new DataLazy(m_data,tmp.borrowDataPtr(),MUL); // for lazy * is equivalent to *=
1962 m_data=c->getPtr();
1963 return (*this);
1964 }
1965 else
1966 {
1967 binaryOp(tmp,multiplies<double>());
1968 return (*this);
1969 }
1970 }
1971
1972 Data&
1973 Data::operator/=(const Data& right)
1974 {
1975 if (isProtected()) {
1976 throw DataException("Error - attempt to update protected Data object.");
1977 }
1978 if (isLazy() || right.isLazy())
1979 {
1980 DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),DIV); // for lazy / is equivalent to /=
1981 m_data=c->getPtr();
1982 return (*this);
1983 }
1984 else
1985 {
1986 binaryOp(right,divides<double>());
1987 return (*this);
1988 }
1989 }
1990
1991 Data&
1992 Data::operator/=(const boost::python::object& right)
1993 {
1994 if (isProtected()) {
1995 throw DataException("Error - attempt to update protected Data object.");
1996 }
1997 Data tmp(right,getFunctionSpace(),false);
1998 if (isLazy())
1999 {
2000 DataLazy* c=new DataLazy(m_data,tmp.borrowDataPtr(),DIV); // for lazy / is equivalent to /=
2001 m_data=c->getPtr();
2002 return (*this);
2003 }
2004 else
2005 {
2006 binaryOp(tmp,divides<double>());
2007 return (*this);
2008 }
2009 }
2010
2011 Data
2012 Data::rpowO(const boost::python::object& left) const
2013 {
2014 Data left_d(left,*this);
2015 return left_d.powD(*this);
2016 }
2017
2018 Data
2019 Data::powO(const boost::python::object& right) const
2020 {
2021 Data tmp(right,getFunctionSpace(),false);
2022 return powD(tmp);
2023 }
2024
2025 Data
2026 Data::powD(const Data& right) const
2027 {
2028 return C_TensorBinaryOperation<double (*)(double, double)>(*this, right, ::pow);
2029 }
2030
2031 //
2032 // NOTE: It is essential to specify the namespace this operator belongs to
2033 Data
2034 escript::operator+(const Data& left, const Data& right)
2035 {
2036 if (left.isLazy() || right.isLazy())
2037 {
2038 DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),ADD);
2039 return Data(c);
2040 }
2041 return C_TensorBinaryOperation(left, right, plus<double>());
2042 }
2043
2044 //
2045 // NOTE: It is essential to specify the namespace this operator belongs to
2046 Data
2047 escript::operator-(const Data& left, const Data& right)
2048 {
2049 if (left.isLazy() || right.isLazy())
2050 {
2051 DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),SUB);
2052 return Data(c);
2053 }
2054 return C_TensorBinaryOperation(left, right, minus<double>());
2055 }
2056
2057 //
2058 // NOTE: It is essential to specify the namespace this operator belongs to
2059 Data
2060 escript::operator*(const Data& left, const Data& right)
2061 {
2062 if (left.isLazy() || right.isLazy())
2063 {
2064 DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),MUL);
2065 return Data(c);
2066 }
2067 return C_TensorBinaryOperation(left, right, multiplies<double>());
2068 }
2069
2070 //
2071 // NOTE: It is essential to specify the namespace this operator belongs to
2072 Data
2073 escript::operator/(const Data& left, const Data& right)
2074 {
2075 if (left.isLazy() || right.isLazy())
2076 {
2077 DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),DIV);
2078 return Data(c);
2079 }
2080 return C_TensorBinaryOperation(left, right, divides<double>());
2081 }
2082
2083 //
2084 // NOTE: It is essential to specify the namespace this operator belongs to
2085 Data
2086 escript::operator+(const Data& left, const boost::python::object& right)
2087 {
2088 if (left.isLazy())
2089 {
2090 DataLazy* c=new DataLazy(left.borrowDataPtr(),Data(right,left.getFunctionSpace(),false).borrowDataPtr(),ADD);
2091 return Data(c);
2092 }
2093 return left+Data(right,left.getFunctionSpace(),false);
2094 }
2095
2096 //
2097 // NOTE: It is essential to specify the namespace this operator belongs to
2098 Data
2099 escript::operator-(const Data& left, const boost::python::object& right)
2100 {
2101 if (left.isLazy())
2102 {
2103 DataLazy* c=new DataLazy(left.borrowDataPtr(),Data(right,left.getFunctionSpace(),false).borrowDataPtr(),SUB);
2104 return Data(c);
2105 }
2106 return left-Data(right,left.getFunctionSpace(),false);
2107 }
2108
2109 //
2110 // NOTE: It is essential to specify the namespace this operator belongs to
2111 Data
2112 escript::operator*(const Data& left, const boost::python::object& right)
2113 {
2114 if (left.isLazy())
2115 {
2116 DataLazy* c=new DataLazy(left.borrowDataPtr(),Data(right,left.getFunctionSpace(),false).borrowDataPtr(),MUL);
2117 return Data(c);
2118 }
2119 return left*Data(right,left.getFunctionSpace(),false);
2120 }
2121
2122 //
2123 // NOTE: It is essential to specify the namespace this operator belongs to
2124 Data
2125 escript::operator/(const Data& left, const boost::python::object& right)
2126 {
2127 if (left.isLazy())
2128 {
2129 DataLazy* c=new DataLazy(left.borrowDataPtr(),Data(right,left.getFunctionSpace(),false).borrowDataPtr(),DIV);
2130 return Data(c);
2131 }
2132 return left/Data(right,left.getFunctionSpace(),false);
2133 }
2134
2135 //
2136 // NOTE: It is essential to specify the namespace this operator belongs to
2137 Data
2138 escript::operator+(const boost::python::object& left, const Data& right)
2139 {
2140 if (right.isLazy())
2141 {
2142 DataLazy* c=new DataLazy(Data(left,right.getFunctionSpace(),false).borrowDataPtr(),right.borrowDataPtr(),ADD);
2143 return Data(c);
2144 }
2145 return Data(left,right.getFunctionSpace(),false)+right;
2146 }
2147
2148 //
2149 // NOTE: It is essential to specify the namespace this operator belongs to
2150 Data
2151 escript::operator-(const boost::python::object& left, const Data& right)
2152 {
2153 if (right.isLazy())
2154 {
2155 DataLazy* c=new DataLazy(Data(left,right.getFunctionSpace(),false).borrowDataPtr(),right.borrowDataPtr(),SUB);
2156 return Data(c);
2157 }
2158 return Data(left,right.getFunctionSpace(),false)-right;
2159 }
2160
2161 //
2162 // NOTE: It is essential to specify the namespace this operator belongs to
2163 Data
2164 escript::operator*(const boost::python::object& left, const Data& right)
2165 {
2166 if (right.isLazy())
2167 {
2168 DataLazy* c=new DataLazy(Data(left,right.getFunctionSpace(),false).borrowDataPtr(),right.borrowDataPtr(),MUL);
2169 return Data(c);
2170 }
2171 return Data(left,right.getFunctionSpace(),false)*right;
2172 }
2173
2174 //
2175 // NOTE: It is essential to specify the namespace this operator belongs to
2176 Data
2177 escript::operator/(const boost::python::object& left, const Data& right)
2178 {
2179 if (right.isLazy())
2180 {
2181 DataLazy* c=new DataLazy(Data(left,right.getFunctionSpace(),false).borrowDataPtr(),right.borrowDataPtr(),DIV);
2182 return Data(c);
2183 }
2184 return Data(left,right.getFunctionSpace(),false)/right;
2185 }
2186
2187
2188 /* TODO */
2189 /* global reduction */
2190 Data
2191 Data::getItem(const boost::python::object& key) const
2192 {
2193 // const DataArrayView& view=getPointDataView();
2194
2195 DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
2196
2197 if (slice_region.size()!=getDataPointRank()) {
2198 throw DataException("Error - slice size does not match Data rank.");
2199 }
2200
2201 return getSlice(slice_region);
2202 }
2203
2204 /* TODO */
2205 /* global reduction */
2206 Data
2207 Data::getSlice(const DataTypes::RegionType& region) const
2208 {
2209 return Data(*this,region);
2210 }
2211
2212 /* TODO */
2213 /* global reduction */
2214 void
2215 Data::setItemO(const boost::python::object& key,
2216 const boost::python::object& value)
2217 {
2218 Data tempData(value,getFunctionSpace());
2219 setItemD(key,tempData);
2220 }
2221
2222 void
2223 Data::setItemD(const boost::python::object& key,
2224 const Data& value)
2225 {
2226 // const DataArrayView& view=getPointDataView();
2227
2228 DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
2229 if (slice_region.size()!=getDataPointRank()) {
2230 throw DataException("Error - slice size does not match Data rank.");
2231 }
2232 if (getFunctionSpace()!=value.getFunctionSpace()) {
2233 setSlice(Data(value,getFunctionSpace()),slice_region);
2234 } else {
2235 setSlice(value,slice_region);
2236 }
2237 }
2238
2239 void
2240 Data::setSlice(const Data& value,
2241 const DataTypes::RegionType& region)
2242 {
2243 if (isProtected()) {
2244 throw DataException("Error - attempt to update protected Data object.");
2245 }
2246 if (isLazy())
2247 {
2248 throw DataException("Error - setSlice not permitted on lazy data.");
2249 }
2250 Data tempValue(value);
2251 typeMatchLeft(tempValue);
2252 typeMatchRight(tempValue);
2253 getReady()->setSlice(tempValue.m_data.get(),region);
2254 }
2255
2256 void
2257 Data::typeMatchLeft(Data& right) const
2258 {
2259 if (isExpanded()){
2260 right.expand();
2261 } else if (isTagged()) {
2262 if (right.isConstant()) {
2263 right.tag();
2264 }
2265 }
2266 }
2267
2268 void
2269 Data::typeMatchRight(const Data& right)
2270 {
2271 if (isTagged()) {
2272 if (right.isExpanded()) {
2273 expand();
2274 }
2275 } else if (isConstant()) {
2276 if (right.isExpanded()) {
2277 expand();
2278 } else if (right.isTagged()) {
2279 tag();
2280 }
2281 }
2282 }
2283
2284 void
2285 Data::setTaggedValueByName(std::string name,
2286 const boost::python::object& value)
2287 {
2288 if (getFunctionSpace().getDomain()->isValidTagName(name)) {
2289 FORCERESOLVE;
2290 int tagKey=getFunctionSpace().getDomain()->getTag(name);
2291 setTaggedValue(tagKey,value);
2292 }
2293 }
2294 void
2295 Data::setTaggedValue(int tagKey,
2296 const boost::python::object& value)
2297 {
2298 if (isProtected()) {
2299 throw DataException("Error - attempt to update protected Data object.");
2300 }
2301 //
2302 // Ensure underlying data object is of type DataTagged
2303 FORCERESOLVE;
2304 if (isConstant()) tag();
2305 numeric::array asNumArray(value);
2306
2307 // extract the shape of the numarray
2308 DataTypes::ShapeType tempShape;
2309 for (int i=0; i < asNumArray.getrank(); i++) {
2310 tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
2311 }
2312
2313 DataVector temp_data2;
2314 temp_data2.copyFromNumArray(asNumArray);
2315
2316 m_data->setTaggedValue(tagKey,tempShape, temp_data2);
2317 }
2318
2319
2320 void
2321 Data::setTaggedValueFromCPP(int tagKey,
2322 const DataTypes::ShapeType& pointshape,
2323 const DataTypes::ValueType& value,
2324 int dataOffset)
2325 {
2326 if (isProtected()) {
2327 throw DataException("Error - attempt to update protected Data object.");
2328 }
2329 //
2330 // Ensure underlying data object is of type DataTagged
2331 FORCERESOLVE;
2332 if (isConstant()) tag();
2333 //
2334 // Call DataAbstract::setTaggedValue
2335 m_data->setTaggedValue(tagKey,pointshape, value, dataOffset);
2336 }
2337
2338 int
2339 Data::getTagNumber(int dpno)
2340 {
2341 if (isEmpty())
2342 {
2343 throw DataException("Error - operation not permitted on instances of DataEmpty.");
2344 }
2345 return getFunctionSpace().getTagFromDataPointNo(dpno);
2346 }
2347
2348
2349 ostream& escript::operator<<(ostream& o, const Data& data)
2350 {
2351 o << data.toString();
2352 return o;
2353 }
2354
2355 Data
2356 escript::C_GeneralTensorProduct(Data& arg_0,
2357 Data& arg_1,
2358 int axis_offset,
2359 int transpose)
2360 {
2361 // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)
2362 // SM is the product of the last axis_offset entries in arg_0.getShape().
2363
2364 // Interpolate if necessary and find an appropriate function space
2365 Data arg_0_Z, arg_1_Z;
2366 if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
2367 if (arg_0.probeInterpolation(arg_1.getFunctionSpace())) {
2368 arg_0_Z = arg_0.interpolate(arg_1.getFunctionSpace());
2369 arg_1_Z = Data(arg_1);
2370 }
2371 else if (arg_1.probeInterpolation(arg_0.getFunctionSpace())) {
2372 arg_1_Z=arg_1.interpolate(arg_0.getFunctionSpace());
2373 arg_0_Z =Data(arg_0);
2374 }
2375 else {
2376 throw DataException("Error - C_GeneralTensorProduct: arguments have incompatible function spaces.");
2377 }
2378 } else {
2379 arg_0_Z = Data(arg_0);
2380 arg_1_Z = Data(arg_1);
2381 }
2382 // Get rank and shape of inputs
2383 int rank0 = arg_0_Z.getDataPointRank();
2384 int rank1 = arg_1_Z.getDataPointRank();
2385 const DataTypes::ShapeType& shape0 = arg_0_Z.getDataPointShape();
2386 const DataTypes::ShapeType& shape1 = arg_1_Z.getDataPointShape();
2387
2388 // Prepare for the loops of the product and verify compatibility of shapes
2389 int start0=0, start1=0;
2390 if (transpose == 0) {}
2391 else if (transpose == 1) { start0 = axis_offset; }
2392 else if (transpose == 2) { start1 = rank1-axis_offset; }
2393 else { throw DataException("C_GeneralTensorProduct: Error - transpose should be 0, 1 or 2"); }
2394
2395
2396 // Adjust the shapes for transpose
2397 DataTypes::ShapeType tmpShape0(rank0); // pre-sizing the vectors rather
2398 DataTypes::ShapeType tmpShape1(rank1); // than using push_back
2399 for (int i=0; i<rank0; i++) { tmpShape0[i]=shape0[(i+start0)%rank0]; }
2400 for (int i=0; i<rank1; i++) { tmpShape1[i]=shape1[(i+start1)%rank1]; }
2401
2402 #if 0
2403 // For debugging: show shape after transpose
2404 char tmp[100];
2405 std::string shapeStr;
2406 shapeStr = "(";
2407 for (int i=0; i<rank0; i++) { sprintf(tmp, "%d,", tmpShape0[i]); shapeStr += tmp; }
2408 shapeStr += ")";
2409 cout << "C_GeneralTensorProduct: Shape of arg0 is " << shapeStr << endl;
2410 shapeStr = "(";
2411 for (int i=0; i<rank1; i++) { sprintf(tmp, "%d,", tmpShape1[i]); shapeStr += tmp; }
2412 shapeStr += ")";
2413 cout << "C_GeneralTensorProduct: Shape of arg1 is " << shapeStr << endl;
2414 #endif
2415
2416 // Prepare for the loops of the product
2417 int SL=1, SM=1, SR=1;
2418 for (int i=0; i<rank0-axis_offset; i++) {
2419 SL *= tmpShape0[i];
2420 }
2421 for (int i=rank0-axis_offset; i<rank0; i++) {
2422 if (tmpShape0[i] != tmpShape1[i-(rank0-axis_offset)]) {
2423 throw DataException("C_GeneralTensorProduct: Error - incompatible shapes");
2424 }
2425 SM *= tmpShape0[i];
2426 }
2427 for (int i=axis_offset; i<rank1; i++) {
2428 SR *= tmpShape1[i];
2429 }
2430
2431 // Define the shape of the output (rank of shape is the sum of the loop ranges below)
2432 DataTypes::ShapeType shape2(rank0+rank1-2*axis_offset);
2433 { // block to limit the scope of out_index
2434 int out_index=0;
2435 for (int i=0; i<rank0-axis_offset; i++, ++out_index) { shape2[out_index]=tmpShape0[i]; } // First part of arg_0_Z
2436 for (int i=axis_offset; i<rank1; i++, ++out_index) { shape2[out_index]=tmpShape1[i]; } // Last part of arg_1_Z
2437 }
2438
2439 // Declare output Data object
2440 Data res;
2441
2442 if (arg_0_Z.isConstant() && arg_1_Z.isConstant()) {
2443 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace()); // DataConstant output
2444 double *ptr_0 = &(arg_0_Z.getDataAtOffset(0));
2445 double *ptr_1 = &(arg_1_Z.getDataAtOffset(0));
2446 double *ptr_2 = &(res.getDataAtOffset(0));
2447 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2448 }
2449 else if (arg_0_Z.isConstant() && arg_1_Z.isTagged()) {
2450
2451 // Prepare the DataConstant input
2452 DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2453 if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2454
2455 // Borrow DataTagged input from Data object
2456 DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2457 if (tmp_1==0) { throw DataException("GTP_1 Programming error - casting to DataTagged."); }
2458
2459 // Prepare a DataTagged output 2
2460 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace()); // DataTagged output
2461 res.tag();
2462 DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2463 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2464
2465 // Prepare offset into DataConstant
2466 int offset_0 = tmp_0->getPointOffset(0,0);
2467 double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2468 // Get the views
2469 // DataArrayView view_1 = tmp_1->getDefaultValue();
2470 // DataArrayView view_2 = tmp_2->getDefaultValue();
2471 // // Get the pointers to the actual data
2472 // double *ptr_1 = &((view_1.getData())[0]);
2473 // double *ptr_2 = &((view_2.getData())[0]);
2474
2475 double *ptr_1 = &(tmp_1->getDefaultValue(0));
2476 double *ptr_2 = &(tmp_2->getDefaultValue(0));
2477
2478
2479 // Compute an MVP for the default
2480 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2481 // Compute an MVP for each tag
2482 const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2483 DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2484 for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2485 tmp_2->addTag(i->first);
2486 // DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2487 // DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2488 // double *ptr_1 = &view_1.getData(0);
2489 // double *ptr_2 = &view_2.getData(0);
2490
2491 double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2492 double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2493
2494 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2495 }
2496
2497 }
2498 else if (arg_0_Z.isConstant() && arg_1_Z.isExpanded()) {
2499
2500 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2501 DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2502 DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2503 DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2504 if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2505 if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2506 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2507 int sampleNo_1,dataPointNo_1;
2508 int numSamples_1 = arg_1_Z.getNumSamples();
2509 int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2510 int offset_0 = tmp_0->getPointOffset(0,0);
2511 #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2512 for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2513 for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2514 int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2515 int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2516 double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2517 double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2518 double *ptr_2 = &(res.getDataAtOffset(offset_2));
2519 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2520 }
2521 }
2522
2523 }
2524 else if (arg_0_Z.isTagged() && arg_1_Z.isConstant()) {
2525
2526 // Borrow DataTagged input from Data object
2527 DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2528 if (tmp_0==0) { throw DataException("GTP_0 Programming error - casting to DataTagged."); }
2529
2530 // Prepare the DataConstant input
2531 DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2532 if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2533
2534 // Prepare a DataTagged output 2
2535 res = Data(0.0, shape2, arg_0_Z.getFunctionSpace()); // DataTagged output
2536 res.tag();
2537 DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2538 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2539
2540 // Prepare offset into DataConstant
2541 int offset_1 = tmp_1->getPointOffset(0,0);
2542 double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2543 // Get the views
2544 // DataArrayView view_0 = tmp_0->getDefaultValue();
2545 // DataArrayView view_2 = tmp_2->getDefaultValue();
2546 // // Get the pointers to the actual data
2547 // double *ptr_0 = &((view_0.getData())[0]);
2548 // double *ptr_2 = &((view_2.getData())[0]);
2549
2550 double *ptr_0 = &(tmp_0->getDefaultValue(0));
2551 double *ptr_2 = &(tmp_2->getDefaultValue(0));
2552
2553 // Compute an MVP for the default
2554 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2555 // Compute an MVP for each tag
2556 const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2557 DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2558 for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2559 // tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2560 // DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2561 // DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2562 // double *ptr_0 = &view_0.getData(0);
2563 // double *ptr_2 = &view_2.getData(0);
2564
2565 tmp_2->addTag(i->first);
2566 double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2567 double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2568 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2569 }
2570
2571 }
2572 else if (arg_0_Z.isTagged() && arg_1_Z.isTagged()) {
2573
2574 // Borrow DataTagged input from Data object
2575 DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2576 if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2577
2578 // Borrow DataTagged input from Data object
2579 DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2580 if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2581
2582 // Prepare a DataTagged output 2
2583 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());
2584 res.tag(); // DataTagged output
2585 DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2586 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2587
2588 // // Get the views
2589 // DataArrayView view_0 = tmp_0->getDefaultValue();
2590 // DataArrayView view_1 = tmp_1->getDefaultValue();
2591 // DataArrayView view_2 = tmp_2->getDefaultValue();
2592 // // Get the pointers to the actual data
2593 // double *ptr_0 = &((view_0.getData())[0]);
2594 // double *ptr_1 = &((view_1.getData())[0]);
2595 // double *ptr_2 = &((view_2.getData())[0]);
2596
2597 double *ptr_0 = &(tmp_0->getDefaultValue(0));
2598 double *ptr_1 = &(tmp_1->getDefaultValue(0));
2599 double *ptr_2 = &(tmp_2->getDefaultValue(0));
2600
2601
2602 // Compute an MVP for the default
2603 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2604 // Merge the tags
2605 DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2606 const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2607 const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2608 for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2609 tmp_2->addTag(i->first); // use tmp_2 to get correct shape
2610 }
2611 for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2612 tmp_2->addTag(i->first);
2613 }
2614 // Compute an MVP for each tag
2615 const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2616 for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2617 // DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2618 // DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2619 // DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2620 // double *ptr_0 = &view_0.getData(0);
2621 // double *ptr_1 = &view_1.getData(0);
2622 // double *ptr_2 = &view_2.getData(0);
2623
2624 double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2625 double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2626 double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2627
2628 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2629 }
2630
2631 }
2632 else if (arg_0_Z.isTagged() && arg_1_Z.isExpanded()) {
2633
2634 // After finding a common function space above the two inputs have the same numSamples and num DPPS
2635 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2636 DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2637 DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2638 DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2639 if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2640 if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2641 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2642 int sampleNo_0,dataPointNo_0;
2643 int numSamples_0 = arg_0_Z.getNumSamples();
2644 int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2645 #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2646 for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2647 int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2648 double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2649 for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2650 int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2651 int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2652 double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2653 double *ptr_2 = &(res.getDataAtOffset(offset_2));
2654 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2655 }
2656 }
2657
2658 }
2659 else if (arg_0_Z.isExpanded() && arg_1_Z.isConstant()) {
2660
2661 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2662 DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2663 DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2664 DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2665 if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2666 if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2667 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2668 int sampleNo_0,dataPointNo_0;
2669 int numSamples_0 = arg_0_Z.getNumSamples();
2670 int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2671 int offset_1 = tmp_1->getPointOffset(0,0);
2672 #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2673 for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2674 for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2675 int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2676 int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2677 double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2678 double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2679 double *ptr_2 = &(res.getDataAtOffset(offset_2));
2680 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2681 }
2682 }
2683
2684
2685 }
2686 else if (arg_0_Z.isExpanded() && arg_1_Z.isTagged()) {
2687
2688 // After finding a common function space above the two inputs have the same numSamples and num DPPS
2689 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2690 DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2691 DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2692 DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2693 if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2694 if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2695 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2696 int sampleNo_0,dataPointNo_0;
2697 int numSamples_0 = arg_0_Z.getNumSamples();
2698 int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2699 #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2700 for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2701 int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2702 double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2703 for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2704 int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2705 int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2706 double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2707 double *ptr_2 = &(res.getDataAtOffset(offset_2));
2708 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2709 }
2710 }
2711
2712 }
2713 else if (arg_0_Z.isExpanded() && arg_1_Z.isExpanded()) {
2714
2715 // After finding a common function space above the two inputs have the same numSamples and num DPPS
2716 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2717 DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2718 DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2719 DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2720 if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2721 if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2722 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2723 int sampleNo_0,dataPointNo_0;
2724 int numSamples_0 = arg_0_Z.getNumSamples();
2725 int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2726 #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2727 for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2728 for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2729 int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2730 int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2731 int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2732 double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2733 double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2734 double *ptr_2 = &(res.getDataAtOffset(offset_2));
2735 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2736 }
2737 }
2738
2739 }
2740 else {
2741 throw DataException("Error - C_GeneralTensorProduct: unknown combination of inputs");
2742 }
2743
2744 return res;
2745 }
2746
2747 DataAbstract*
2748 Data::borrowData() const
2749 {
2750 return m_data.get();
2751 }
2752
2753 // Not all that happy about returning a non-const from a const
2754 DataAbstract_ptr
2755 Data::borrowDataPtr() const
2756 {
2757 return m_data;
2758 }
2759
2760 // Not all that happy about returning a non-const from a const
2761 DataReady_ptr
2762 Data::borrowReadyPtr() const
2763 {
2764 DataReady_ptr dr=dynamic_pointer_cast<DataReady>(m_data);
2765 EsysAssert((dr!=0), "Error - casting to DataReady.");
2766 return dr;
2767 }
2768
2769 std::string
2770 Data::toString() const
2771 {
2772 static const DataTypes::ValueType::size_type TOO_MANY_POINTS=80;
2773 if (isLazy())
2774 { // This needs to change back to printing out something useful once the summary ops
2775 return m_data->toString(); // are defined
2776 }
2777 if (getNumDataPoints()*getDataPointSize()>TOO_MANY_POINTS)
2778 {
2779 stringstream temp;
2780 temp << "Summary: inf="<< inf_const() << " sup=" << sup_const() << " data points=" << getNumDataPoints();
2781 return temp.str();
2782 }
2783 return m_data->toString();
2784 }
2785
2786
2787
2788 DataTypes::ValueType::const_reference
2789 Data::getDataAtOffset(DataTypes::ValueType::size_type i) const
2790 {
2791 if (isLazy())
2792 {
2793 throw DataException("getDataAtOffset not permitted on lazy data.");
2794 }
2795 return getReady()->getDataAtOffset(i);
2796 }
2797
2798
2799 DataTypes::ValueType::reference
2800 Data::getDataAtOffset(DataTypes::ValueType::size_type i)
2801 {
2802 if (isLazy())
2803 {
2804 throw DataException("getDataAtOffset not permitted on lazy data.");
2805 }
2806 return getReady()->getDataAtOffset(i);
2807 }
2808
2809 DataTypes::ValueType::const_reference
2810 Data::getDataPoint(int sampleNo, int dataPointNo) const
2811 {
2812 if (!isReady())
2813 {
2814 throw DataException("Programmer error - getDataPoint() not permitted on Lazy Data.");
2815 }
2816 else
2817 {
2818 const DataReady* dr=getReady();
2819 return dr->getDataAtOffset(dr->getPointOffset(sampleNo, dataPointNo));
2820 }
2821 }
2822
2823
2824 DataTypes::ValueType::reference
2825 Data::getDataPoint(int sampleNo, int dataPointNo)
2826 {
2827 if (!isReady())
2828 {
2829 throw DataException("Programmer error - getDataPoint() not permitted on Lazy Data.");
2830 }
2831 else
2832 {
2833 DataReady* dr=getReady();
2834 return dr->getDataAtOffset(dr->getPointOffset(sampleNo, dataPointNo));
2835 }
2836 }
2837
2838
2839 /* Member functions specific to the MPI implementation */
2840
2841 void
2842 Data::print()
2843 {
2844 int i,j;
2845
2846 printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );
2847 for( i=0; i<getNumSamples(); i++ )
2848 {
2849 printf( "[%6d]", i );
2850 for( j=0; j<getNumDataPointsPerSample(); j++ )
2851 printf( "\t%10.7g", (getSampleData(i))[j] );
2852 printf( "\n" );
2853 }
2854 }
2855 void
2856 Data::dump(const std::string fileName) const
2857 {
2858 try
2859 {
2860 if (isLazy())
2861 {
2862 Data temp(*this); // this is to get a non-const object which we can resolve
2863 temp.resolve();
2864 temp.dump(fileName);
2865 }
2866 else
2867 {
2868 return m_data->dump(fileName);
2869 }
2870 }
2871 catch (exception& e)
2872 {
2873 cout << e.what() << endl;
2874 }
2875 }
2876
2877 int
2878 Data::get_MPISize() const
2879 {
2880 int size;
2881 #ifdef PASO_MPI
2882 int error;
2883 error = MPI_Comm_size( get_MPIComm(), &size );
2884 #else
2885 size = 1;
2886 #endif
2887 return size;
2888 }
2889
2890 int
2891 Data::get_MPIRank() const
2892 {
2893 int rank;
2894 #ifdef PASO_MPI
2895 int error;
2896 error = MPI_Comm_rank( get_MPIComm(), &rank );
2897 #else
2898 rank = 0;
2899 #endif
2900 return rank;
2901 }
2902
2903 MPI_Comm
2904 Data::get_MPIComm() const
2905 {
2906 #ifdef PASO_MPI
2907 return MPI_COMM_WORLD;
2908 #else
2909 return -1;
2910 #endif
2911 }
2912
2913

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26