/[escript]/trunk/escript/src/Data.cpp
ViewVC logotype

Contents of /trunk/escript/src/Data.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1349 - (show annotations)
Wed Nov 21 06:38:08 2007 UTC (11 years, 7 months ago) by matt
File size: 75737 byte(s)
Disambiguation of several template argument parameters so that it compiles under Windows.

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26