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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 106 - (hide annotations)
Thu Dec 23 07:20:12 2004 UTC (14 years, 6 months ago) by jgs
File size: 23609 byte(s)
*** empty log message ***

1 jgs 94 // $Id$
2     /*=============================================================================
3    
4     ******************************************************************************
5     * *
6     * COPYRIGHT ACcESS 2004 - All Rights Reserved *
7     * *
8     * This software is the property of ACcESS. No part of this code *
9     * may be copied in any form or by any means without the expressed written *
10     * consent of ACcESS. Copying, use or modification of this software *
11     * by any unauthorised person is illegal unless that *
12     * person has a software license agreement with ACcESS. *
13     * *
14     ******************************************************************************
15    
16     ******************************************************************************/
17    
18     #include "escript/Data/Data.h"
19    
20     #include <iostream>
21     #include <algorithm>
22     #include <vector>
23     #include <exception>
24     #include <functional>
25     #include <math.h>
26    
27     #include <boost/python/str.hpp>
28     #include <boost/python/extract.hpp>
29     #include <boost/python/long.hpp>
30    
31     #include "escript/Data/DataException.h"
32    
33     #include "escript/Data/DataExpanded.h"
34     #include "escript/Data/DataConstant.h"
35     #include "escript/Data/DataTagged.h"
36     #include "escript/Data/DataEmpty.h"
37     #include "escript/Data/DataArray.h"
38     #include "escript/Data/DataAlgorithm.h"
39     #include "escript/Data/FunctionSpaceFactory.h"
40     #include "escript/Data/AbstractContinuousDomain.h"
41 jgs 102 #include "escript/Data/UnaryFuncs.h"
42 jgs 94
43     using namespace std;
44     using namespace boost::python;
45     using namespace boost;
46     using namespace escript;
47    
48     Data::Data()
49     {
50     //
51     // Default data is type DataEmpty
52     DataAbstract* temp=new DataEmpty();
53 jgs 102 shared_ptr<DataAbstract> temp_data(temp);
54     m_data=temp_data;
55 jgs 94 }
56    
57     Data::Data(double value,
58     const tuple& shape,
59     const FunctionSpace& what,
60     bool expanded)
61     {
62     DataArrayView::ShapeType dataPointShape;
63     for (int i = 0; i < shape.attr("__len__")(); ++i) {
64     dataPointShape.push_back(extract<const int>(shape[i]));
65     }
66     DataArray temp(dataPointShape,value);
67     initialise(temp.getView(),what,expanded);
68     }
69    
70     Data::Data(double value,
71     const DataArrayView::ShapeType& dataPointShape,
72     const FunctionSpace& what,
73     bool expanded)
74     {
75     DataArray temp(dataPointShape,value);
76     pair<int,int> dataShape=what.getDataShape();
77     initialise(temp.getView(),what,expanded);
78     }
79    
80 jgs 102 Data::Data(const Data& inData)
81 jgs 94 {
82 jgs 102 m_data=inData.m_data;
83 jgs 94 }
84    
85     Data::Data(const Data& inData,
86     const DataArrayView::RegionType& region)
87     {
88     //
89 jgs 102 // Create Data which is a slice of another Data
90     DataAbstract* tmp = inData.m_data->getSlice(region);
91     shared_ptr<DataAbstract> temp_data(tmp);
92     m_data=temp_data;
93 jgs 94 }
94    
95     Data::Data(const Data& inData,
96     const FunctionSpace& functionspace)
97     {
98     if (inData.getFunctionSpace()==functionspace) {
99     m_data=inData.m_data;
100     } else {
101     Data tmp(0,inData.getPointDataView().getShape(),functionspace,true);
102     // Note for Lutz, Must use a reference or pointer to a derived object
103     // in order to get polymorphic behaviour. Shouldn't really
104     // be able to create an instance of AbstractDomain but that was done
105     // as a boost python work around which may no longer be required.
106     const AbstractDomain& inDataDomain=inData.getDomain();
107     if (inDataDomain==functionspace.getDomain()) {
108     inDataDomain.interpolateOnDomain(tmp,inData);
109     } else {
110     inDataDomain.interpolateACross(tmp,inData);
111     }
112     m_data=tmp.m_data;
113     }
114     }
115    
116     Data::Data(const DataTagged::TagListType& tagKeys,
117     const DataTagged::ValueListType & values,
118     const DataArrayView& defaultValue,
119     const FunctionSpace& what,
120     bool expanded)
121     {
122     DataAbstract* temp=new DataTagged(tagKeys,values,defaultValue,what);
123 jgs 102 shared_ptr<DataAbstract> temp_data(temp);
124     m_data=temp_data;
125 jgs 94 if (expanded) {
126     expand();
127     }
128     }
129    
130     Data::Data(const numeric::array& value,
131     const FunctionSpace& what,
132     bool expanded)
133     {
134     initialise(value,what,expanded);
135     }
136    
137     Data::Data(const DataArrayView& value,
138     const FunctionSpace& what,
139     bool expanded)
140     {
141     initialise(value,what,expanded);
142     }
143    
144     Data::Data(const object& value,
145     const FunctionSpace& what,
146     bool expanded)
147     {
148     numeric::array asNumArray(value);
149     initialise(asNumArray,what,expanded);
150     }
151    
152     Data::Data(const object& value,
153     const Data& other)
154     {
155     //
156     // Create DataConstant using the given value and all other parameters
157     // copied from other. If value is a rank 0 object this Data
158     // will assume the point data shape of other.
159     DataArray temp(value);
160     if (temp.getView().getRank()==0) {
161     //
162     // Create a DataArray with the scalar value for all elements
163     DataArray temp2(other.getPointDataView().getShape(),temp.getView()());
164     initialise(temp2.getView(),other.getFunctionSpace(),false);
165     } else {
166     //
167     // Create a DataConstant with the same sample shape as other
168     initialise(temp.getView(),other.getFunctionSpace(),false);
169     }
170     }
171    
172     escriptDataC
173     Data::getDataC()
174     {
175     escriptDataC temp;
176     temp.m_dataPtr=(void*)this;
177     return temp;
178     }
179    
180     escriptDataC
181     Data::getDataC() const
182     {
183     escriptDataC temp;
184     temp.m_dataPtr=(void*)this;
185     return temp;
186     }
187    
188     tuple
189     Data::getShapeTuple() const
190     {
191     const DataArrayView::ShapeType& shape=getDataPointShape();
192     switch(getDataPointRank()) {
193     case 0:
194     return make_tuple();
195     case 1:
196     return make_tuple(long_(shape[0]));
197     case 2:
198     return make_tuple(long_(shape[0]),long_(shape[1]));
199     case 3:
200     return make_tuple(long_(shape[0]),long_(shape[1]),long_(shape[2]));
201     case 4:
202     return make_tuple(long_(shape[0]),long_(shape[1]),long_(shape[2]),long_(shape[3]));
203     default:
204     throw DataException("Error - illegal Data rank.");
205     }
206     }
207    
208     void
209     Data::copy(const Data& other)
210     {
211     //
212     // Perform a deep copy
213     {
214     DataExpanded* temp=dynamic_cast<DataExpanded*>(other.m_data.get());
215     if (temp!=0) {
216     //
217     // Construct a DataExpanded copy
218     DataAbstract* newData=new DataExpanded(*temp);
219 jgs 102 shared_ptr<DataAbstract> temp_data(newData);
220     m_data=temp_data;
221 jgs 94 return;
222     }
223     }
224     {
225     DataTagged* temp=dynamic_cast<DataTagged*>(other.m_data.get());
226     if (temp!=0) {
227     //
228 jgs 102 // Construct a DataTagged copy
229 jgs 94 DataAbstract* newData=new DataTagged(*temp);
230 jgs 102 shared_ptr<DataAbstract> temp_data(newData);
231     m_data=temp_data;
232 jgs 94 return;
233     }
234     }
235     {
236     DataConstant* temp=dynamic_cast<DataConstant*>(other.m_data.get());
237     if (temp!=0) {
238     //
239     // Construct a DataConstant copy
240     DataAbstract* newData=new DataConstant(*temp);
241 jgs 102 shared_ptr<DataAbstract> temp_data(newData);
242     m_data=temp_data;
243 jgs 94 return;
244     }
245     }
246 jgs 102 {
247     DataEmpty* temp=dynamic_cast<DataEmpty*>(other.m_data.get());
248     if (temp!=0) {
249     //
250     // Construct a DataEmpty copy
251     DataAbstract* newData=new DataEmpty();
252     shared_ptr<DataAbstract> temp_data(newData);
253     m_data=temp_data;
254     return;
255     }
256     }
257 jgs 94 throw DataException("Error - Copy not implemented for this Data type.");
258     }
259    
260     void
261     Data::copyWithMask(const Data& other,
262     const Data& mask)
263     {
264     Data mask1;
265     Data mask2;
266    
267     mask1 = mask.wherePositive();
268     mask2.copy(mask1);
269    
270     mask1 *= other;
271     mask2 *= *this;
272     mask2 = *this - mask2;
273    
274     *this = mask1 + mask2;
275     }
276    
277     bool
278     Data::isExpanded() const
279     {
280     DataExpanded* temp=dynamic_cast<DataExpanded*>(m_data.get());
281     return (temp!=0);
282     }
283    
284     bool
285     Data::isTagged() const
286     {
287     DataTagged* temp=dynamic_cast<DataTagged*>(m_data.get());
288     return (temp!=0);
289     }
290    
291     bool
292     Data::isEmpty() const
293     {
294     DataEmpty* temp=dynamic_cast<DataEmpty*>(m_data.get());
295     return (temp!=0);
296     }
297    
298     bool
299     Data::isConstant() const
300     {
301     DataConstant* temp=dynamic_cast<DataConstant*>(m_data.get());
302     return (temp!=0);
303     }
304    
305     void
306     Data::expand()
307     {
308     if (isConstant()) {
309     DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());
310     DataAbstract* temp=new DataExpanded(*tempDataConst);
311 jgs 102 shared_ptr<DataAbstract> temp_data(temp);
312     m_data=temp_data;
313 jgs 94 } else if (isTagged()) {
314     DataTagged* tempDataTag=dynamic_cast<DataTagged*>(m_data.get());
315     DataAbstract* temp=new DataExpanded(*tempDataTag);
316 jgs 102 shared_ptr<DataAbstract> temp_data(temp);
317     m_data=temp_data;
318 jgs 94 } else if (isExpanded()) {
319     //
320     // do nothing
321     } else if (isEmpty()) {
322     throw DataException("Error - Expansion of DataEmpty not possible.");
323     } else {
324     throw DataException("Error - Expansion not implemented for this Data type.");
325     }
326     }
327    
328     void
329     Data::tag()
330     {
331     if (isConstant()) {
332     DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());
333     DataAbstract* temp=new DataTagged(*tempDataConst);
334 jgs 102 shared_ptr<DataAbstract> temp_data(temp);
335     m_data=temp_data;
336 jgs 94 } else if (isTagged()) {
337     // do nothing
338     } else if (isExpanded()) {
339     throw DataException("Error - Creating tag data from DataExpanded not possible.");
340     } else if (isEmpty()) {
341     throw DataException("Error - Creating tag data from DataEmpty not possible.");
342     } else {
343     throw DataException("Error - Tagging not implemented for this Data type.");
344     }
345     }
346    
347 jgs 102 void
348     Data::reshapeDataPoint(const DataArrayView::ShapeType& shape)
349     {
350     m_data->reshapeDataPoint(shape);
351     }
352    
353 jgs 94 Data
354     Data::wherePositive() const
355     {
356     return escript::unaryOp(*this,bind2nd(greater<double>(),0.0));
357     }
358    
359     Data
360 jgs 102 Data::whereNegative() const
361     {
362     return escript::unaryOp(*this,bind2nd(less<double>(),0.0));
363     }
364    
365     Data
366 jgs 94 Data::whereNonNegative() const
367     {
368     return escript::unaryOp(*this,bind2nd(greater_equal<double>(),0.0));
369     }
370    
371     Data
372 jgs 102 Data::whereNonPositive() const
373 jgs 94 {
374 jgs 102 return escript::unaryOp(*this,bind2nd(less_equal<double>(),0.0));
375 jgs 94 }
376    
377     Data
378     Data::whereZero() const
379     {
380     return escript::unaryOp(*this,bind2nd(equal_to<double>(),0.0));
381     }
382    
383     Data
384 jgs 102 Data::whereNonZero() const
385     {
386     return escript::unaryOp(*this,bind2nd(not_equal_to<double>(),0.0));
387     }
388    
389     Data
390 jgs 94 Data::interpolate(const FunctionSpace& functionspace) const
391     {
392     return Data(*this,functionspace);
393     }
394    
395     bool
396     Data::probeInterpolation(const FunctionSpace& functionspace) const
397     {
398     if (getFunctionSpace()==functionspace) {
399     return true;
400     } else {
401     const AbstractDomain& domain=getDomain();
402     if (domain==functionspace.getDomain()) {
403     return domain.probeInterpolationOnDomain(getFunctionSpace().getTypeCode(),functionspace.getTypeCode());
404     } else {
405     return domain.probeInterpolationACross(getFunctionSpace().getTypeCode(),functionspace.getDomain(),functionspace.getTypeCode());
406     }
407     }
408     }
409    
410     Data
411     Data::gradOn(const FunctionSpace& functionspace) const
412     {
413     if (functionspace.getDomain()!=getDomain())
414     throw DataException("Error - gradient cannot be calculated on different domains.");
415     DataArrayView::ShapeType grad_shape=getPointDataView().getShape();
416     grad_shape.push_back(functionspace.getDim());
417     Data out(0.0,grad_shape,functionspace,true);
418     getDomain().setToGradient(out,*this);
419     return out;
420     }
421    
422     Data
423     Data::grad() const
424     {
425     return gradOn(escript::function(getDomain()));
426     }
427    
428     int
429     Data::getDataPointSize() const
430     {
431     return getPointDataView().noValues();
432     }
433    
434     DataArrayView::ValueType::size_type
435     Data::getLength() const
436     {
437     return m_data->getLength();
438     }
439    
440     const DataArrayView::ShapeType&
441     Data::getDataPointShape() const
442     {
443     return getPointDataView().getShape();
444     }
445    
446     boost::python::numeric::array
447     Data::integrate() const
448     {
449     int index;
450     int rank = getDataPointRank();
451     DataArrayView::ShapeType shape = getDataPointShape();
452    
453     //
454     // calculate the integral values
455     vector<double> integrals(getDataPointSize());
456     AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals,*this);
457    
458     //
459     // create the numeric array to be returned
460     // and load the array with the integral values
461     boost::python::numeric::array bp_array(1.0);
462     if (rank==0) {
463     index = 0;
464     bp_array[0] = integrals[index];
465     }
466     if (rank==1) {
467     bp_array.resize(shape[0]);
468     for (int i=0; i<shape[0]; i++) {
469     index = i;
470     bp_array[i] = integrals[index];
471     }
472     }
473     if (rank==2) {
474     bp_array.resize(shape[0],shape[1]);
475     for (int i=0; i<shape[0]; i++) {
476     for (int j=0; j<shape[1]; j++) {
477     index = i + shape[0] * j;
478     bp_array[i,j] = integrals[index];
479     }
480     }
481     }
482     if (rank==3) {
483     bp_array.resize(shape[0],shape[1],shape[2]);
484     for (int i=0; i<shape[0]; i++) {
485     for (int j=0; j<shape[1]; j++) {
486     for (int k=0; k<shape[2]; k++) {
487     index = i + shape[0] * ( j + shape[1] * k );
488     bp_array[i,j,k] = integrals[index];
489     }
490     }
491     }
492     }
493     if (rank==4) {
494     bp_array.resize(shape[0],shape[1],shape[2],shape[3]);
495     for (int i=0; i<shape[0]; i++) {
496     for (int j=0; j<shape[1]; j++) {
497     for (int k=0; k<shape[2]; k++) {
498     for (int l=0; l<shape[3]; l++) {
499     index = i + shape[0] * ( j + shape[1] * ( k + shape[2] * l ) );
500     bp_array[i,j,k,l] = integrals[index];
501     }
502     }
503     }
504     }
505     }
506    
507     //
508     // return the loaded array
509     return bp_array;
510     }
511    
512     Data
513     Data::sin() const
514     {
515     return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sin);
516     }
517    
518     Data
519     Data::cos() const
520     {
521     return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cos);
522     }
523    
524     Data
525     Data::tan() const
526     {
527     return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tan);
528     }
529    
530     Data
531     Data::log() const
532     {
533     return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log10);
534     }
535    
536     Data
537     Data::ln() const
538     {
539     return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);
540     }
541    
542 jgs 106 Data
543     Data::sign() const
544 jgs 94 {
545 jgs 106 return escript::unaryOp(*this,escript::fsign);
546 jgs 94 }
547    
548 jgs 106 Data
549     Data::abs() const
550 jgs 94 {
551 jgs 106 return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);
552 jgs 94 }
553    
554 jgs 106 Data
555     Data::neg() const
556 jgs 94 {
557 jgs 106 return escript::unaryOp(*this,negate<double>());
558 jgs 94 }
559    
560 jgs 102 Data
561 jgs 106 Data::pos() const
562 jgs 94 {
563 jgs 102 return (*this);
564     }
565    
566     Data
567 jgs 106 Data::exp() const
568 jgs 102 {
569 jgs 106 return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);
570 jgs 102 }
571    
572     Data
573 jgs 106 Data::sqrt() const
574 jgs 102 {
575 jgs 106 return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);
576 jgs 102 }
577    
578 jgs 106 double
579     Data::Lsup() const
580 jgs 102 {
581 jgs 106 //
582     // set the initial absolute maximum value to zero
583     return algorithm(DataAlgorithmAdapter<AbsMax>(0));
584 jgs 102 }
585    
586 jgs 106 double
587     Data::sup() const
588 jgs 102 {
589 jgs 106 //
590     // set the initial maximum value to min possible double
591     return algorithm(DataAlgorithmAdapter<FMax>(numeric_limits<double>::min()));
592 jgs 102 }
593    
594 jgs 106 double
595     Data::inf() const
596 jgs 102 {
597 jgs 106 //
598     // set the initial minimum value to max possible double
599     return algorithm(DataAlgorithmAdapter<FMin>(numeric_limits<double>::max()));
600 jgs 102 }
601    
602     Data
603 jgs 106 Data::maxval() const
604 jgs 102 {
605 jgs 106 return dp_algorithm(DataAlgorithmAdapter<FMax>(numeric_limits<double>::min()));
606 jgs 102 }
607    
608     Data
609 jgs 106 Data::minval() const
610 jgs 102 {
611 jgs 106 return dp_algorithm(DataAlgorithmAdapter<FMin>(numeric_limits<double>::max()));
612 jgs 102 }
613    
614     Data
615 jgs 106 Data::length() const
616 jgs 102 {
617 jgs 106 return dp_algorithm(DataAlgorithmAdapter<Length>(0));
618 jgs 102 }
619    
620     Data
621 jgs 106 Data::trace() const
622 jgs 102 {
623 jgs 106 return dp_algorithm(DataAlgorithmAdapter<Trace>(0));
624 jgs 102 }
625    
626     Data
627 jgs 106 Data::transpose(int axis) const
628 jgs 102 {
629 jgs 106 // not implemented
630     throw DataException("Error - Data::transpose not implemented yet.");
631     return Data();
632 jgs 102 }
633    
634 jgs 104 void
635     Data::saveDX(std::string fileName) const
636     {
637     getDomain().saveDX(fileName,*this);
638     return;
639     }
640    
641 jgs 102 Data&
642     Data::operator+=(const Data& right)
643     {
644 jgs 94 binaryOp(right,plus<double>());
645     return (*this);
646     }
647    
648 jgs 102 Data&
649     Data::operator+=(const boost::python::object& right)
650 jgs 94 {
651     binaryOp(right,plus<double>());
652     return (*this);
653     }
654    
655 jgs 102 Data&
656     Data::operator-=(const Data& right)
657 jgs 94 {
658     binaryOp(right,minus<double>());
659     return (*this);
660     }
661    
662 jgs 102 Data&
663     Data::operator-=(const boost::python::object& right)
664 jgs 94 {
665     binaryOp(right,minus<double>());
666     return (*this);
667     }
668    
669 jgs 102 Data&
670     Data::operator*=(const Data& right)
671 jgs 94 {
672     binaryOp(right,multiplies<double>());
673     return (*this);
674     }
675    
676 jgs 102 Data&
677     Data::operator*=(const boost::python::object& right)
678 jgs 94 {
679     binaryOp(right,multiplies<double>());
680     return (*this);
681     }
682    
683 jgs 102 Data&
684     Data::operator/=(const Data& right)
685 jgs 94 {
686     binaryOp(right,divides<double>());
687     return (*this);
688     }
689    
690 jgs 102 Data&
691     Data::operator/=(const boost::python::object& right)
692 jgs 94 {
693     binaryOp(right,divides<double>());
694     return (*this);
695     }
696    
697 jgs 102 Data
698     Data::powO(const boost::python::object& right) const
699 jgs 94 {
700     Data result;
701     result.copy(*this);
702     result.binaryOp(right,(Data::BinaryDFunPtr)::pow);
703     return result;
704     }
705    
706 jgs 102 Data
707     Data::powD(const Data& right) const
708 jgs 94 {
709     Data result;
710     result.copy(*this);
711     result.binaryOp(right,(Data::BinaryDFunPtr)::pow);
712     return result;
713     }
714    
715     //
716     // NOTE: It is essential to specify the namepsace this operator belongs to
717 jgs 102 Data
718     escript::operator+(const Data& left, const Data& right)
719 jgs 94 {
720     Data result;
721     //
722     // perform a deep copy
723     result.copy(left);
724     result+=right;
725     return result;
726     }
727    
728     //
729     // NOTE: It is essential to specify the namepsace this operator belongs to
730 jgs 102 Data
731     escript::operator-(const Data& left, const Data& right)
732 jgs 94 {
733     Data result;
734     //
735     // perform a deep copy
736     result.copy(left);
737     result-=right;
738     return result;
739     }
740    
741     //
742     // NOTE: It is essential to specify the namepsace this operator belongs to
743 jgs 102 Data
744     escript::operator*(const Data& left, const Data& right)
745 jgs 94 {
746     Data result;
747     //
748     // perform a deep copy
749     result.copy(left);
750     result*=right;
751     return result;
752     }
753    
754     //
755     // NOTE: It is essential to specify the namepsace this operator belongs to
756 jgs 102 Data
757     escript::operator/(const Data& left, const Data& right)
758 jgs 94 {
759     Data result;
760     //
761     // perform a deep copy
762     result.copy(left);
763     result/=right;
764     return result;
765     }
766    
767     //
768     // NOTE: It is essential to specify the namepsace this operator belongs to
769 jgs 102 Data
770     escript::operator+(const Data& left, const boost::python::object& right)
771 jgs 94 {
772     //
773     // Convert to DataArray format if possible
774     DataArray temp(right);
775     Data result;
776     //
777     // perform a deep copy
778     result.copy(left);
779     result+=right;
780     return result;
781     }
782    
783     //
784     // NOTE: It is essential to specify the namepsace this operator belongs to
785 jgs 102 Data
786     escript::operator-(const Data& left, const boost::python::object& right)
787 jgs 94 {
788     //
789     // Convert to DataArray format if possible
790     DataArray temp(right);
791     Data result;
792     //
793     // perform a deep copy
794     result.copy(left);
795     result-=right;
796     return result;
797     }
798    
799     //
800     // NOTE: It is essential to specify the namepsace this operator belongs to
801 jgs 102 Data
802     escript::operator*(const Data& left, const boost::python::object& right)
803 jgs 94 {
804     //
805     // Convert to DataArray format if possible
806     DataArray temp(right);
807     Data result;
808     //
809     // perform a deep copy
810     result.copy(left);
811     result*=right;
812     return result;
813     }
814    
815     //
816     // NOTE: It is essential to specify the namepsace this operator belongs to
817 jgs 102 Data
818     escript::operator/(const Data& left, const boost::python::object& right)
819 jgs 94 {
820     //
821     // Convert to DataArray format if possible
822     DataArray temp(right);
823     Data result;
824     //
825     // perform a deep copy
826     result.copy(left);
827     result/=right;
828     return result;
829     }
830    
831     //
832     // NOTE: It is essential to specify the namepsace this operator belongs to
833 jgs 102 Data
834     escript::operator+(const boost::python::object& left, const Data& right)
835 jgs 94 {
836     //
837     // Construct the result using the given value and the other parameters
838     // from right
839     Data result(left,right);
840     result+=right;
841     return result;
842     }
843    
844     //
845     // NOTE: It is essential to specify the namepsace this operator belongs to
846 jgs 102 Data
847     escript::operator-(const boost::python::object& left, const Data& right)
848 jgs 94 {
849     //
850     // Construct the result using the given value and the other parameters
851     // from right
852     Data result(left,right);
853     result-=right;
854     return result;
855     }
856    
857     //
858     // NOTE: It is essential to specify the namepsace this operator belongs to
859 jgs 102 Data
860     escript::operator*(const boost::python::object& left, const Data& right)
861 jgs 94 {
862     //
863     // Construct the result using the given value and the other parameters
864     // from right
865     Data result(left,right);
866     result*=right;
867     return result;
868     }
869    
870     //
871     // NOTE: It is essential to specify the namepsace this operator belongs to
872 jgs 102 Data
873     escript::operator/(const boost::python::object& left, const Data& right)
874 jgs 94 {
875     //
876     // Construct the result using the given value and the other parameters
877     // from right
878     Data result(left,right);
879     result/=right;
880     return result;
881     }
882    
883     //
884     // NOTE: It is essential to specify the namepsace this operator belongs to
885 jgs 102 //bool escript::operator==(const Data& left, const Data& right)
886     //{
887     // /*
888     // NB: this operator does very little at this point, and isn't to
889     // be relied on. Requires further implementation.
890     // */
891     //
892     // bool ret;
893     //
894     // if (left.isEmpty()) {
895     // if(!right.isEmpty()) {
896     // ret = false;
897     // } else {
898     // ret = true;
899     // }
900     // }
901     //
902     // if (left.isConstant()) {
903     // if(!right.isConstant()) {
904     // ret = false;
905     // } else {
906     // ret = true;
907     // }
908     // }
909     //
910     // if (left.isTagged()) {
911     // if(!right.isTagged()) {
912     // ret = false;
913     // } else {
914     // ret = true;
915     // }
916     // }
917     //
918     // if (left.isExpanded()) {
919     // if(!right.isExpanded()) {
920     // ret = false;
921     // } else {
922     // ret = true;
923     // }
924     // }
925     //
926     // return ret;
927     //}
928    
929     Data
930     Data::getItem(const boost::python::object& key) const
931 jgs 94 {
932 jgs 102 const DataArrayView& view=getPointDataView();
933 jgs 94
934 jgs 102 DataArrayView::RegionType slice_region=view.getSliceRegion(key);
935 jgs 94
936 jgs 102 if (slice_region.size()!=view.getRank()) {
937     throw DataException("Error - slice size does not match Data rank.");
938 jgs 94 }
939    
940 jgs 102 return getSlice(slice_region);
941 jgs 94 }
942    
943     Data
944 jgs 102 Data::getSlice(const DataArrayView::RegionType& region) const
945 jgs 94 {
946 jgs 102 return Data(*this,region);
947 jgs 94 }
948    
949     void
950 jgs 102 Data::setItemO(const boost::python::object& key,
951     const boost::python::object& value)
952 jgs 94 {
953 jgs 102 Data tempData(value,getFunctionSpace());
954     setItemD(key,tempData);
955     }
956    
957     void
958     Data::setItemD(const boost::python::object& key,
959     const Data& value)
960     {
961 jgs 94 const DataArrayView& view=getPointDataView();
962     DataArrayView::RegionType slice_region=view.getSliceRegion(key);
963     if (slice_region.size()!=view.getRank()) {
964     throw DataException("Error - slice size does not match Data rank.");
965     }
966     setSlice(value,slice_region);
967     }
968    
969     void
970 jgs 102 Data::setSlice(const Data& value,
971     const DataArrayView::RegionType& region)
972 jgs 94 {
973 jgs 102 Data tempValue(value);
974     typeMatchLeft(tempValue);
975     typeMatchRight(tempValue);
976     m_data->setSlice(tempValue.m_data.get(),region);
977     }
978    
979     void
980     Data::typeMatchLeft(Data& right) const
981     {
982     if (isExpanded()){
983     right.expand();
984     } else if (isTagged()) {
985     if (right.isConstant()) {
986     right.tag();
987     }
988     }
989     }
990    
991     void
992     Data::typeMatchRight(const Data& right)
993     {
994 jgs 94 if (isTagged()) {
995     if (right.isExpanded()) {
996     expand();
997     }
998     } else if (isConstant()) {
999     if (right.isExpanded()) {
1000     expand();
1001     } else if (right.isTagged()) {
1002     tag();
1003     }
1004     }
1005     }
1006    
1007     void
1008     Data::setTaggedValue(int tagKey,
1009     const boost::python::object& value)
1010     {
1011     //
1012     // Ensure underlying data object is of type DataTagged
1013     tag();
1014    
1015     if (!isTagged()) {
1016     throw DataException("Error - DataTagged conversion failed!!");
1017     }
1018    
1019     //
1020     // Construct DataArray from boost::python::object input value
1021     DataArray valueDataArray(value);
1022    
1023     //
1024     // Call DataAbstract::setTaggedValue
1025     m_data->setTaggedValue(tagKey,valueDataArray.getView());
1026     }
1027    
1028     /*
1029     Note: this version removed for now. Not needed, and breaks escript.cpp
1030     void
1031     Data::setTaggedValue(int tagKey,
1032     const DataArrayView& value)
1033     {
1034     //
1035     // Ensure underlying data object is of type DataTagged
1036     tag();
1037    
1038     if (!isTagged()) {
1039     throw DataException("Error - DataTagged conversion failed!!");
1040     }
1041    
1042     //
1043     // Call DataAbstract::setTaggedValue
1044     m_data->setTaggedValue(tagKey,value);
1045     }
1046     */
1047    
1048     ostream& escript::operator<<(ostream& o, const Data& data)
1049     {
1050     o << data.toString();
1051     return o;
1052     }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26