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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1328 - (hide annotations)
Mon Oct 15 03:23:15 2007 UTC (11 years, 11 months ago) by matt
File size: 76015 byte(s)
Fixed bug in power operation.

1 jgs 480
2 ksteube 1312 /* $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 jgs 474 #include "Data.h"
17 jgs 94
18 jgs 480 #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 ksteube 1312 extern "C" {
27     #include "escript/blocktimer.h"
28     }
29 jgs 480
30 jgs 119 #include <fstream>
31 jgs 94 #include <algorithm>
32     #include <vector>
33     #include <functional>
34    
35 jgs 153 #include <boost/python/dict.hpp>
36 jgs 94 #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 jgs 102 shared_ptr<DataAbstract> temp_data(temp);
50     m_data=temp_data;
51 gross 783 m_protected=false;
52 jgs 94 }
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 matt 1319
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 gross 783 m_protected=false;
71 jgs 94 }
72    
73     Data::Data(double value,
74     const DataArrayView::ShapeType& dataPointShape,
75     const FunctionSpace& what,
76     bool expanded)
77     {
78 matt 1319 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 gross 783 m_protected=false;
86 jgs 94 }
87    
88 jgs 102 Data::Data(const Data& inData)
89 jgs 94 {
90 jgs 102 m_data=inData.m_data;
91 gross 783 m_protected=inData.isProtected();
92 jgs 94 }
93    
94     Data::Data(const Data& inData,
95     const DataArrayView::RegionType& region)
96     {
97     //
98 jgs 102 // 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 gross 783 m_protected=false;
103 jgs 94 }
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 jgs 123 // Note: Must use a reference or pointer to a derived object
113 jgs 94 // in order to get polymorphic behaviour. Shouldn't really
114     // be able to create an instance of AbstractDomain but that was done
115 jgs 123 // as a boost:python work around which may no longer be required.
116 jgs 94 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 gross 783 m_protected=false;
125 jgs 94 }
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 jgs 102 shared_ptr<DataAbstract> temp_data(temp);
135     m_data=temp_data;
136 gross 783 m_protected=false;
137 jgs 94 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 gross 783 m_protected=false;
148 jgs 94 }
149    
150     Data::Data(const DataArrayView& value,
151     const FunctionSpace& what,
152     bool expanded)
153     {
154     initialise(value,what,expanded);
155 gross 783 m_protected=false;
156 jgs 94 }
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 gross 783 m_protected=false;
165 jgs 94 }
166    
167 matt 1319
168 jgs 94 Data::Data(const object& value,
169     const Data& other)
170     {
171 matt 1319
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 jgs 94 //
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 matt 1319
191     if (temp_dataView.getRank()==0) {
192     int len = DataArrayView::noValues(other.getPointDataView().getShape());
193    
194     DataVector temp2_data(len, temp_dataView(), len);
195 matt 1328 DataArrayView temp2_dataView(temp2_data, other.getPointDataView().getShape());
196 matt 1319 initialise(temp2_dataView, other.getFunctionSpace(), false);
197    
198 jgs 94 } else {
199     //
200     // Create a DataConstant with the same sample shape as other
201 matt 1319 initialise(temp_dataView, other.getFunctionSpace(), false);
202 jgs 94 }
203 gross 783 m_protected=false;
204 jgs 94 }
205    
206 jgs 151 Data::~Data()
207     {
208    
209     }
210    
211 jgs 94 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 jgs 121 const boost::python::tuple
228 jgs 94 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 jgs 102 shared_ptr<DataAbstract> temp_data(newData);
258     m_data=temp_data;
259 jgs 94 return;
260     }
261     }
262     {
263     DataTagged* temp=dynamic_cast<DataTagged*>(other.m_data.get());
264     if (temp!=0) {
265     //
266 jgs 102 // Construct a DataTagged copy
267 jgs 94 DataAbstract* newData=new DataTagged(*temp);
268 jgs 102 shared_ptr<DataAbstract> temp_data(newData);
269     m_data=temp_data;
270 jgs 94 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 jgs 102 shared_ptr<DataAbstract> temp_data(newData);
280     m_data=temp_data;
281 jgs 94 return;
282     }
283     }
284 jgs 102 {
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 jgs 94 throw DataException("Error - Copy not implemented for this Data type.");
296     }
297    
298 gross 1118
299 jgs 94 void
300 gross 1118 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 jgs 94 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 ksteube 1312 Data::setProtection()
373     {
374 gross 783 m_protected=true;
375     }
376    
377     bool
378 ksteube 1312 Data::isProtected() const
379     {
380 gross 783 return m_protected;
381     }
382    
383    
384    
385     void
386 jgs 94 Data::expand()
387     {
388     if (isConstant()) {
389     DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());
390     DataAbstract* temp=new DataExpanded(*tempDataConst);
391 jgs 102 shared_ptr<DataAbstract> temp_data(temp);
392     m_data=temp_data;
393 jgs 94 } else if (isTagged()) {
394     DataTagged* tempDataTag=dynamic_cast<DataTagged*>(m_data.get());
395     DataAbstract* temp=new DataExpanded(*tempDataTag);
396 jgs 102 shared_ptr<DataAbstract> temp_data(temp);
397     m_data=temp_data;
398 jgs 94 } 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 jgs 102 shared_ptr<DataAbstract> temp_data(temp);
415     m_data=temp_data;
416 jgs 94 } 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 gross 854 Data
428     Data::oneOver() const
429 jgs 102 {
430 gross 854 return escript::unaryOp(*this,bind1st(divides<double>(),1.));
431 jgs 102 }
432    
433 jgs 94 Data
434 gross 698 Data::wherePositive() const
435 jgs 94 {
436 gross 698 return escript::unaryOp(*this,bind2nd(greater<double>(),0.0));
437 jgs 94 }
438    
439     Data
440 gross 698 Data::whereNegative() const
441 jgs 102 {
442 gross 698 return escript::unaryOp(*this,bind2nd(less<double>(),0.0));
443 jgs 102 }
444    
445     Data
446 gross 698 Data::whereNonNegative() const
447 jgs 94 {
448 gross 698 return escript::unaryOp(*this,bind2nd(greater_equal<double>(),0.0));
449 jgs 94 }
450    
451     Data
452 gross 698 Data::whereNonPositive() const
453 jgs 94 {
454 gross 698 return escript::unaryOp(*this,bind2nd(less_equal<double>(),0.0));
455 jgs 94 }
456    
457     Data
458 jgs 571 Data::whereZero(double tol) const
459 jgs 94 {
460 jgs 571 Data dataAbs=abs();
461     return escript::unaryOp(dataAbs,bind2nd(less_equal<double>(),tol));
462 jgs 94 }
463    
464     Data
465 jgs 571 Data::whereNonZero(double tol) const
466 jgs 102 {
467 jgs 571 Data dataAbs=abs();
468     return escript::unaryOp(dataAbs,bind2nd(greater<double>(),tol));
469 jgs 102 }
470    
471     Data
472 jgs 94 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 ksteube 1312 double blocktimer_start = blocktimer_time();
496 jgs 94 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 ksteube 1312 blocktimer_increment("grad()", blocktimer_start);
503 jgs 94 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 gross 921
531    
532 ksteube 1312 const
533 jgs 121 boost::python::numeric::array
534 ksteube 1312 Data:: getValueOfDataPoint(int dataPointNo)
535 jgs 121 {
536 gross 921 size_t length=0;
537     int i, j, k, l;
538 jgs 121 //
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 gross 921 // 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 jgs 121
553     //
554     // resize the numeric array to the shape just calculated
555 gross 921 if (arrayRank==0) {
556     numArray.resize(1);
557     }
558 jgs 121 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 gross 921 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 trankine 924 if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
577 gross 921 throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
578     }
579 ksteube 1312
580 gross 921 //
581     // Check a valid data point number has been supplied
582 trankine 924 if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
583 gross 921 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 ksteube 1312
589 gross 921 switch( dataPointRank ){
590     case 0 :
591     numArray[0] = dataPointView();
592     break;
593 ksteube 1312 case 1 :
594 gross 921 for( i=0; i<dataPointShape[0]; i++ )
595     numArray[i]=dataPointView(i);
596     break;
597 ksteube 1312 case 2 :
598 gross 921 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 ksteube 1312 case 3 :
603 gross 921 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 jgs 117 }
617     //
618 gross 921 // return the array
619 jgs 117 return numArray;
620 gross 921
621 jgs 117 }
622 gross 921 void
623 ksteube 1312 Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)
624 jgs 121 {
625 gross 1034 // 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 gross 921 if (isProtected()) {
636     throw DataException("Error - attempt to update protected Data object.");
637     }
638     //
639     // check rank
640 ksteube 1312 if (num_array.getrank()<getDataPointRank())
641 gross 921 throw DataException("Rank of numarray does not match Data object rank");
642 bcumming 790
643 jgs 121 //
644 gross 921 // 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 jgs 121 }
649     //
650 gross 921 // make sure data is expanded:
651     if (!isExpanded()) {
652     expand();
653 jgs 121 }
654 gross 921 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 jgs 121
663 gross 922 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 ksteube 1312 const
684 gross 921 boost::python::numeric::array
685 ksteube 1312 Data::getValueOfGlobalDataPoint(int procNo, int dataPointNo)
686 gross 921 {
687     size_t length=0;
688     int i, j, k, l, pos;
689 jgs 121 //
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 gross 921 // 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 bcumming 790
727 jgs 121 //
728     // load the values for the data point into the numeric array.
729 bcumming 790
730     // updated for the MPI case
731     if( get_MPIRank()==procNo ){
732 gross 921 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 trankine 924 if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
738 gross 921 throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
739     }
740 ksteube 1312
741 gross 921 //
742     // Check a valid data point number has been supplied
743 trankine 924 if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
744 gross 921 throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
745     }
746     // TODO: global error handling
747 bcumming 790 // create a view of the data if it is stored locally
748 gross 921 DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
749 ksteube 1312
750 bcumming 790 // 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 ksteube 1312 case 1 :
757 bcumming 790 for( i=0; i<dataPointShape[0]; i++ )
758     tmpData[i]=dataPointView(i);
759     break;
760 ksteube 1312 case 2 :
761 bcumming 790 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 ksteube 1312 case 3 :
766 bcumming 790 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 gross 921 }
780 bcumming 790 }
781 ksteube 1312 #ifdef PASO_MPI
782 gross 921 // broadcast the data to all other processes
783     MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );
784     #endif
785 bcumming 790
786     // unpack the data
787     switch( dataPointRank ){
788     case 0 :
789 gross 921 numArray[0]=tmpData[0];
790 bcumming 790 break;
791 ksteube 1312 case 1 :
792 bcumming 790 for( i=0; i<dataPointShape[0]; i++ )
793     numArray[i]=tmpData[i];
794     break;
795 ksteube 1312 case 2 :
796 bcumming 790 for( i=0; i<dataPointShape[0]; i++ )
797     for( j=0; j<dataPointShape[1]; j++ )
798 gross 921 numArray[make_tuple(i,j)]=tmpData[i+j*dataPointShape[0]];
799 bcumming 790 break;
800 ksteube 1312 case 3 :
801 bcumming 790 for( i=0; i<dataPointShape[0]; i++ )
802     for( j=0; j<dataPointShape[1]; j++ )
803     for( k=0; k<dataPointShape[2]; k++ )
804 gross 921 numArray[make_tuple(i,j,k)]=tmpData[i+dataPointShape[0]*(j*+k*dataPointShape[1])];
805 bcumming 790 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 gross 921 numArray[make_tuple(i,j,k,l)]=tmpData[i+dataPointShape[0]*(j*+dataPointShape[1]*(k+l*dataPointShape[2]))];
812 bcumming 790 break;
813     }
814    
815 ksteube 1312 delete [] tmpData;
816 jgs 121 //
817     // return the loaded array
818     return numArray;
819     }
820    
821 gross 921
822    
823 jgs 121 boost::python::numeric::array
824 jgs 94 Data::integrate() const
825     {
826     int index;
827     int rank = getDataPointRank();
828     DataArrayView::ShapeType shape = getDataPointShape();
829 ksteube 1312 int dataPointSize = getDataPointSize();
830 jgs 94
831     //
832     // calculate the integral values
833 ksteube 1312 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 jgs 94 AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals,*this);
847 ksteube 1312 #endif
848 jgs 94
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 jgs 108 bp_array.resize(1);
855 jgs 94 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 gross 436 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 jgs 94 }
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 gross 436 bp_array[make_tuple(i,j,k)] = integrals[index];
881 jgs 94 }
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 gross 436 bp_array[make_tuple(i,j,k,l)] = integrals[index];
893 jgs 94 }
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 escript::unaryOp(*this,(Data::UnaryDFunPtr)::sin);
908     }
909    
910     Data
911     Data::cos() const
912     {
913     return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cos);
914     }
915    
916     Data
917     Data::tan() const
918     {
919     return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tan);
920     }
921    
922     Data
923 jgs 150 Data::asin() const
924     {
925     return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asin);
926     }
927    
928     Data
929     Data::acos() const
930     {
931     return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acos);
932     }
933    
934 phornby 1026
935 jgs 150 Data
936     Data::atan() const
937     {
938     return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atan);
939     }
940    
941     Data
942     Data::sinh() const
943     {
944     return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sinh);
945     }
946    
947     Data
948     Data::cosh() const
949     {
950     return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cosh);
951     }
952    
953     Data
954     Data::tanh() const
955     {
956     return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tanh);
957     }
958    
959 phornby 1026
960 jgs 150 Data
961 phornby 1026 Data::erf() const
962     {
963 gross 1028 #ifdef _WIN32
964     throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");
965     #else
966 phornby 1026 return escript::unaryOp(*this,(Data::UnaryDFunPtr)::erf);
967     #endif
968     }
969    
970     Data
971 jgs 150 Data::asinh() const
972     {
973 phornby 1032 #ifdef _WIN32
974     return escript::unaryOp(*this,escript::asinh_substitute);
975     #else
976 jgs 150 return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asinh);
977 phornby 1032 #endif
978 jgs 150 }
979    
980     Data
981     Data::acosh() const
982     {
983 phornby 1032 #ifdef _WIN32
984     return escript::unaryOp(*this,escript::acosh_substitute);
985     #else
986 jgs 150 return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acosh);
987 phornby 1032 #endif
988 jgs 150 }
989    
990     Data
991     Data::atanh() const
992     {
993 phornby 1032 #ifdef _WIN32
994     return escript::unaryOp(*this,escript::atanh_substitute);
995     #else
996 jgs 150 return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atanh);
997 phornby 1032 #endif
998 jgs 150 }
999    
1000     Data
1001 gross 286 Data::log10() const
1002 jgs 94 {
1003     return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log10);
1004     }
1005    
1006     Data
1007 gross 286 Data::log() const
1008 jgs 94 {
1009     return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);
1010     }
1011    
1012 jgs 106 Data
1013     Data::sign() const
1014 jgs 94 {
1015 jgs 106 return escript::unaryOp(*this,escript::fsign);
1016 jgs 94 }
1017    
1018 jgs 106 Data
1019     Data::abs() const
1020 jgs 94 {
1021 jgs 106 return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);
1022 jgs 94 }
1023    
1024 jgs 106 Data
1025     Data::neg() const
1026 jgs 94 {
1027 jgs 106 return escript::unaryOp(*this,negate<double>());
1028 jgs 94 }
1029    
1030 jgs 102 Data
1031 jgs 106 Data::pos() const
1032 jgs 94 {
1033 jgs 148 Data result;
1034     // perform a deep copy
1035     result.copy(*this);
1036     return result;
1037 jgs 102 }
1038    
1039     Data
1040 jgs 106 Data::exp() const
1041 jgs 102 {
1042 jgs 106 return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);
1043 jgs 102 }
1044    
1045     Data
1046 jgs 106 Data::sqrt() const
1047 jgs 102 {
1048 jgs 106 return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);
1049 jgs 102 }
1050    
1051 jgs 106 double
1052     Data::Lsup() const
1053 jgs 102 {
1054 bcumming 751 double localValue, globalValue;
1055 jgs 106 //
1056     // set the initial absolute maximum value to zero
1057 bcumming 751
1058 jgs 147 AbsMax abs_max_func;
1059 bcumming 751 localValue = algorithm(abs_max_func,0);
1060     #ifdef PASO_MPI
1061     MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1062     return globalValue;
1063     #else
1064     return localValue;
1065     #endif
1066 jgs 117 }
1067    
1068     double
1069 jgs 106 Data::sup() const
1070 jgs 102 {
1071 bcumming 751 double localValue, globalValue;
1072 jgs 106 //
1073     // set the initial maximum value to min possible double
1074 jgs 147 FMax fmax_func;
1075 bcumming 751 localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);
1076     #ifdef PASO_MPI
1077     MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1078     return globalValue;
1079     #else
1080     return localValue;
1081     #endif
1082 jgs 102 }
1083    
1084 jgs 106 double
1085     Data::inf() const
1086 jgs 102 {
1087 bcumming 751 double localValue, globalValue;
1088 jgs 106 //
1089     // set the initial minimum value to max possible double
1090 jgs 147 FMin fmin_func;
1091 bcumming 751 localValue = algorithm(fmin_func,numeric_limits<double>::max());
1092     #ifdef PASO_MPI
1093     MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );
1094     return globalValue;
1095     #else
1096     return localValue;
1097     #endif
1098 jgs 102 }
1099    
1100 bcumming 751 /* TODO */
1101     /* global reduction */
1102 jgs 102 Data
1103 jgs 106 Data::maxval() const
1104 jgs 102 {
1105 jgs 113 //
1106     // set the initial maximum value to min possible double
1107 jgs 147 FMax fmax_func;
1108     return dp_algorithm(fmax_func,numeric_limits<double>::max()*-1);
1109 jgs 102 }
1110    
1111     Data
1112 jgs 106 Data::minval() const
1113 jgs 102 {
1114 jgs 113 //
1115     // set the initial minimum value to max possible double
1116 jgs 147 FMin fmin_func;
1117     return dp_algorithm(fmin_func,numeric_limits<double>::max());
1118 jgs 102 }
1119    
1120 jgs 123 Data
1121 gross 804 Data::swapaxes(const int axis0, const int axis1) const
1122 jgs 123 {
1123 gross 804 int axis0_tmp,axis1_tmp;
1124 gross 800 DataArrayView::ShapeType s=getDataPointShape();
1125     DataArrayView::ShapeType ev_shape;
1126     // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1127     // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1128     int rank=getDataPointRank();
1129 gross 804 if (rank<2) {
1130     throw DataException("Error - Data::swapaxes argument must have at least rank 2.");
1131 gross 800 }
1132 gross 804 if (axis0<0 || axis0>rank-1) {
1133     throw DataException("Error - Data::swapaxes: axis0 must be between 0 and rank-1=" + rank-1);
1134     }
1135     if (axis1<0 || axis1>rank-1) {
1136     throw DataException("Error - Data::swapaxes: axis1 must be between 0 and rank-1=" + rank-1);
1137     }
1138     if (axis0 == axis1) {
1139     throw DataException("Error - Data::swapaxes: axis indices must be different.");
1140     }
1141     if (axis0 > axis1) {
1142     axis0_tmp=axis1;
1143     axis1_tmp=axis0;
1144     } else {
1145     axis0_tmp=axis0;
1146     axis1_tmp=axis1;
1147     }
1148 gross 800 for (int i=0; i<rank; i++) {
1149 gross 804 if (i == axis0_tmp) {
1150 ksteube 1312 ev_shape.push_back(s[axis1_tmp]);
1151 gross 804 } else if (i == axis1_tmp) {
1152 ksteube 1312 ev_shape.push_back(s[axis0_tmp]);
1153 gross 800 } else {
1154 ksteube 1312 ev_shape.push_back(s[i]);
1155 gross 800 }
1156     }
1157     Data ev(0.,ev_shape,getFunctionSpace());
1158     ev.typeMatchRight(*this);
1159 gross 804 m_data->swapaxes(ev.m_data.get(), axis0_tmp, axis1_tmp);
1160 gross 800 return ev;
1161    
1162 jgs 123 }
1163    
1164     Data
1165 ksteube 775 Data::symmetric() const
1166 jgs 123 {
1167 ksteube 775 // check input
1168     DataArrayView::ShapeType s=getDataPointShape();
1169     if (getDataPointRank()==2) {
1170 ksteube 1312 if(s[0] != s[1])
1171 ksteube 775 throw DataException("Error - Data::symmetric can only be calculated for rank 2 object with equal first and second dimension.");
1172     }
1173     else if (getDataPointRank()==4) {
1174     if(!(s[0] == s[2] && s[1] == s[3]))
1175     throw DataException("Error - Data::symmetric can only be calculated for rank 4 object with dim0==dim2 and dim1==dim3.");
1176     }
1177     else {
1178     throw DataException("Error - Data::symmetric can only be calculated for rank 2 or 4 object.");
1179     }
1180     Data ev(0.,getDataPointShape(),getFunctionSpace());
1181     ev.typeMatchRight(*this);
1182     m_data->symmetric(ev.m_data.get());
1183     return ev;
1184     }
1185    
1186     Data
1187     Data::nonsymmetric() const
1188     {
1189     // check input
1190     DataArrayView::ShapeType s=getDataPointShape();
1191     if (getDataPointRank()==2) {
1192 ksteube 1312 if(s[0] != s[1])
1193 ksteube 775 throw DataException("Error - Data::nonsymmetric can only be calculated for rank 2 object with equal first and second dimension.");
1194     DataArrayView::ShapeType ev_shape;
1195     ev_shape.push_back(s[0]);
1196     ev_shape.push_back(s[1]);
1197     Data ev(0.,ev_shape,getFunctionSpace());
1198     ev.typeMatchRight(*this);
1199     m_data->nonsymmetric(ev.m_data.get());
1200     return ev;
1201     }
1202     else if (getDataPointRank()==4) {
1203     if(!(s[0] == s[2] && s[1] == s[3]))
1204     throw DataException("Error - Data::nonsymmetric can only be calculated for rank 4 object with dim0==dim2 and dim1==dim3.");
1205     DataArrayView::ShapeType ev_shape;
1206     ev_shape.push_back(s[0]);
1207     ev_shape.push_back(s[1]);
1208     ev_shape.push_back(s[2]);
1209     ev_shape.push_back(s[3]);
1210     Data ev(0.,ev_shape,getFunctionSpace());
1211     ev.typeMatchRight(*this);
1212     m_data->nonsymmetric(ev.m_data.get());
1213     return ev;
1214     }
1215     else {
1216     throw DataException("Error - Data::nonsymmetric can only be calculated for rank 2 or 4 object.");
1217     }
1218     }
1219    
1220     Data
1221 gross 800 Data::trace(int axis_offset) const
1222 ksteube 775 {
1223     DataArrayView::ShapeType s=getDataPointShape();
1224     if (getDataPointRank()==2) {
1225     DataArrayView::ShapeType ev_shape;
1226     Data ev(0.,ev_shape,getFunctionSpace());
1227     ev.typeMatchRight(*this);
1228 gross 800 m_data->trace(ev.m_data.get(), axis_offset);
1229 ksteube 775 return ev;
1230     }
1231     if (getDataPointRank()==3) {
1232     DataArrayView::ShapeType ev_shape;
1233     if (axis_offset==0) {
1234     int s2=s[2];
1235     ev_shape.push_back(s2);
1236     }
1237     else if (axis_offset==1) {
1238     int s0=s[0];
1239     ev_shape.push_back(s0);
1240     }
1241     Data ev(0.,ev_shape,getFunctionSpace());
1242     ev.typeMatchRight(*this);
1243 gross 800 m_data->trace(ev.m_data.get(), axis_offset);
1244 ksteube 775 return ev;
1245     }
1246     if (getDataPointRank()==4) {
1247     DataArrayView::ShapeType ev_shape;
1248     if (axis_offset==0) {
1249     ev_shape.push_back(s[2]);
1250     ev_shape.push_back(s[3]);
1251     }
1252     else if (axis_offset==1) {
1253     ev_shape.push_back(s[0]);
1254     ev_shape.push_back(s[3]);
1255     }
1256     else if (axis_offset==2) {
1257     ev_shape.push_back(s[0]);
1258     ev_shape.push_back(s[1]);
1259     }
1260     Data ev(0.,ev_shape,getFunctionSpace());
1261     ev.typeMatchRight(*this);
1262 gross 800 m_data->trace(ev.m_data.get(), axis_offset);
1263 ksteube 775 return ev;
1264     }
1265     else {
1266 gross 800 throw DataException("Error - Data::trace can only be calculated for rank 2, 3 or 4 object.");
1267 ksteube 775 }
1268     }
1269    
1270     Data
1271     Data::transpose(int axis_offset) const
1272     {
1273     DataArrayView::ShapeType s=getDataPointShape();
1274     DataArrayView::ShapeType ev_shape;
1275     // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1276     // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1277     int rank=getDataPointRank();
1278     if (axis_offset<0 || axis_offset>rank) {
1279     throw DataException("Error - Data::transpose must have 0 <= axis_offset <= rank=" + rank);
1280     }
1281     for (int i=0; i<rank; i++) {
1282     int index = (axis_offset+i)%rank;
1283     ev_shape.push_back(s[index]); // Append to new shape
1284     }
1285     Data ev(0.,ev_shape,getFunctionSpace());
1286     ev.typeMatchRight(*this);
1287     m_data->transpose(ev.m_data.get(), axis_offset);
1288     return ev;
1289 jgs 123 }
1290    
1291 gross 576 Data
1292     Data::eigenvalues() const
1293     {
1294     // check input
1295     DataArrayView::ShapeType s=getDataPointShape();
1296 ksteube 1312 if (getDataPointRank()!=2)
1297 gross 576 throw DataException("Error - Data::eigenvalues can only be calculated for rank 2 object.");
1298 ksteube 1312 if(s[0] != s[1])
1299 gross 576 throw DataException("Error - Data::eigenvalues can only be calculated for object with equal first and second dimension.");
1300     // create return
1301     DataArrayView::ShapeType ev_shape(1,s[0]);
1302     Data ev(0.,ev_shape,getFunctionSpace());
1303     ev.typeMatchRight(*this);
1304     m_data->eigenvalues(ev.m_data.get());
1305     return ev;
1306     }
1307    
1308 jgs 121 const boost::python::tuple
1309 gross 576 Data::eigenvalues_and_eigenvectors(const double tol) const
1310     {
1311     DataArrayView::ShapeType s=getDataPointShape();
1312 ksteube 1312 if (getDataPointRank()!=2)
1313 gross 576 throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for rank 2 object.");
1314 ksteube 1312 if(s[0] != s[1])
1315 gross 576 throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for object with equal first and second dimension.");
1316     // create return
1317     DataArrayView::ShapeType ev_shape(1,s[0]);
1318     Data ev(0.,ev_shape,getFunctionSpace());
1319     ev.typeMatchRight(*this);
1320     DataArrayView::ShapeType V_shape(2,s[0]);
1321     Data V(0.,V_shape,getFunctionSpace());
1322     V.typeMatchRight(*this);
1323     m_data->eigenvalues_and_eigenvectors(ev.m_data.get(),V.m_data.get(),tol);
1324     return make_tuple(boost::python::object(ev),boost::python::object(V));
1325     }
1326    
1327     const boost::python::tuple
1328 gross 921 Data::minGlobalDataPoint() const
1329 jgs 121 {
1330 gross 921 // NB: calc_minGlobalDataPoint( had to be split off from minGlobalDataPoint( as boost::make_tuple causes an
1331 jgs 148 // abort (for unknown reasons) if there are openmp directives with it in the
1332     // surrounding function
1333    
1334     int DataPointNo;
1335 gross 921 int ProcNo;
1336     calc_minGlobalDataPoint(ProcNo,DataPointNo);
1337     return make_tuple(ProcNo,DataPointNo);
1338 jgs 148 }
1339    
1340     void
1341 gross 921 Data::calc_minGlobalDataPoint(int& ProcNo,
1342     int& DataPointNo) const
1343 jgs 148 {
1344     int i,j;
1345     int lowi=0,lowj=0;
1346     double min=numeric_limits<double>::max();
1347    
1348 jgs 121 Data temp=minval();
1349    
1350     int numSamples=temp.getNumSamples();
1351     int numDPPSample=temp.getNumDataPointsPerSample();
1352    
1353 jgs 148 double next,local_min;
1354     int local_lowi,local_lowj;
1355 jgs 121
1356 jgs 148 #pragma omp parallel private(next,local_min,local_lowi,local_lowj)
1357     {
1358     local_min=min;
1359     #pragma omp for private(i,j) schedule(static)
1360     for (i=0; i<numSamples; i++) {
1361     for (j=0; j<numDPPSample; j++) {
1362     next=temp.getDataPoint(i,j)();
1363     if (next<local_min) {
1364     local_min=next;
1365     local_lowi=i;
1366     local_lowj=j;
1367     }
1368 jgs 121 }
1369     }
1370 jgs 148 #pragma omp critical
1371     if (local_min<min) {
1372     min=local_min;
1373     lowi=local_lowi;
1374     lowj=local_lowj;
1375     }
1376 jgs 121 }
1377    
1378 bcumming 782 #ifdef PASO_MPI
1379     // determine the processor on which the minimum occurs
1380     next = temp.getDataPoint(lowi,lowj)();
1381     int lowProc = 0;
1382     double *globalMins = new double[get_MPISize()+1];
1383     int error = MPI_Gather ( &next, 1, MPI_DOUBLE, globalMins, 1, MPI_DOUBLE, 0, get_MPIComm() );
1384 ksteube 1312
1385 bcumming 782 if( get_MPIRank()==0 ){
1386     next = globalMins[lowProc];
1387     for( i=1; i<get_MPISize(); i++ )
1388     if( next>globalMins[i] ){
1389     lowProc = i;
1390     next = globalMins[i];
1391     }
1392     }
1393     MPI_Bcast( &lowProc, 1, MPI_DOUBLE, 0, get_MPIComm() );
1394    
1395     delete [] globalMins;
1396     ProcNo = lowProc;
1397 bcumming 790 #else
1398     ProcNo = 0;
1399 bcumming 782 #endif
1400 gross 921 DataPointNo = lowj + lowi * numDPPSample;
1401 jgs 121 }
1402    
1403 jgs 104 void
1404     Data::saveDX(std::string fileName) const
1405     {
1406 jgs 153 boost::python::dict args;
1407     args["data"]=boost::python::object(this);
1408     getDomain().saveDX(fileName,args);
1409 jgs 104 return;
1410     }
1411    
1412 jgs 110 void
1413     Data::saveVTK(std::string fileName) const
1414     {
1415 jgs 153 boost::python::dict args;
1416     args["data"]=boost::python::object(this);
1417     getDomain().saveVTK(fileName,args);
1418 jgs 110 return;
1419     }
1420    
1421 jgs 102 Data&
1422     Data::operator+=(const Data& right)
1423     {
1424 gross 783 if (isProtected()) {
1425     throw DataException("Error - attempt to update protected Data object.");
1426     }
1427 jgs 94 binaryOp(right,plus<double>());
1428     return (*this);
1429     }
1430    
1431 jgs 102 Data&
1432     Data::operator+=(const boost::python::object& right)
1433 jgs 94 {
1434 gross 854 Data tmp(right,getFunctionSpace(),false);
1435     binaryOp(tmp,plus<double>());
1436 jgs 94 return (*this);
1437     }
1438 ksteube 1312 Data&
1439     Data::operator=(const Data& other)
1440     {
1441     copy(other);
1442     return (*this);
1443     }
1444 jgs 94
1445 jgs 102 Data&
1446     Data::operator-=(const Data& right)
1447 jgs 94 {
1448 gross 783 if (isProtected()) {
1449     throw DataException("Error - attempt to update protected Data object.");
1450     }
1451 jgs 94 binaryOp(right,minus<double>());
1452     return (*this);
1453     }
1454    
1455 jgs 102 Data&
1456     Data::operator-=(const boost::python::object& right)
1457 jgs 94 {
1458 gross 854 Data tmp(right,getFunctionSpace(),false);
1459     binaryOp(tmp,minus<double>());
1460 jgs 94 return (*this);
1461     }
1462    
1463 jgs 102 Data&
1464     Data::operator*=(const Data& right)
1465 jgs 94 {
1466 gross 783 if (isProtected()) {
1467     throw DataException("Error - attempt to update protected Data object.");
1468     }
1469 jgs 94 binaryOp(right,multiplies<double>());
1470     return (*this);
1471     }
1472    
1473 jgs 102 Data&
1474     Data::operator*=(const boost::python::object& right)
1475 jgs 94 {
1476 gross 854 Data tmp(right,getFunctionSpace(),false);
1477     binaryOp(tmp,multiplies<double>());
1478 jgs 94 return (*this);
1479     }
1480    
1481 jgs 102 Data&
1482     Data::operator/=(const Data& right)
1483 jgs 94 {
1484 gross 783 if (isProtected()) {
1485     throw DataException("Error - attempt to update protected Data object.");
1486     }
1487 jgs 94 binaryOp(right,divides<double>());
1488     return (*this);
1489     }
1490    
1491 jgs 102 Data&
1492     Data::operator/=(const boost::python::object& right)
1493 jgs 94 {
1494 gross 854 Data tmp(right,getFunctionSpace(),false);
1495     binaryOp(tmp,divides<double>());
1496 jgs 94 return (*this);
1497     }
1498    
1499 jgs 102 Data
1500 gross 699 Data::rpowO(const boost::python::object& left) const
1501     {
1502     Data left_d(left,*this);
1503     return left_d.powD(*this);
1504     }
1505    
1506     Data
1507 jgs 102 Data::powO(const boost::python::object& right) const
1508 jgs 94 {
1509 gross 854 Data tmp(right,getFunctionSpace(),false);
1510     return powD(tmp);
1511 jgs 94 }
1512    
1513 jgs 102 Data
1514     Data::powD(const Data& right) const
1515 jgs 94 {
1516     Data result;
1517 gross 854 if (getDataPointRank()<right.getDataPointRank()) {
1518 ksteube 1312 result.copy(right);
1519 gross 854 result.binaryOp(*this,escript::rpow);
1520     } else {
1521     result.copy(*this);
1522     result.binaryOp(right,(Data::BinaryDFunPtr)::pow);
1523     }
1524 jgs 94 return result;
1525     }
1526    
1527 bcumming 751
1528 jgs 94 //
1529 jgs 123 // NOTE: It is essential to specify the namespace this operator belongs to
1530 jgs 102 Data
1531     escript::operator+(const Data& left, const Data& right)
1532 jgs 94 {
1533 matt 1327 return C_TensorBinaryOperation(left, right, plus<double>());
1534 jgs 94 }
1535    
1536     //
1537 jgs 123 // NOTE: It is essential to specify the namespace this operator belongs to
1538 jgs 102 Data
1539     escript::operator-(const Data& left, const Data& right)
1540 jgs 94 {
1541 matt 1327 return C_TensorBinaryOperation(left, right, minus<double>());
1542 jgs 94 }
1543    
1544     //
1545 jgs 123 // NOTE: It is essential to specify the namespace this operator belongs to
1546 jgs 102 Data
1547     escript::operator*(const Data& left, const Data& right)
1548 jgs 94 {
1549 matt 1327 return C_TensorBinaryOperation(left, right, multiplies<double>());
1550 jgs 94 }
1551    
1552     //
1553 jgs 123 // NOTE: It is essential to specify the namespace this operator belongs to
1554 jgs 102 Data
1555     escript::operator/(const Data& left, const Data& right)
1556 jgs 94 {
1557 matt 1327 return C_TensorBinaryOperation(left, right, divides<double>());
1558 jgs 94 }
1559    
1560     //
1561 jgs 123 // NOTE: It is essential to specify the namespace this operator belongs to
1562 jgs 102 Data
1563     escript::operator+(const Data& left, const boost::python::object& right)
1564 jgs 94 {
1565 gross 854 return left+Data(right,left.getFunctionSpace(),false);
1566 jgs 94 }
1567    
1568     //
1569 jgs 123 // NOTE: It is essential to specify the namespace this operator belongs to
1570 jgs 102 Data
1571     escript::operator-(const Data& left, const boost::python::object& right)
1572 jgs 94 {
1573 gross 854 return left-Data(right,left.getFunctionSpace(),false);
1574 jgs 94 }
1575    
1576     //
1577 jgs 123 // NOTE: It is essential to specify the namespace this operator belongs to
1578 jgs 102 Data
1579     escript::operator*(const Data& left, const boost::python::object& right)
1580 jgs 94 {
1581 gross 854 return left*Data(right,left.getFunctionSpace(),false);
1582 jgs 94 }
1583    
1584     //
1585 jgs 123 // NOTE: It is essential to specify the namespace this operator belongs to
1586 jgs 102 Data
1587     escript::operator/(const Data& left, const boost::python::object& right)
1588 jgs 94 {
1589 gross 854 return left/Data(right,left.getFunctionSpace(),false);
1590 jgs 94 }
1591    
1592     //
1593 jgs 123 // NOTE: It is essential to specify the namespace this operator belongs to
1594 jgs 102 Data
1595     escript::operator+(const boost::python::object& left, const Data& right)
1596 jgs 94 {
1597 gross 854 return Data(left,right.getFunctionSpace(),false)+right;
1598 jgs 94 }
1599    
1600     //
1601 jgs 123 // NOTE: It is essential to specify the namespace this operator belongs to
1602 jgs 102 Data
1603     escript::operator-(const boost::python::object& left, const Data& right)
1604 jgs 94 {
1605 gross 854 return Data(left,right.getFunctionSpace(),false)-right;
1606 jgs 94 }
1607    
1608     //
1609 jgs 123 // NOTE: It is essential to specify the namespace this operator belongs to
1610 jgs 102 Data
1611     escript::operator*(const boost::python::object& left, const Data& right)
1612 jgs 94 {
1613 gross 854 return Data(left,right.getFunctionSpace(),false)*right;
1614 jgs 94 }
1615    
1616     //
1617 jgs 123 // NOTE: It is essential to specify the namespace this operator belongs to
1618 jgs 102 Data
1619     escript::operator/(const boost::python::object& left, const Data& right)
1620 jgs 94 {
1621 gross 854 return Data(left,right.getFunctionSpace(),false)/right;
1622 jgs 94 }
1623    
1624     //
1625 jgs 102 //bool escript::operator==(const Data& left, const Data& right)
1626     //{
1627     // /*
1628     // NB: this operator does very little at this point, and isn't to
1629     // be relied on. Requires further implementation.
1630     // */
1631     //
1632     // bool ret;
1633     //
1634     // if (left.isEmpty()) {
1635     // if(!right.isEmpty()) {
1636     // ret = false;
1637     // } else {
1638     // ret = true;
1639     // }
1640     // }
1641     //
1642     // if (left.isConstant()) {
1643     // if(!right.isConstant()) {
1644     // ret = false;
1645     // } else {
1646     // ret = true;
1647     // }
1648     // }
1649     //
1650     // if (left.isTagged()) {
1651     // if(!right.isTagged()) {
1652     // ret = false;
1653     // } else {
1654     // ret = true;
1655     // }
1656     // }
1657     //
1658     // if (left.isExpanded()) {
1659     // if(!right.isExpanded()) {
1660     // ret = false;
1661     // } else {
1662     // ret = true;
1663     // }
1664     // }
1665     //
1666     // return ret;
1667     //}
1668    
1669 bcumming 751 /* TODO */
1670     /* global reduction */
1671 jgs 102 Data
1672 ksteube 1312 Data::getItem(const boost::python::object& key) const
1673 jgs 94 {
1674 jgs 102 const DataArrayView& view=getPointDataView();
1675 jgs 94
1676 jgs 102 DataArrayView::RegionType slice_region=view.getSliceRegion(key);
1677 jgs 94
1678 jgs 102 if (slice_region.size()!=view.getRank()) {
1679     throw DataException("Error - slice size does not match Data rank.");
1680 jgs 94 }
1681    
1682 jgs 102 return getSlice(slice_region);
1683 jgs 94 }
1684    
1685 bcumming 751 /* TODO */
1686     /* global reduction */
1687 jgs 94 Data
1688 jgs 102 Data::getSlice(const DataArrayView::RegionType& region) const
1689 jgs 94 {
1690 jgs 102 return Data(*this,region);
1691 jgs 94 }
1692    
1693 bcumming 751 /* TODO */
1694     /* global reduction */
1695 jgs 94 void
1696 jgs 102 Data::setItemO(const boost::python::object& key,
1697     const boost::python::object& value)
1698 jgs 94 {
1699 jgs 102 Data tempData(value,getFunctionSpace());
1700     setItemD(key,tempData);
1701     }
1702    
1703     void
1704     Data::setItemD(const boost::python::object& key,
1705     const Data& value)
1706     {
1707 jgs 94 const DataArrayView& view=getPointDataView();
1708 jgs 123
1709 jgs 94 DataArrayView::RegionType slice_region=view.getSliceRegion(key);
1710     if (slice_region.size()!=view.getRank()) {
1711     throw DataException("Error - slice size does not match Data rank.");
1712     }
1713 jgs 108 if (getFunctionSpace()!=value.getFunctionSpace()) {
1714     setSlice(Data(value,getFunctionSpace()),slice_region);
1715     } else {
1716     setSlice(value,slice_region);
1717     }
1718 jgs 94 }
1719    
1720     void
1721 jgs 102 Data::setSlice(const Data& value,
1722     const DataArrayView::RegionType& region)
1723 jgs 94 {
1724 gross 783 if (isProtected()) {
1725     throw DataException("Error - attempt to update protected Data object.");
1726     }
1727 jgs 102 Data tempValue(value);
1728     typeMatchLeft(tempValue);
1729     typeMatchRight(tempValue);
1730     m_data->setSlice(tempValue.m_data.get(),region);
1731     }
1732    
1733     void
1734     Data::typeMatchLeft(Data& right) const
1735     {
1736     if (isExpanded()){
1737     right.expand();
1738     } else if (isTagged()) {
1739     if (right.isConstant()) {
1740     right.tag();
1741     }
1742     }
1743     }
1744    
1745     void
1746     Data::typeMatchRight(const Data& right)
1747     {
1748 jgs 94 if (isTagged()) {
1749     if (right.isExpanded()) {
1750     expand();
1751     }
1752     } else if (isConstant()) {
1753     if (right.isExpanded()) {
1754     expand();
1755     } else if (right.isTagged()) {
1756     tag();
1757     }
1758     }
1759     }
1760    
1761     void
1762 gross 1044 Data::setTaggedValueByName(std::string name,
1763 ksteube 1312 const boost::python::object& value)
1764 gross 1044 {
1765     if (getFunctionSpace().getDomain().isValidTagName(name)) {
1766     int tagKey=getFunctionSpace().getDomain().getTag(name);
1767     setTaggedValue(tagKey,value);
1768     }
1769     }
1770     void
1771 jgs 94 Data::setTaggedValue(int tagKey,
1772     const boost::python::object& value)
1773     {
1774 gross 783 if (isProtected()) {
1775     throw DataException("Error - attempt to update protected Data object.");
1776     }
1777 jgs 94 //
1778     // Ensure underlying data object is of type DataTagged
1779     tag();
1780    
1781     if (!isTagged()) {
1782     throw DataException("Error - DataTagged conversion failed!!");
1783     }
1784    
1785 matt 1319 numeric::array asNumArray(value);
1786 jgs 94
1787 matt 1319
1788     // extract the shape of the numarray
1789     DataArrayView::ShapeType tempShape;
1790     for (int i=0; i < asNumArray.getrank(); i++) {
1791     tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
1792     }
1793    
1794     // get the space for the data vector
1795     int len = DataArrayView::noValues(tempShape);
1796     DataVector temp_data(len, 0.0, len);
1797     DataArrayView temp_dataView(temp_data, tempShape);
1798     temp_dataView.copy(asNumArray);
1799    
1800 jgs 94 //
1801     // Call DataAbstract::setTaggedValue
1802 matt 1319 m_data->setTaggedValue(tagKey,temp_dataView);
1803 jgs 94 }
1804    
1805 jgs 110 void
1806 jgs 121 Data::setTaggedValueFromCPP(int tagKey,
1807     const DataArrayView& value)
1808     {
1809 gross 783 if (isProtected()) {
1810     throw DataException("Error - attempt to update protected Data object.");
1811     }
1812 jgs 121 //
1813     // Ensure underlying data object is of type DataTagged
1814     tag();
1815    
1816     if (!isTagged()) {
1817     throw DataException("Error - DataTagged conversion failed!!");
1818     }
1819 ksteube 1312
1820 jgs 121 //
1821     // Call DataAbstract::setTaggedValue
1822     m_data->setTaggedValue(tagKey,value);
1823     }
1824    
1825 jgs 149 int
1826     Data::getTagNumber(int dpno)
1827     {
1828     return m_data->getTagNumber(dpno);
1829     }
1830    
1831 jgs 121 void
1832 jgs 119 Data::archiveData(const std::string fileName)
1833     {
1834     cout << "Archiving Data object to: " << fileName << endl;
1835    
1836     //
1837     // Determine type of this Data object
1838     int dataType = -1;
1839    
1840     if (isEmpty()) {
1841     dataType = 0;
1842     cout << "\tdataType: DataEmpty" << endl;
1843     }
1844     if (isConstant()) {
1845     dataType = 1;
1846     cout << "\tdataType: DataConstant" << endl;
1847     }
1848     if (isTagged()) {
1849     dataType = 2;
1850     cout << "\tdataType: DataTagged" << endl;
1851     }
1852     if (isExpanded()) {
1853     dataType = 3;
1854     cout << "\tdataType: DataExpanded" << endl;
1855     }
1856 jgs 123
1857 jgs 119 if (dataType == -1) {
1858     throw DataException("archiveData Error: undefined dataType");
1859     }
1860    
1861     //
1862     // Collect data items common to all Data types
1863     int noSamples = getNumSamples();
1864     int noDPPSample = getNumDataPointsPerSample();
1865     int functionSpaceType = getFunctionSpace().getTypeCode();
1866     int dataPointRank = getDataPointRank();
1867     int dataPointSize = getDataPointSize();
1868     int dataLength = getLength();
1869     DataArrayView::ShapeType dataPointShape = getDataPointShape();
1870 woo409 757 vector<int> referenceNumbers(noSamples);
1871 jgs 119 for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
1872 gross 964 referenceNumbers[sampleNo] = getFunctionSpace().getReferenceIDOfSample(sampleNo);
1873 jgs 119 }
1874 woo409 757 vector<int> tagNumbers(noSamples);
1875 jgs 119 if (isTagged()) {
1876     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
1877     tagNumbers[sampleNo] = getFunctionSpace().getTagFromSampleNo(sampleNo);
1878     }
1879     }
1880    
1881     cout << "\tnoSamples: " << noSamples << " noDPPSample: " << noDPPSample << endl;
1882     cout << "\tfunctionSpaceType: " << functionSpaceType << endl;
1883     cout << "\trank: " << dataPointRank << " size: " << dataPointSize << " length: " << dataLength << endl;
1884    
1885     //
1886     // Flatten Shape to an array of integers suitable for writing to file
1887     int flatShape[4] = {0,0,0,0};
1888     cout << "\tshape: < ";
1889     for (int dim=0; dim<dataPointRank; dim++) {
1890     flatShape[dim] = dataPointShape[dim];
1891     cout << dataPointShape[dim] << " ";
1892     }
1893     cout << ">" << endl;
1894    
1895     //
1896 jgs 123 // Open archive file
1897 jgs 119 ofstream archiveFile;
1898     archiveFile.open(fileName.data(), ios::out);
1899    
1900     if (!archiveFile.good()) {
1901     throw DataException("archiveData Error: problem opening archive file");
1902     }
1903    
1904 jgs 123 //
1905     // Write common data items to archive file
1906 jgs 119 archiveFile.write(reinterpret_cast<char *>(&dataType),sizeof(int));
1907     archiveFile.write(reinterpret_cast<char *>(&noSamples),sizeof(int));
1908     archiveFile.write(reinterpret_cast<char *>(&noDPPSample),sizeof(int));
1909     archiveFile.write(reinterpret_cast<char *>(&functionSpaceType),sizeof(int));
1910     archiveFile.write(reinterpret_cast<char *>(&dataPointRank),sizeof(int));
1911     archiveFile.write(reinterpret_cast<char *>(&dataPointSize),sizeof(int));
1912     archiveFile.write(reinterpret_cast<char *>(&dataLength),sizeof(int));
1913     for (int dim = 0; dim < 4; dim++) {
1914     archiveFile.write(reinterpret_cast<char *>(&flatShape[dim]),sizeof(int));
1915     }
1916     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
1917     archiveFile.write(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));
1918     }
1919     if (isTagged()) {
1920     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
1921     archiveFile.write(reinterpret_cast<char *>(&tagNumbers[sampleNo]),sizeof(int));
1922     }
1923     }
1924    
1925     if (!archiveFile.good()) {
1926     throw DataException("archiveData Error: problem writing to archive file");
1927     }
1928    
1929     //
1930 jgs 123 // Archive underlying data values for each Data type
1931     int noValues;
1932 jgs 119 switch (dataType) {
1933     case 0:
1934     // DataEmpty
1935 jgs 123 noValues = 0;
1936     archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));
1937     cout << "\tnoValues: " << noValues << endl;
1938 jgs 119 break;
1939     case 1:
1940     // DataConstant
1941 jgs 123 noValues = m_data->getLength();
1942     archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));
1943     cout << "\tnoValues: " << noValues << endl;
1944     if (m_data->archiveData(archiveFile,noValues)) {
1945     throw DataException("archiveData Error: problem writing data to archive file");
1946     }
1947 jgs 119 break;
1948     case 2:
1949     // DataTagged
1950 jgs 123 noValues = m_data->getLength();
1951     archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));
1952     cout << "\tnoValues: " << noValues << endl;
1953     if (m_data->archiveData(archiveFile,noValues)) {
1954     throw DataException("archiveData Error: problem writing data to archive file");
1955     }
1956 jgs 119 break;
1957     case 3:
1958     // DataExpanded
1959 jgs 123 noValues = m_data->getLength();
1960     archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));
1961     cout << "\tnoValues: " << noValues << endl;
1962     if (m_data->archiveData(archiveFile,noValues)) {
1963     throw DataException("archiveData Error: problem writing data to archive file");
1964     }
1965 jgs 119 break;
1966     }
1967    
1968 jgs 123 if (!archiveFile.good()) {
1969     throw DataException("archiveData Error: problem writing data to archive file");
1970     }
1971    
1972     //
1973     // Close archive file
1974     archiveFile.close();
1975    
1976     if (!archiveFile.good()) {
1977     throw DataException("archiveData Error: problem closing archive file");
1978     }
1979    
1980 jgs 119 }
1981    
1982     void
1983     Data::extractData(const std::string fileName,
1984     const FunctionSpace& fspace)
1985     {
1986     //
1987     // Can only extract Data to an object which is initially DataEmpty
1988     if (!isEmpty()) {
1989     throw DataException("extractData Error: can only extract to DataEmpty object");
1990     }
1991    
1992     cout << "Extracting Data object from: " << fileName << endl;
1993    
1994     int dataType;
1995     int noSamples;
1996     int noDPPSample;
1997     int functionSpaceType;
1998     int dataPointRank;
1999     int dataPointSize;
2000     int dataLength;
2001     DataArrayView::ShapeType dataPointShape;
2002     int flatShape[4];
2003    
2004     //
2005 jgs 123 // Open the archive file
2006 jgs 119 ifstream archiveFile;
2007     archiveFile.open(fileName.data(), ios::in);
2008    
2009     if (!archiveFile.good()) {
2010     throw DataException("extractData Error: problem opening archive file");
2011     }
2012    
2013 jgs 123 //
2014     // Read common data items from archive file
2015 jgs 119 archiveFile.read(reinterpret_cast<char *>(&dataType),sizeof(int));
2016     archiveFile.read(reinterpret_cast<char *>(&noSamples),sizeof(int));
2017     archiveFile.read(reinterpret_cast<char *>(&noDPPSample),sizeof(int));
2018     archiveFile.read(reinterpret_cast<char *>(&functionSpaceType),sizeof(int));
2019     archiveFile.read(reinterpret_cast<char *>(&dataPointRank),sizeof(int));
2020     archiveFile.read(reinterpret_cast<char *>(&dataPointSize),sizeof(int));
2021     archiveFile.read(reinterpret_cast<char *>(&dataLength),sizeof(int));
2022     for (int dim = 0; dim < 4; dim++) {
2023     archiveFile.read(reinterpret_cast<char *>(&flatShape[dim]),sizeof(int));
2024     if (flatShape[dim]>0) {
2025     dataPointShape.push_back(flatShape[dim]);
2026     }
2027     }
2028 woo409 757 vector<int> referenceNumbers(noSamples);
2029 jgs 119 for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
2030     archiveFile.read(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));
2031     }
2032 woo409 757 vector<int> tagNumbers(noSamples);
2033 jgs 119 if (dataType==2) {
2034     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
2035     archiveFile.read(reinterpret_cast<char *>(&tagNumbers[sampleNo]),sizeof(int));
2036     }
2037     }
2038    
2039     if (!archiveFile.good()) {
2040     throw DataException("extractData Error: problem reading from archive file");
2041     }
2042    
2043 jgs 123 //
2044     // Verify the values just read from the archive file
2045 jgs 119 switch (dataType) {
2046     case 0:
2047     cout << "\tdataType: DataEmpty" << endl;
2048     break;
2049     case 1:
2050     cout << "\tdataType: DataConstant" << endl;
2051     break;
2052     case 2:
2053     cout << "\tdataType: DataTagged" << endl;
2054     break;
2055     case 3:
2056     cout << "\tdataType: DataExpanded" << endl;
2057     break;
2058     default:
2059     throw DataException("extractData Error: undefined dataType read from archive file");
2060     break;
2061     }
2062    
2063     cout << "\tnoSamples: " << noSamples << " noDPPSample: " << noDPPSample << endl;
2064     cout << "\tfunctionSpaceType: " << functionSpaceType << endl;
2065     cout << "\trank: " << dataPointRank << " size: " << dataPointSize << " length: " << dataLength << endl;
2066     cout << "\tshape: < ";
2067     for (int dim = 0; dim < dataPointRank; dim++) {
2068     cout << dataPointShape[dim] << " ";
2069     }
2070     cout << ">" << endl;
2071    
2072     //
2073     // Verify that supplied FunctionSpace object is compatible with this Data object.
2074     if ( (fspace.getTypeCode()!=functionSpaceType) ||
2075     (fspace.getNumSamples()!=noSamples) ||
2076     (fspace.getNumDPPSample()!=noDPPSample)
2077     ) {
2078     throw DataException("extractData Error: incompatible FunctionSpace");
2079     }
2080     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
2081 gross 964 if (referenceNumbers[sampleNo] != fspace.getReferenceIDOfSample(sampleNo)) {
2082 jgs 119 throw DataException("extractData Error: incompatible FunctionSpace");
2083     }
2084     }
2085     if (dataType==2) {
2086     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
2087     if (tagNumbers[sampleNo] != fspace.getTagFromSampleNo(sampleNo)) {
2088     throw DataException("extractData Error: incompatible FunctionSpace");
2089     }
2090     }
2091     }
2092    
2093     //
2094     // Construct a DataVector to hold underlying data values
2095     DataVector dataVec(dataLength);
2096    
2097     //
2098     // Load this DataVector with the appropriate values
2099 jgs 123 int noValues;
2100     archiveFile.read(reinterpret_cast<char *>(&noValues),sizeof(int));
2101     cout << "\tnoValues: " << noValues << endl;
2102 jgs 119 switch (dataType) {
2103     case 0:
2104     // DataEmpty
2105 jgs 123 if (noValues != 0) {
2106     throw DataException("extractData Error: problem reading data from archive file");
2107     }
2108 jgs 119 break;
2109     case 1:
2110     // DataConstant
2111 jgs 123 if (dataVec.extractData(archiveFile,noValues)) {
2112     throw DataException("extractData Error: problem reading data from archive file");
2113     }
2114 jgs 119 break;
2115     case 2:
2116     // DataTagged
2117 jgs 123 if (dataVec.extractData(archiveFile,noValues)) {
2118     throw DataException("extractData Error: problem reading data from archive file");
2119     }
2120 jgs 119 break;
2121     case 3:
2122     // DataExpanded
2123 jgs 123 if (dataVec.extractData(archiveFile,noValues)) {
2124     throw DataException("extractData Error: problem reading data from archive file");
2125     }
2126 jgs 119 break;
2127     }
2128    
2129 jgs 123 if (!archiveFile.good()) {
2130     throw DataException("extractData Error: problem reading from archive file");
2131     }
2132    
2133 jgs 119 //
2134 jgs 123 // Close archive file
2135     archiveFile.close();
2136    
2137     if (!archiveFile.good()) {
2138     throw DataException("extractData Error: problem closing archive file");
2139     }
2140    
2141     //
2142 jgs 119 // Construct an appropriate Data object
2143     DataAbstract* tempData;
2144     switch (dataType) {
2145     case 0:
2146     // DataEmpty
2147     tempData=new DataEmpty();
2148     break;
2149     case 1:
2150     // DataConstant
2151     tempData=new DataConstant(fspace,dataPointShape,dataVec);
2152     break;
2153     case 2:
2154     // DataTagged
2155     tempData=new DataTagged(fspace,dataPointShape,tagNumbers,dataVec);
2156     break;
2157     case 3:
2158     // DataExpanded
2159     tempData=new DataExpanded(fspace,dataPointShape,dataVec);
2160     break;
2161     }
2162     shared_ptr<DataAbstract> temp_data(tempData);
2163     m_data=temp_data;
2164     }
2165    
2166 jgs 94 ostream& escript::operator<<(ostream& o, const Data& data)
2167     {
2168     o << data.toString();
2169     return o;
2170     }
2171 bcumming 782
2172 ksteube 813 Data
2173     escript::C_GeneralTensorProduct(Data& arg_0,
2174     Data& arg_1,
2175     int axis_offset,
2176     int transpose)
2177     {
2178 gross 826 // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)
2179 ksteube 813 // SM is the product of the last axis_offset entries in arg_0.getShape().
2180    
2181     // Interpolate if necessary and find an appropriate function space
2182 gross 826 Data arg_0_Z, arg_1_Z;
2183 ksteube 813 if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
2184     if (arg_0.probeInterpolation(arg_1.getFunctionSpace())) {
2185 gross 826 arg_0_Z = arg_0.interpolate(arg_1.getFunctionSpace());
2186     arg_1_Z = Data(arg_1);
2187 ksteube 813 }
2188     else if (arg_1.probeInterpolation(arg_0.getFunctionSpace())) {
2189 gross 826 arg_1_Z=arg_1.interpolate(arg_0.getFunctionSpace());
2190     arg_0_Z =Data(arg_0);
2191 ksteube 813 }
2192     else {
2193     throw DataException("Error - C_GeneralTensorProduct: arguments have incompatible function spaces.");
2194     }
2195 gross 826 } else {
2196     arg_0_Z = Data(arg_0);
2197     arg_1_Z = Data(arg_1);
2198 ksteube 813 }
2199     // Get rank and shape of inputs
2200 gross 826 int rank0 = arg_0_Z.getDataPointRank();
2201     int rank1 = arg_1_Z.getDataPointRank();
2202     DataArrayView::ShapeType shape0 = arg_0_Z.getDataPointShape();
2203     DataArrayView::ShapeType shape1 = arg_1_Z.getDataPointShape();
2204 ksteube 813
2205     // Prepare for the loops of the product and verify compatibility of shapes
2206     int start0=0, start1=0;
2207     if (transpose == 0) {}
2208     else if (transpose == 1) { start0 = axis_offset; }
2209     else if (transpose == 2) { start1 = rank1-axis_offset; }
2210     else { throw DataException("C_GeneralTensorProduct: Error - transpose should be 0, 1 or 2"); }
2211    
2212     // Adjust the shapes for transpose
2213     DataArrayView::ShapeType tmpShape0;
2214     DataArrayView::ShapeType tmpShape1;
2215     for (int i=0; i<rank0; i++) { tmpShape0.push_back( shape0[(i+start0)%rank0] ); }
2216     for (int i=0; i<rank1; i++) { tmpShape1.push_back( shape1[(i+start1)%rank1] ); }
2217    
2218     #if 0
2219     // For debugging: show shape after transpose
2220     char tmp[100];
2221     std::string shapeStr;
2222     shapeStr = "(";
2223     for (int i=0; i<rank0; i++) { sprintf(tmp, "%d,", tmpShape0[i]); shapeStr += tmp; }
2224     shapeStr += ")";
2225     cout << "C_GeneralTensorProduct: Shape of arg0 is " << shapeStr << endl;
2226     shapeStr = "(";
2227     for (int i=0; i<rank1; i++) { sprintf(tmp, "%d,", tmpShape1[i]); shapeStr += tmp; }
2228     shapeStr += ")";
2229     cout << "C_GeneralTensorProduct: Shape of arg1 is " << shapeStr << endl;
2230     #endif
2231    
2232     // Prepare for the loops of the product
2233     int SL=1, SM=1, SR=1;
2234     for (int i=0; i<rank0-axis_offset; i++) {
2235     SL *= tmpShape0[i];
2236     }
2237     for (int i=rank0-axis_offset; i<rank0; i++) {
2238     if (tmpShape0[i] != tmpShape1[i-(rank0-axis_offset)]) {
2239     throw DataException("C_GeneralTensorProduct: Error - incompatible shapes");
2240     }
2241     SM *= tmpShape0[i];
2242     }
2243     for (int i=axis_offset; i<rank1; i++) {
2244     SR *= tmpShape1[i];
2245     }
2246    
2247     // Define the shape of the output
2248     DataArrayView::ShapeType shape2;
2249 gross 826 for (int i=0; i<rank0-axis_offset; i++) { shape2.push_back(tmpShape0[i]); } // First part of arg_0_Z
2250     for (int i=axis_offset; i<rank1; i++) { shape2.push_back(tmpShape1[i]); } // Last part of arg_1_Z
2251 ksteube 813
2252     // Declare output Data object
2253 gross 826 Data res;
2254 ksteube 813
2255 gross 826 if (arg_0_Z.isConstant() && arg_1_Z.isConstant()) {
2256     res = Data(0.0, shape2, arg_1_Z.getFunctionSpace()); // DataConstant output
2257     double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[0]);
2258     double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[0]);
2259     double *ptr_2 = &((res.getPointDataView().getData())[0]);
2260 ksteube 813 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2261     }
2262 gross 826 else if (arg_0_Z.isConstant() && arg_1_Z.isTagged()) {
2263 ksteube 813
2264     // Prepare the DataConstant input
2265 gross 826 DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2266 ksteube 813 if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2267    
2268     // Borrow DataTagged input from Data object
2269 gross 826 DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2270 ksteube 813 if (tmp_1==0) { throw DataException("GTP_1 Programming error - casting to DataTagged."); }
2271    
2272     // Prepare a DataTagged output 2
2273 gross 826 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace()); // DataTagged output
2274     res.tag();
2275     DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2276 ksteube 813 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2277    
2278     // Prepare offset into DataConstant
2279     int offset_0 = tmp_0->getPointOffset(0,0);
2280 gross 826 double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2281 ksteube 813 // Get the views
2282     DataArrayView view_1 = tmp_1->getDefaultValue();
2283     DataArrayView view_2 = tmp_2->getDefaultValue();
2284     // Get the pointers to the actual data
2285     double *ptr_1 = &((view_1.getData())[0]);
2286     double *ptr_2 = &((view_2.getData())[0]);
2287     // Compute an MVP for the default
2288     matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2289     // Compute an MVP for each tag
2290     const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2291     DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2292     for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2293     tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2294     DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2295     DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2296     double *ptr_1 = &view_1.getData(0);
2297     double *ptr_2 = &view_2.getData(0);
2298     matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2299     }
2300    
2301     }
2302 gross 826 else if (arg_0_Z.isConstant() && arg_1_Z.isExpanded()) {
2303 ksteube 813
2304 gross 826 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2305     DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2306     DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2307     DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2308 ksteube 813 if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2309     if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2310     if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2311     int sampleNo_1,dataPointNo_1;
2312 gross 826 int numSamples_1 = arg_1_Z.getNumSamples();
2313     int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2314 ksteube 813 int offset_0 = tmp_0->getPointOffset(0,0);
2315     #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2316     for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2317     for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2318     int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2319     int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2320 gross 826 double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2321     double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2322     double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2323 ksteube 813 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2324     }
2325     }
2326    
2327     }
2328 gross 826 else if (arg_0_Z.isTagged() && arg_1_Z.isConstant()) {
2329 ksteube 813
2330     // Borrow DataTagged input from Data object
2331 gross 826 DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2332 ksteube 813 if (tmp_0==0) { throw DataException("GTP_0 Programming error - casting to DataTagged."); }
2333    
2334     // Prepare the DataConstant input
2335 gross 826 DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2336 ksteube 813 if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2337    
2338     // Prepare a DataTagged output 2
2339 gross 826 res = Data(0.0, shape2, arg_0_Z.getFunctionSpace()); // DataTagged output
2340     res.tag();
2341     DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2342 ksteube 813 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2343    
2344     // Prepare offset into DataConstant
2345     int offset_1 = tmp_1->getPointOffset(0,0);
2346 gross 826 double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2347 ksteube 813 // Get the views
2348     DataArrayView view_0 = tmp_0->getDefaultValue();
2349     DataArrayView view_2 = tmp_2->getDefaultValue();
2350     // Get the pointers to the actual data
2351     double *ptr_0 = &((view_0.getData())[0]);
2352     double *ptr_2 = &((view_2.getData())[0]);
2353     // Compute an MVP for the default
2354     matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2355     // Compute an MVP for each tag
2356     const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2357     DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2358     for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2359     tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2360     DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2361     DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2362     double *ptr_0 = &view_0.getData(0);
2363     double *ptr_2 = &view_2.getData(0);
2364     matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2365     }
2366    
2367     }
2368 gross 826 else if (arg_0_Z.isTagged() && arg_1_Z.isTagged()) {
2369 ksteube 813
2370     // Borrow DataTagged input from Data object
2371 gross 826 DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2372 ksteube 813 if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2373    
2374     // Borrow DataTagged input from Data object
2375 gross 826 DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2376 ksteube 813 if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2377    
2378     // Prepare a DataTagged output 2
2379 gross 826 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());
2380     res.tag(); // DataTagged output
2381     DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2382 ksteube 813 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2383    
2384     // Get the views
2385     DataArrayView view_0 = tmp_0->getDefaultValue();
2386     DataArrayView view_1 = tmp_1->getDefaultValue();
2387     DataArrayView view_2 = tmp_2->getDefaultValue();
2388     // Get the pointers to the actual data
2389     double *ptr_0 = &((view_0.getData())[0]);
2390     double *ptr_1 = &((view_1.getData())[0]);
2391     double *ptr_2 = &((view_2.getData())[0]);
2392     // Compute an MVP for the default
2393     matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2394     // Merge the tags
2395     DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2396     const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2397     const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2398     for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2399     tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue()); // use tmp_2 to get correct shape
2400     }
2401     for (i=lookup_1.begin();i!=loo