1 |
|
2 |
/******************************************************* |
3 |
* |
4 |
* Copyright (c) 2003-2008 by University of Queensland |
5 |
* Earth Systems Science Computational Center (ESSCC) |
6 |
* http://www.uq.edu.au/esscc |
7 |
* |
8 |
* Primary Business: Queensland, Australia |
9 |
* Licensed under the Open Software License version 3.0 |
10 |
* http://www.opensource.org/licenses/osl-3.0.php |
11 |
* |
12 |
*******************************************************/ |
13 |
|
14 |
|
15 |
#if !defined escript_DataAbstract_20040315_H |
16 |
#define escript_DataAbstract_20040315_H |
17 |
#include "system_dep.h" |
18 |
|
19 |
#include "DataTypes.h" |
20 |
#include "FunctionSpace.h" |
21 |
|
22 |
#include <boost/scoped_ptr.hpp> |
23 |
#include <boost/python/numeric.hpp> |
24 |
|
25 |
#include "DataException.h" |
26 |
|
27 |
#include <string> |
28 |
#include <fstream> |
29 |
|
30 |
#include "Pointers.h" |
31 |
|
32 |
namespace escript { |
33 |
|
34 |
/** |
35 |
\brief |
36 |
DataAbstract provides an abstract interface for the class of containers |
37 |
which hold ESyS data. |
38 |
|
39 |
Description: |
40 |
DataAbstract provides an abstract interface for the class of containers |
41 |
which hold ESyS data. The container may be thought of as a 2 dimensional |
42 |
array of data points where one dimension corresponds to the number of samples |
43 |
and the other to the number of data points per sample as defined by the function |
44 |
space associated with each Data object. The data points themselves are arrays of |
45 |
doubles of rank 0-4. |
46 |
*/ |
47 |
|
48 |
class DataAbstract; |
49 |
|
50 |
typedef POINTER_WRAPPER_CLASS(DataAbstract) DataAbstract_ptr; |
51 |
typedef POINTER_WRAPPER_CLASS(const DataAbstract) const_DataAbstract_ptr; |
52 |
|
53 |
class DataReady; |
54 |
|
55 |
typedef POINTER_WRAPPER_CLASS(DataReady) DataReady_ptr; |
56 |
typedef POINTER_WRAPPER_CLASS(const DataReady) const_DataReady_ptr; |
57 |
|
58 |
class DataAbstract : public REFCOUNT_BASE_CLASS(DataAbstract) |
59 |
{ |
60 |
|
61 |
public: |
62 |
|
63 |
typedef DataTypes::ValueType ValueType; |
64 |
typedef DataTypes::ShapeType ShapeType; |
65 |
|
66 |
ESCRIPT_DLL_API |
67 |
DataAbstract_ptr getPtr(); |
68 |
ESCRIPT_DLL_API |
69 |
const_DataAbstract_ptr getPtr() const; |
70 |
|
71 |
/** |
72 |
\brief |
73 |
Constructor for DataAbstract. |
74 |
|
75 |
Description: |
76 |
Constructor for DataAbstract. |
77 |
|
78 |
\param what - Input - A description of what this data represents. |
79 |
*/ |
80 |
ESCRIPT_DLL_API |
81 |
DataAbstract(const FunctionSpace& what, const ShapeType& shape, bool isDataEmpty=false); |
82 |
|
83 |
/** |
84 |
\brief |
85 |
Destructor for DataAbstract. |
86 |
*/ |
87 |
ESCRIPT_DLL_API |
88 |
virtual |
89 |
~DataAbstract(); |
90 |
|
91 |
/** |
92 |
\brief |
93 |
Write the data as a string. |
94 |
*/ |
95 |
ESCRIPT_DLL_API |
96 |
virtual |
97 |
std::string |
98 |
toString() const = 0; |
99 |
|
100 |
/** |
101 |
\brief Return a deep copy of the current object. |
102 |
*/ |
103 |
ESCRIPT_DLL_API |
104 |
virtual |
105 |
DataAbstract* |
106 |
deepCopy()=0; |
107 |
|
108 |
/** |
109 |
\brief Return a data object with all points resolved. |
110 |
*/ |
111 |
ESCRIPT_DLL_API |
112 |
virtual |
113 |
DataReady_ptr |
114 |
resolve()=0; |
115 |
|
116 |
/** |
117 |
\brief |
118 |
dumps the object into a netCDF file |
119 |
*/ |
120 |
ESCRIPT_DLL_API |
121 |
virtual |
122 |
void |
123 |
dump(const std::string fileName) const; |
124 |
|
125 |
/** |
126 |
\brief |
127 |
Return the number of data points per sample. |
128 |
*/ |
129 |
ESCRIPT_DLL_API |
130 |
int |
131 |
getNumDPPSample() const; |
132 |
|
133 |
/** |
134 |
\brief |
135 |
Return the number of samples. |
136 |
*/ |
137 |
ESCRIPT_DLL_API |
138 |
int |
139 |
getNumSamples() const; |
140 |
|
141 |
/** |
142 |
\brief |
143 |
Return the shape information for the point data. |
144 |
|
145 |
The omission of a non-constant form is deliberate. |
146 |
*/ |
147 |
ESCRIPT_DLL_API |
148 |
const DataTypes::ShapeType& |
149 |
getShape() const; |
150 |
|
151 |
/** |
152 |
\brief |
153 |
Return the rank information for the point data. |
154 |
*/ |
155 |
ESCRIPT_DLL_API |
156 |
unsigned int |
157 |
getRank() const; |
158 |
|
159 |
|
160 |
|
161 |
/** |
162 |
\brief |
163 |
Return the offset for the given sample. This returns the offset for the given |
164 |
point into the container holding the point data. |
165 |
|
166 |
\param sampleNo - Input - sample number. |
167 |
\param dataPointNo - Input - data point number. |
168 |
*/ |
169 |
ESCRIPT_DLL_API |
170 |
virtual |
171 |
ValueType::size_type |
172 |
getPointOffset(int sampleNo, |
173 |
int dataPointNo) const = 0; |
174 |
|
175 |
ESCRIPT_DLL_API |
176 |
virtual |
177 |
ValueType::size_type |
178 |
getPointOffset(int sampleNo, |
179 |
int dataPointNo) = 0; |
180 |
|
181 |
// /** |
182 |
// return the container storing points for this object |
183 |
// */ |
184 |
// ESCRIPT_DLL_API |
185 |
// virtual |
186 |
// ValueType& |
187 |
// getVector(); |
188 |
// |
189 |
// ESCRIPT_DLL_API |
190 |
// virtual |
191 |
// const ValueType& |
192 |
// getVector() const; |
193 |
|
194 |
|
195 |
// /** |
196 |
// \brief |
197 |
// Return the sample data for the given sample number. |
198 |
// */ |
199 |
// ESCRIPT_DLL_API |
200 |
// double* |
201 |
// getSampleData(ValueType::size_type sampleNo); |
202 |
|
203 |
/** |
204 |
\brief |
205 |
Return the number of doubles stored for this Data object. |
206 |
*/ |
207 |
ESCRIPT_DLL_API |
208 |
virtual |
209 |
ValueType::size_type |
210 |
getLength() const = 0; |
211 |
|
212 |
/** |
213 |
\brief |
214 |
Return the sample data for the given tag key. |
215 |
NB: If the data isn't tagged an exception will be thrown. |
216 |
*/ |
217 |
ESCRIPT_DLL_API |
218 |
virtual |
219 |
double* |
220 |
getSampleDataByTag(int tag); |
221 |
|
222 |
/** |
223 |
This method is used primarily for LazyData. |
224 |
\return the size of the buffer required to evaulate a sample for this object. |
225 |
*/ |
226 |
ESCRIPT_DLL_API |
227 |
virtual size_t |
228 |
getSampleBufferSize() const=0; |
229 |
|
230 |
|
231 |
|
232 |
/** |
233 |
\brief |
234 |
Check this and the given RHS operands are compatible. Throws |
235 |
an exception if they aren't. |
236 |
|
237 |
\param right - Input - The right hand side. |
238 |
*/ |
239 |
ESCRIPT_DLL_API |
240 |
void |
241 |
operandCheck(const DataAbstract& right) const; |
242 |
|
243 |
/** |
244 |
\brief |
245 |
Return true if a valid sample point number. |
246 |
*/ |
247 |
ESCRIPT_DLL_API |
248 |
bool |
249 |
validSamplePointNo(int samplePointNo) const; |
250 |
|
251 |
/** |
252 |
\brief |
253 |
Return true if a valid sample number. |
254 |
*/ |
255 |
ESCRIPT_DLL_API |
256 |
bool |
257 |
validSampleNo(int sampleNo) const; |
258 |
|
259 |
|
260 |
/** |
261 |
\brief |
262 |
Return the function space associated with this Data object. |
263 |
*/ |
264 |
ESCRIPT_DLL_API |
265 |
const |
266 |
FunctionSpace& |
267 |
getFunctionSpace() const; |
268 |
|
269 |
/** |
270 |
\brief |
271 |
Return the given slice from this object. |
272 |
|
273 |
NB: The caller is responsible for managing the object created. |
274 |
*/ |
275 |
ESCRIPT_DLL_API |
276 |
virtual |
277 |
DataAbstract* |
278 |
getSlice(const DataTypes::RegionType& region) const = 0; |
279 |
|
280 |
// /** |
281 |
// \brief |
282 |
// Copy the specified region from the given object. |
283 |
// |
284 |
// \param value - Input - Data to copy from |
285 |
// \param region - Input - Region to copy. |
286 |
// */ |
287 |
// ESCRIPT_DLL_API |
288 |
// virtual |
289 |
// void |
290 |
// setSlice(const DataAbstract* value, |
291 |
// const DataTypes::RegionType& region) = 0; |
292 |
|
293 |
/** |
294 |
\brief |
295 |
setTaggedValue |
296 |
|
297 |
Description: |
298 |
Assign the given value to the given tag. |
299 |
|
300 |
NB: If the data isn't tagged an exception will be thrown. |
301 |
|
302 |
\param tagKey - Input - Integer key. |
303 |
\param pointshape - Input - the shape of the value parameter. |
304 |
\param value - Input - |
305 |
*/ |
306 |
ESCRIPT_DLL_API |
307 |
virtual |
308 |
void |
309 |
setTaggedValue(int tagKey, |
310 |
const DataTypes::ShapeType& pointshape, |
311 |
const DataTypes::ValueType& value, |
312 |
int dataOffset=0); |
313 |
|
314 |
|
315 |
|
316 |
|
317 |
/** |
318 |
\brief |
319 |
Copy the numarray object to the data points in this object. |
320 |
|
321 |
Description: |
322 |
Copy the numarray object to the data points in this object. |
323 |
|
324 |
\param value Input - new values for the data points |
325 |
*/ |
326 |
ESCRIPT_DLL_API |
327 |
virtual void |
328 |
copyAll(const boost::python::numeric::array& value); |
329 |
|
330 |
/** |
331 |
\brief |
332 |
Copy a double value to the data point dataPointNo of sample sampleNo in this object. |
333 |
|
334 |
Description: |
335 |
Copy a double value to the data point dataPointNo of sample sampleNo in this object. |
336 |
|
337 |
\param sampleNo Input - sample number |
338 |
\param dataPointNo Input - data point of the sample |
339 |
\param value Input - new values for the data point |
340 |
*/ |
341 |
ESCRIPT_DLL_API |
342 |
virtual void |
343 |
copyToDataPoint(const int sampleNo, const int dataPointNo, const double value); |
344 |
|
345 |
/** |
346 |
\brief |
347 |
Copy the numarray object to the data point dataPointNo of sample sampleNo in this object. |
348 |
|
349 |
Description: |
350 |
Copy the numarray object to the data point dataPointNo of sample sampleNo in this object. |
351 |
|
352 |
\param sampleNo Input - sample number |
353 |
\param dataPointNo Input - data point of the sample |
354 |
\param value Input - new values for the data point |
355 |
*/ |
356 |
ESCRIPT_DLL_API |
357 |
virtual void |
358 |
copyToDataPoint(const int sampleNo, const int dataPointNo, const boost::python::numeric::array& value); |
359 |
|
360 |
|
361 |
/** |
362 |
\brief |
363 |
Return the tag number associated with the given data-point number. |
364 |
|
365 |
If the object cannot be referenced by tag numbers, an exception |
366 |
will be thrown. |
367 |
*/ |
368 |
ESCRIPT_DLL_API |
369 |
virtual |
370 |
int |
371 |
getTagNumber(int dpno); |
372 |
|
373 |
/** |
374 |
\brief |
375 |
Computes a symmetric matrix (A + AT) / 2 |
376 |
|
377 |
\param ev - Output - a symmetric matrix |
378 |
|
379 |
*/ |
380 |
ESCRIPT_DLL_API |
381 |
virtual void |
382 |
symmetric(DataAbstract* ev); |
383 |
|
384 |
/** |
385 |
\brief |
386 |
Computes a nonsymmetric matrix (A - AT) / 2 |
387 |
|
388 |
\param ev - Output - a nonsymmetric matrix |
389 |
|
390 |
*/ |
391 |
ESCRIPT_DLL_API |
392 |
virtual void |
393 |
nonsymmetric(DataAbstract* ev); |
394 |
|
395 |
/** |
396 |
\brief |
397 |
Computes the trace of a matrix |
398 |
|
399 |
\param ev - Output - the trace of a matrix |
400 |
|
401 |
*/ |
402 |
ESCRIPT_DLL_API |
403 |
virtual void |
404 |
trace(DataAbstract* ev, int axis_offset); |
405 |
|
406 |
/** |
407 |
\brief |
408 |
Transpose each data point of this Data object around the given axis. |
409 |
|
410 |
\param ev - Output - the transpose of a matrix |
411 |
|
412 |
*/ |
413 |
ESCRIPT_DLL_API |
414 |
virtual void |
415 |
transpose(DataAbstract* ev, int axis_offset); |
416 |
|
417 |
/** |
418 |
\brief |
419 |
swaps components axis0 and axis1 |
420 |
|
421 |
\param ev - Output - swapped components |
422 |
|
423 |
*/ |
424 |
ESCRIPT_DLL_API |
425 |
virtual void |
426 |
swapaxes(DataAbstract* ev, int axis0, int axis1); |
427 |
/** |
428 |
\brief |
429 |
solves the eigenvalue problem this*V=ev*V for the eigenvalues ev |
430 |
|
431 |
\param ev - Output - eigenvalues in increasing order at each data point |
432 |
|
433 |
*/ |
434 |
ESCRIPT_DLL_API |
435 |
virtual void |
436 |
eigenvalues(DataAbstract* ev); |
437 |
|
438 |
/** |
439 |
\brief |
440 |
sets values to zero |
441 |
|
442 |
*/ |
443 |
ESCRIPT_DLL_API |
444 |
virtual void |
445 |
setToZero(); |
446 |
|
447 |
/** |
448 |
\brief |
449 |
solves the eigenvalue problem this*V=ev*V for the eigenvalues ev and eigenvectors V |
450 |
|
451 |
\param ev - Output - eigenvalues in increasing order at each data point |
452 |
\param V - Output - corresponding eigenvectors. They are normalized such that their length is one |
453 |
and the first nonzero component is positive. |
454 |
\param tol - Input - eigenvalue with relative distance tol are treated as equal. |
455 |
|
456 |
*/ |
457 |
|
458 |
ESCRIPT_DLL_API |
459 |
virtual void |
460 |
eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol=1.e-13); |
461 |
|
462 |
/** |
463 |
\brief |
464 |
reorders data sample ordered by reference_ids to the ordering of the functions space |
465 |
|
466 |
\param reference_ids - Input - reference_ids used for current ordering |
467 |
*/ |
468 |
ESCRIPT_DLL_API |
469 |
virtual void |
470 |
reorderByReferenceIDs(int *reference_ids); |
471 |
|
472 |
|
473 |
|
474 |
/** |
475 |
\brief |
476 |
Return the number of values in the shape for this object. |
477 |
*/ |
478 |
ESCRIPT_DLL_API |
479 |
unsigned int |
480 |
getNoValues() const; |
481 |
|
482 |
|
483 |
|
484 |
// /** |
485 |
// \brief get a reference to the beginning of a data point |
486 |
// */ |
487 |
// ESCRIPT_DLL_API |
488 |
// DataTypes::ValueType::const_reference |
489 |
// getDataAtOffset(DataTypes::ValueType::size_type i) const; |
490 |
// |
491 |
// |
492 |
// ESCRIPT_DLL_API |
493 |
// DataTypes::ValueType::reference |
494 |
// getDataAtOffset(DataTypes::ValueType::size_type i); |
495 |
|
496 |
|
497 |
// /** |
498 |
// \brief Provide access to underlying storage. Internal use only! |
499 |
// */ |
500 |
// ESCRIPT_DLL_API |
501 |
// virtual DataTypes::ValueType& |
502 |
// getVector()=0; |
503 |
// |
504 |
// ESCRIPT_DLL_API |
505 |
// virtual const DataTypes::ValueType& |
506 |
// getVector() const=0; |
507 |
|
508 |
ESCRIPT_DLL_API |
509 |
bool isLazy() const; // a test to determine if this object is an instance of DataLazy |
510 |
|
511 |
ESCRIPT_DLL_API |
512 |
virtual |
513 |
bool |
514 |
isConstant() const {return false;} |
515 |
|
516 |
ESCRIPT_DLL_API |
517 |
virtual |
518 |
bool |
519 |
isExpanded() const {return false;} |
520 |
|
521 |
|
522 |
/** |
523 |
\brief |
524 |
Return true if this Data is expanded or resolves to expanded. |
525 |
That is, if it has a separate value for each datapoint in the sample. |
526 |
*/ |
527 |
virtual |
528 |
bool |
529 |
actsExpanded() const {return false;} |
530 |
|
531 |
virtual |
532 |
bool |
533 |
isTagged() const {return false;} |
534 |
|
535 |
ESCRIPT_DLL_API |
536 |
bool isEmpty() const; // a fast test to determine if this object is an instance of DataEmpty |
537 |
|
538 |
protected: |
539 |
|
540 |
|
541 |
|
542 |
private: |
543 |
|
544 |
// |
545 |
// The number of samples in this Data object. |
546 |
// This is derived directly from the FunctionSpace. |
547 |
int m_noSamples; |
548 |
|
549 |
// |
550 |
// The number of data points per sample in this Data object. |
551 |
// This is derived directly from the FunctionSpace. |
552 |
int m_noDataPointsPerSample; |
553 |
|
554 |
// |
555 |
// A FunctionSpace which provides a description of the data associated |
556 |
// with this Data object. |
557 |
FunctionSpace m_functionSpace; |
558 |
|
559 |
// |
560 |
// The shape of the points stored in this view |
561 |
DataTypes::ShapeType m_shape; |
562 |
|
563 |
// |
564 |
// The number of values in each point |
565 |
unsigned int m_novalues; |
566 |
|
567 |
// |
568 |
// The rank of the points stored in this view |
569 |
unsigned int m_rank; |
570 |
|
571 |
// |
572 |
// Is this an instance of DataEmpty? |
573 |
bool m_isempty; |
574 |
}; |
575 |
|
576 |
inline |
577 |
bool |
578 |
DataAbstract::isEmpty() const |
579 |
{ |
580 |
return m_isempty; |
581 |
} |
582 |
|
583 |
// inline |
584 |
// DataTypes::ValueType::const_reference |
585 |
// DataAbstract::getDataAtOffset(DataTypes::ValueType::size_type i) const |
586 |
// { |
587 |
// if (isLazy()) |
588 |
// { |
589 |
// throw DataException("Programmer error - getDataAtOffset() not permitted on Lazy Data."); |
590 |
// } |
591 |
// DataReady* d; |
592 |
// return (dynamic_cast<const DataReady*>(this))->getVector()[i]; |
593 |
// // return getVector()[i]; |
594 |
// } |
595 |
|
596 |
// inline |
597 |
// DataTypes::ValueType::reference |
598 |
// DataAbstract::getDataAtOffset(DataTypes::ValueType::size_type i) |
599 |
// { |
600 |
// if (isLazy()) |
601 |
// { |
602 |
// throw DataException("Programmer error - getDataAtOffset() not permitted on Lazy Data."); |
603 |
// } |
604 |
// return dynamic_cast<DataReady*>(this)->getVector()[i]; |
605 |
// // return getVector()[i]; |
606 |
// } |
607 |
|
608 |
|
609 |
inline |
610 |
bool |
611 |
DataAbstract::validSamplePointNo(int samplePointNo) const |
612 |
{ |
613 |
return ((0 <= samplePointNo) && (samplePointNo < m_noDataPointsPerSample)); |
614 |
} |
615 |
|
616 |
inline |
617 |
bool |
618 |
DataAbstract::validSampleNo(int sampleNo) const |
619 |
{ |
620 |
return ((0 <= sampleNo) && (sampleNo < m_noSamples)); |
621 |
} |
622 |
|
623 |
inline |
624 |
int |
625 |
DataAbstract::getNumDPPSample() const |
626 |
{ |
627 |
if (isEmpty()) |
628 |
{ |
629 |
throw DataException("Error - Operations not permitted on instances of DataEmpty."); |
630 |
} |
631 |
return m_noDataPointsPerSample; |
632 |
} |
633 |
|
634 |
inline |
635 |
int |
636 |
DataAbstract::getNumSamples() const |
637 |
{ |
638 |
if (isEmpty()) |
639 |
{ |
640 |
throw DataException("Error - Operations not permitted on instances of DataEmpty."); |
641 |
} |
642 |
return m_noSamples; |
643 |
} |
644 |
|
645 |
inline |
646 |
const |
647 |
FunctionSpace& |
648 |
DataAbstract::getFunctionSpace() const |
649 |
{ |
650 |
return m_functionSpace; |
651 |
} |
652 |
|
653 |
inline |
654 |
const DataTypes::ShapeType& |
655 |
DataAbstract::getShape() const |
656 |
{ |
657 |
if (isEmpty()) |
658 |
{ |
659 |
throw DataException("Error - Operations not permitted on instances of DataEmpty."); |
660 |
} |
661 |
return m_shape; |
662 |
} |
663 |
|
664 |
inline |
665 |
unsigned int |
666 |
DataAbstract::getRank() const |
667 |
{ |
668 |
if (isEmpty()) |
669 |
{ |
670 |
throw DataException("Error - Operations not permitted on instances of DataEmpty."); |
671 |
} |
672 |
return m_rank; |
673 |
} |
674 |
|
675 |
inline |
676 |
unsigned int |
677 |
DataAbstract::getNoValues() const |
678 |
{ |
679 |
if (isEmpty()) |
680 |
{ |
681 |
throw DataException("Error - Operations not permitted on instances of DataEmpty."); |
682 |
} |
683 |
return m_novalues; |
684 |
} |
685 |
|
686 |
|
687 |
} // end of namespace |
688 |
|
689 |
#endif |