1 |
// $Id$ |
2 |
|
3 |
/* |
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 |
|
15 |
#if !defined escript_DataTagged_20040615_H |
16 |
#define escript_DataTagged_20040615_H |
17 |
#include "system_dep.h" |
18 |
|
19 |
#include "DataAbstract.h" |
20 |
#include "DataArrayView.h" |
21 |
|
22 |
#include <vector> |
23 |
#include <map> |
24 |
|
25 |
namespace escript { |
26 |
|
27 |
class DataConstant; |
28 |
|
29 |
/** |
30 |
\brief |
31 |
Simulates a full dataset accessible via sampleNo and dataPointNo. |
32 |
|
33 |
Description: |
34 |
Each data-point has an associated tag number, and a given tag can represent a |
35 |
range of dataPointNo and sampleNo. Each tag indexes only a single data-point. |
36 |
Thus only a single data-point needs to be stored for a range of sampleNo and |
37 |
dataPointNo values. |
38 |
*/ |
39 |
|
40 |
class DataTagged : public DataAbstract { |
41 |
|
42 |
public: |
43 |
|
44 |
// |
45 |
// Types for the lists of tags and values. |
46 |
typedef std::vector<int> TagListType; |
47 |
typedef std::vector<DataArrayView> ValueListType; |
48 |
typedef DataArrayView::ValueType ValueType; |
49 |
|
50 |
// |
51 |
// Map from a tag to an offset into the data array. |
52 |
typedef std::map<int, int> DataMapType; |
53 |
|
54 |
/** |
55 |
\brief |
56 |
Default constructor for DataTagged. |
57 |
|
58 |
Description: |
59 |
Default constructor for DataTagged. Creates a DataTagged object for which |
60 |
the default data-point is a scalar data-point with value 0.0, and no other |
61 |
tag values are stored. |
62 |
T |
63 |
*/ |
64 |
ESCRIPT_DLL_API |
65 |
DataTagged(); |
66 |
|
67 |
/** |
68 |
\brief |
69 |
Constructor for DataTagged. |
70 |
|
71 |
Description: |
72 |
Constructor for DataTagged. |
73 |
\param tagKeys - Input - A vector of integer tags. |
74 |
\param values - Input - A vector of DataArrayViews. If this is empty |
75 |
all tag values will be assigned a scalar data-point of value |
76 |
0. If it contains one value all tag values will be assigned |
77 |
this value. Otherwise consecutive tags will be assigned |
78 |
consecutive values. If there is a mismatch between the |
79 |
number of keys and the number of values an exception |
80 |
will be generated. |
81 |
\param defaultValue - Input - Value returned if a requested tag doesn't exist. |
82 |
\param what - Input - A description of what this data represents. |
83 |
T |
84 |
*/ |
85 |
ESCRIPT_DLL_API |
86 |
DataTagged(const TagListType& tagKeys, |
87 |
const ValueListType& values, |
88 |
const DataArrayView& defaultValue, |
89 |
const FunctionSpace& what); |
90 |
|
91 |
/** |
92 |
\brief |
93 |
Alternative Constructor for DataTagged. |
94 |
|
95 |
Description: |
96 |
Alternative Constructor for DataTagged. |
97 |
\param what - Input - A description of what this data object represents. |
98 |
\param shape - Input - The shape of each data-point. |
99 |
\param tags - Input - An array of tags, one for each sample number. |
100 |
\param data - The data values for each tag. |
101 |
NB: no unit testing yet |
102 |
*/ |
103 |
ESCRIPT_DLL_API |
104 |
DataTagged(const FunctionSpace& what, |
105 |
const DataArrayView::ShapeType &shape, |
106 |
const int tags[], |
107 |
const ValueType& data); |
108 |
|
109 |
/** |
110 |
\brief |
111 |
Alternative Constructor for DataTagged. |
112 |
|
113 |
Description: |
114 |
Alternative Constructor for DataTagged. |
115 |
\param what - Input - A description of what this data object represents. |
116 |
\param shape - Input - The shape of each data-point. |
117 |
\param tags - Input - An vector of tags, one for each sample number. |
118 |
\param data - The data values for each tag. |
119 |
NB: no unit testing yet |
120 |
*/ |
121 |
ESCRIPT_DLL_API |
122 |
DataTagged(const FunctionSpace& what, |
123 |
const DataArrayView::ShapeType &shape, |
124 |
const TagListType& tags, |
125 |
const ValueType& data); |
126 |
|
127 |
/** |
128 |
\brief |
129 |
Copy Constructor for DataTagged. |
130 |
Performs a deep copy from the given DataTagged object. |
131 |
T |
132 |
*/ |
133 |
ESCRIPT_DLL_API |
134 |
DataTagged(const DataTagged& other); |
135 |
|
136 |
/** |
137 |
\brief |
138 |
Copy Constructor for DataTagged. |
139 |
Construct a DataTagged object from a DataConstant object. |
140 |
The default value will be the value of the DataConstant object. |
141 |
T |
142 |
*/ |
143 |
ESCRIPT_DLL_API |
144 |
DataTagged(const DataConstant& other); |
145 |
|
146 |
/** |
147 |
\brief |
148 |
getSampleDataByTag |
149 |
|
150 |
Description: |
151 |
Return the data-point for the given tag. All of the data for the |
152 |
sample will be visible via the returned pointer. |
153 |
|
154 |
** This provides an interface into the data suitable for legacy C code. |
155 |
** NB: need to do array bounds checking when accessing returned value! |
156 |
T |
157 |
*/ |
158 |
ESCRIPT_DLL_API |
159 |
virtual |
160 |
double* |
161 |
getSampleDataByTag(int tag); |
162 |
|
163 |
/** |
164 |
\brief |
165 |
Write the data as a string. |
166 |
Writes out each tag, including the default, and the data-point which is |
167 |
associated with each tag. |
168 |
T |
169 |
*/ |
170 |
ESCRIPT_DLL_API |
171 |
virtual |
172 |
std::string |
173 |
toString() const; |
174 |
/** |
175 |
\brief |
176 |
dumps the object into a netCDF file |
177 |
*/ |
178 |
ESCRIPT_DLL_API |
179 |
virtual |
180 |
void |
181 |
dump(const std::string fileName) const; |
182 |
|
183 |
/** |
184 |
\brief |
185 |
Return the tag number associated with the given data-point number |
186 |
according to the associated function space. |
187 |
T |
188 |
*/ |
189 |
ESCRIPT_DLL_API |
190 |
virtual |
191 |
int |
192 |
getTagNumber(int dpno); |
193 |
|
194 |
/** |
195 |
\brief |
196 |
getPointOffset |
197 |
|
198 |
Description: |
199 |
Return the offset to the given data-point value in the underlying |
200 |
data vector. |
201 |
|
202 |
\param sampleNo - Input - sample number. |
203 |
\param dataPointNo - Input - data-point number. |
204 |
T |
205 |
*/ |
206 |
ESCRIPT_DLL_API |
207 |
virtual |
208 |
ValueType::size_type |
209 |
getPointOffset(int sampleNo, |
210 |
int dataPointNo) const; |
211 |
|
212 |
/** |
213 |
\brief |
214 |
addTaggedValues |
215 |
|
216 |
Description: |
217 |
Add the given tags and values to this DataTagged object. |
218 |
\param tagKeys - Input - A vector of integer tags. |
219 |
\param values - Input - A vector of DataArrayViews. If this is empty |
220 |
all tag values will be assigned a scalar data-point of value |
221 |
0. If it contains one value all tag values will be assigned |
222 |
this value. Otherwise consecutive tags will be assigned |
223 |
consecutive values. If there is a mismatch between the |
224 |
number of keys and the number of values an exception |
225 |
will be generated. |
226 |
T |
227 |
*/ |
228 |
ESCRIPT_DLL_API |
229 |
void |
230 |
addTaggedValues(const TagListType& tagKeys, |
231 |
const ValueListType& values); |
232 |
|
233 |
/** |
234 |
\brief |
235 |
addTaggedValue |
236 |
|
237 |
Description: |
238 |
Add a single tag and value to this DataTagged object. If this tag already has |
239 |
a value associated with it, setTaggedValue will be used to update this value. |
240 |
\param tagKey - Input - Integer tag. |
241 |
\param value - Input - Single DataArrayView value to be assigned to the tag. |
242 |
T |
243 |
*/ |
244 |
ESCRIPT_DLL_API |
245 |
void |
246 |
addTaggedValue(int tagKey, |
247 |
const DataArrayView& value); |
248 |
|
249 |
/** |
250 |
\brief |
251 |
setTaggedValues |
252 |
|
253 |
Description: |
254 |
Set the given tags to the given values in this DataTagged object. |
255 |
\param tagKeys - Input - A vector of integer tag. |
256 |
\param values - Input - A vector of DataArrayViews. If this is empty |
257 |
all tag values will be assigned a scalar data-point of value |
258 |
0. If it contains one value all tag values will be assigned |
259 |
this value. Otherwise consecutive tags will be assigned |
260 |
consecutive values. If there is a mismatch between the |
261 |
number of keys and the number of values an exception |
262 |
will be generated. |
263 |
T |
264 |
*/ |
265 |
ESCRIPT_DLL_API |
266 |
void |
267 |
setTaggedValues(const TagListType& tagKeys, |
268 |
const ValueListType& values); |
269 |
|
270 |
/** |
271 |
\brief |
272 |
setTaggedValue |
273 |
|
274 |
Description: |
275 |
Assign the given value to the given tag. |
276 |
\param tagKey - Input - Integer tag. |
277 |
\param value - Input - Single DataArrayView value to be assigned to the tag. |
278 |
T |
279 |
*/ |
280 |
ESCRIPT_DLL_API |
281 |
virtual |
282 |
void |
283 |
setTaggedValue(int tagKey, |
284 |
const DataArrayView& value); |
285 |
|
286 |
/** |
287 |
\brief |
288 |
getDataPointByTag |
289 |
|
290 |
Description: |
291 |
Return data-point associated with the given tag as a DataArrayView. |
292 |
\param tag - Input - Integer key. |
293 |
T |
294 |
*/ |
295 |
ESCRIPT_DLL_API |
296 |
DataArrayView |
297 |
getDataPointByTag(int tag) const; |
298 |
|
299 |
/** |
300 |
\brief |
301 |
getDataPoint |
302 |
|
303 |
Description: |
304 |
Return the data-point specified by the given sample and data-point |
305 |
numbers as a DataArrayView. |
306 |
\param sampleNo - Input. |
307 |
\param dataPointNo - Input. |
308 |
T |
309 |
*/ |
310 |
ESCRIPT_DLL_API |
311 |
virtual |
312 |
DataArrayView |
313 |
getDataPoint(int sampleNo, |
314 |
int dataPointNo); |
315 |
|
316 |
/** |
317 |
\brief |
318 |
getData |
319 |
|
320 |
Description: |
321 |
Return pointer to the data |
322 |
T |
323 |
*/ |
324 |
ESCRIPT_DLL_API |
325 |
const DataArrayView::ValueType::ElementType* |
326 |
getData() const; |
327 |
|
328 |
/** |
329 |
\brief |
330 |
getTagLookup |
331 |
|
332 |
Description: |
333 |
Return a reference to the tag offset lookup table. |
334 |
T |
335 |
*/ |
336 |
ESCRIPT_DLL_API |
337 |
const DataMapType& |
338 |
getTagLookup() const; |
339 |
|
340 |
/** |
341 |
\brief |
342 |
isCurrentTag |
343 |
|
344 |
Description: |
345 |
Return true if the given tag exists within the DataTagged tag map. |
346 |
|
347 |
*** NB: The DataTagged tag map does not necessarily coincide with the tag |
348 |
keys in the associated function space. |
349 |
T |
350 |
*/ |
351 |
ESCRIPT_DLL_API |
352 |
bool |
353 |
isCurrentTag(int tag) const; |
354 |
|
355 |
/** |
356 |
\brief |
357 |
getDefaultValue |
358 |
|
359 |
Description: |
360 |
Return the default value. This value is associated with any tag which |
361 |
is not explicitly recorded in this DataTagged object's tag map. |
362 |
T |
363 |
*/ |
364 |
ESCRIPT_DLL_API |
365 |
DataArrayView& |
366 |
getDefaultValue(); |
367 |
|
368 |
ESCRIPT_DLL_API |
369 |
const DataArrayView& |
370 |
getDefaultValue() const; |
371 |
|
372 |
/** |
373 |
\brief |
374 |
getLength |
375 |
|
376 |
Description: |
377 |
Return the total number of doubles stored for this DataTagged object. |
378 |
T |
379 |
*/ |
380 |
ESCRIPT_DLL_API |
381 |
virtual |
382 |
ValueType::size_type |
383 |
getLength() const; |
384 |
|
385 |
/** |
386 |
\brief |
387 |
getSlice |
388 |
|
389 |
Description: |
390 |
Factory method that returns a newly created DataTagged object generated |
391 |
by taking the specified slice from this DataTagged object. |
392 |
The caller is reponsible for managing the returned object. |
393 |
T |
394 |
*/ |
395 |
ESCRIPT_DLL_API |
396 |
virtual |
397 |
DataAbstract* |
398 |
getSlice(const DataArrayView::RegionType& region) const; |
399 |
|
400 |
/** |
401 |
\brief |
402 |
Slice Constructor for DataTagged. |
403 |
|
404 |
Description: |
405 |
Creates a DataTagged object which is the specified slice |
406 |
from the given DataTagged object. |
407 |
\param other - Input - DataTagged object to slice from. |
408 |
\param region - Input - Region to slice. |
409 |
T |
410 |
*/ |
411 |
ESCRIPT_DLL_API |
412 |
DataTagged(const DataTagged& other, |
413 |
const DataArrayView::RegionType& region); |
414 |
|
415 |
/** |
416 |
\brief |
417 |
setSlice |
418 |
|
419 |
Description: |
420 |
Copy the given Data object into the specified region in this object. |
421 |
\param other - Input - Data object to copy from. |
422 |
\param region - Input - Region to copy into (NB: must have same shape as other!). |
423 |
T |
424 |
*/ |
425 |
ESCRIPT_DLL_API |
426 |
virtual |
427 |
void |
428 |
setSlice(const DataAbstract* other, |
429 |
const DataArrayView::RegionType& region); |
430 |
|
431 |
/** |
432 |
\brief |
433 |
Archive the underlying data values to the file referenced |
434 |
by ofstream. A count of the number of values expected to be written |
435 |
is provided as a cross-check. |
436 |
|
437 |
The return value indicates success (0) or otherwise (1). |
438 |
*/ |
439 |
ESCRIPT_DLL_API |
440 |
int |
441 |
archiveData(std::ofstream& archiveFile, |
442 |
const DataArrayView::ValueType::size_type noValues) const; |
443 |
|
444 |
/** |
445 |
\brief |
446 |
Extract the number of values specified by noValues from the file |
447 |
referenced by ifstream to the underlying data structure. |
448 |
|
449 |
The return value indicates success (0) or otherwise (1). |
450 |
*/ |
451 |
ESCRIPT_DLL_API |
452 |
int |
453 |
extractData(std::ifstream& archiveFile, |
454 |
const DataArrayView::ValueType::size_type noValues); |
455 |
|
456 |
/** |
457 |
\brief |
458 |
Computes a symmetric matrix (A + AT) / 2 |
459 |
|
460 |
\param ev - Output - symmetric matrix |
461 |
|
462 |
*/ |
463 |
ESCRIPT_DLL_API |
464 |
virtual void |
465 |
symmetric(DataAbstract* ev); |
466 |
|
467 |
/** |
468 |
\brief |
469 |
Computes a nonsymmetric matrix (A - AT) / 2 |
470 |
|
471 |
\param ev - Output - nonsymmetric matrix |
472 |
|
473 |
*/ |
474 |
ESCRIPT_DLL_API |
475 |
virtual void |
476 |
nonsymmetric(DataAbstract* ev); |
477 |
|
478 |
/** |
479 |
\brief |
480 |
Computes the trace of a matrix |
481 |
|
482 |
\param ev - Output - the trace of a matrix |
483 |
|
484 |
*/ |
485 |
ESCRIPT_DLL_API |
486 |
virtual void |
487 |
trace(DataAbstract* ev, int axis_offset); |
488 |
|
489 |
/** |
490 |
\brief |
491 |
swaps components axis0 and axis1 |
492 |
|
493 |
\param ev - Output - swapped components |
494 |
|
495 |
*/ |
496 |
ESCRIPT_DLL_API |
497 |
virtual void |
498 |
swapaxes(DataAbstract* ev, int axis0, int axis1); |
499 |
|
500 |
/** |
501 |
\brief |
502 |
Transpose each data point of this Data object around the given axis. |
503 |
|
504 |
\param ev - Output - the transpose of a matrix |
505 |
|
506 |
*/ |
507 |
ESCRIPT_DLL_API |
508 |
virtual void |
509 |
transpose(DataAbstract* ev, int axis_offset); |
510 |
|
511 |
/** |
512 |
\brief |
513 |
solves the eigenvalue problem this*V=ev*V for the eigenvalues ev |
514 |
|
515 |
\param ev - Output - eigenvalues in increasing order at each data point |
516 |
|
517 |
*/ |
518 |
ESCRIPT_DLL_API |
519 |
virtual void |
520 |
eigenvalues(DataAbstract* ev); |
521 |
|
522 |
/** |
523 |
\brief |
524 |
solves the eigenvalue problem this*V=ev*V for the eigenvalues ev and eigenvectors V |
525 |
|
526 |
\param ev - Output - eigenvalues in increasing order at each data point |
527 |
\param V - Output - corresponding eigenvectors. They are normalized such that their length is one |
528 |
and the first nonzero component is positive. |
529 |
\param tol - Input - eigenvalue with relative distance tol are treated as equal. |
530 |
|
531 |
*/ |
532 |
|
533 |
ESCRIPT_DLL_API |
534 |
virtual void |
535 |
eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol=1.e-13); |
536 |
|
537 |
|
538 |
protected: |
539 |
|
540 |
private: |
541 |
|
542 |
// |
543 |
// The offset lookup table |
544 |
DataMapType m_offsetLookup; |
545 |
|
546 |
// |
547 |
// the offset to the default value |
548 |
static const int m_defaultValueOffset = 0; |
549 |
|
550 |
// |
551 |
// The actual data |
552 |
ValueType m_data; |
553 |
|
554 |
}; |
555 |
|
556 |
inline |
557 |
bool |
558 |
DataTagged::isCurrentTag(int tag) const |
559 |
{ |
560 |
DataMapType::const_iterator pos(m_offsetLookup.find(tag)); |
561 |
return (pos!=m_offsetLookup.end()); |
562 |
} |
563 |
|
564 |
inline |
565 |
DataArrayView& |
566 |
DataTagged::getDefaultValue() |
567 |
{ |
568 |
// The default value is always the first value. |
569 |
return getPointDataView(); |
570 |
} |
571 |
|
572 |
inline |
573 |
const DataArrayView& |
574 |
DataTagged::getDefaultValue() const |
575 |
{ |
576 |
// The default value is always the first value. |
577 |
return getPointDataView(); |
578 |
} |
579 |
|
580 |
inline |
581 |
const DataArrayView::ValueType::ElementType* |
582 |
DataTagged::getData() const |
583 |
{ |
584 |
return &(m_data[0]); |
585 |
} |
586 |
|
587 |
inline |
588 |
const DataTagged::DataMapType& |
589 |
DataTagged::getTagLookup() const |
590 |
{ |
591 |
return m_offsetLookup; |
592 |
} |
593 |
|
594 |
inline |
595 |
DataArrayView::ValueType::size_type |
596 |
DataTagged::getLength() const |
597 |
{ |
598 |
return m_data.size(); |
599 |
} |
600 |
|
601 |
} // end of namespace |
602 |
|
603 |
#endif |