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