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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 800 - (hide annotations)
Tue Aug 8 11:23:18 2006 UTC (13 years, 1 month ago) by gross
File size: 65317 byte(s)
new function _swap. Python wrapper + testing is still missing.


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