/[escript]/trunk/escript/src/DataTagged.h
ViewVC logotype

Annotation of /trunk/escript/src/DataTagged.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2766 - (hide annotations)
Mon Nov 23 05:10:53 2009 UTC (10 years ago) by jfenwick
File MIME type: text/plain
File size: 16353 byte(s)
inf, sup and Lsup now correctly handle +-infinity.
They also will return NaN if any part of their input is NaN.

This will break unit tests since it exposes the hidden bug (#447 in mantis)

This code relies on the ability to test for NaNs.
To do this it makes use of macros and functions from C99.
If you do not have a C99 compiler, then you will probably get the old behaviour.
That is, you won't know when you have NaNs.


Also did minor tweak to saveDataCSV doco.
1 jgs 121
2 ksteube 1312 /*******************************************************
3 ksteube 1811 *
4 jfenwick 2548 * Copyright (c) 2003-2009 by University of Queensland
5 ksteube 1811 * 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 ksteube 1312
14 ksteube 1811
15 jgs 82 #if !defined escript_DataTagged_20040615_H
16     #define escript_DataTagged_20040615_H
17 woo409 757 #include "system_dep.h"
18 jgs 82
19 jfenwick 2005 #include "DataReady.h"
20 jfenwick 1796 #include "DataTypes.h"
21 jgs 82
22     #include <vector>
23     #include <map>
24    
25     namespace escript {
26    
27     class DataConstant;
28    
29     /**
30     \brief
31 jgs 496 Simulates a full dataset accessible via sampleNo and dataPointNo.
32 jgs 82
33     Description:
34 jgs 496 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 jgs 82 Thus only a single data-point needs to be stored for a range of sampleNo and
37     dataPointNo values.
38     */
39    
40 jfenwick 2005 class DataTagged : public DataReady {
41     typedef DataReady parent;
42 jgs 82 public:
43    
44 jgs 119 //
45 jgs 496 // Types for the lists of tags and values.
46 jgs 121 typedef std::vector<int> TagListType;
47 jfenwick 1796 // typedef std::vector<DataArrayView> ValueListType;
48     typedef DataTypes::ValueType ValueType;
49     typedef std::vector<ValueType::ElementType> ValueBatchType;
50 jgs 82
51     //
52 jgs 496 // Map from a tag to an offset into the data array.
53 jgs 82 typedef std::map<int, int> DataMapType;
54    
55     /**
56     \brief
57     Default constructor for DataTagged.
58    
59     Description:
60     Default constructor for DataTagged. Creates a DataTagged object for which
61 jgs 509 the default data-point is a scalar data-point with value 0.0, and no other
62     tag values are stored.
63 jgs 507 T
64 jgs 82 */
65 woo409 757 ESCRIPT_DLL_API
66 jgs 82 DataTagged();
67    
68     /**
69     \brief
70 jgs 496 Alternative Constructor for DataTagged.
71 jgs 119
72     Description:
73 jgs 496 Alternative Constructor for DataTagged.
74 jgs 119 \param what - Input - A description of what this data object represents.
75 jgs 496 \param shape - Input - The shape of each data-point.
76 jfenwick 1796 \param tags - Input - An array of tags, one for each sample number (starts at tag[1]).
77 jgs 496 \param data - The data values for each tag.
78 jgs 535 NB: no unit testing yet
79 jgs 119 */
80 woo409 757 ESCRIPT_DLL_API
81 jgs 119 DataTagged(const FunctionSpace& what,
82 jfenwick 1796 const DataTypes::ShapeType &shape,
83 jgs 119 const int tags[],
84 jgs 500 const ValueType& data);
85 jgs 119
86 jfenwick 1796 /**
87 jgs 119 \brief
88 woo409 757 Alternative Constructor for DataTagged.
89    
90     Description:
91     Alternative Constructor for DataTagged.
92     \param what - Input - A description of what this data object represents.
93     \param shape - Input - The shape of each data-point.
94     \param tags - Input - An vector of tags, one for each sample number.
95     \param data - The data values for each tag.
96 jfenwick 1796 TODO Make sure to document the relationship between tags and data, ie: data also contains the default value
97     */
98 woo409 757 ESCRIPT_DLL_API
99     DataTagged(const FunctionSpace& what,
100 jfenwick 1796 const DataTypes::ShapeType &shape,
101 woo409 757 const TagListType& tags,
102     const ValueType& data);
103    
104     /**
105     \brief
106 jgs 496 Copy Constructor for DataTagged.
107 jgs 82 Performs a deep copy from the given DataTagged object.
108 jgs 507 T
109 jgs 82 */
110 woo409 757 ESCRIPT_DLL_API
111 jgs 82 DataTagged(const DataTagged& other);
112    
113     /**
114     \brief
115 jgs 496 Copy Constructor for DataTagged.
116 jgs 509 Construct a DataTagged object from a DataConstant object.
117     The default value will be the value of the DataConstant object.
118 jgs 507 T
119 jgs 82 */
120 woo409 757 ESCRIPT_DLL_API
121 jgs 82 DataTagged(const DataConstant& other);
122    
123     /**
124     \brief
125 jfenwick 1796 Copies the tags from a DataTagged into a new Data Tagged and assigns them the default value. ** Not unit tested **
126    
127     This is different from a deep copy because we are not copying shape or other information, just tags.
128     \param what - Input - FunctionSpace for the new DataTagged
129     \param shape - Input - Shape for points in the new DataTagged
130     \param defaultvalue - Input - Default value for new DataTagged
131     \param tagsource - Input - A DataTagged object which supplies the tags.
132     */
133     ESCRIPT_DLL_API
134     DataTagged(const FunctionSpace& what,
135     const DataTypes::ShapeType& shape,
136     const DataTypes::ValueType& defaultvalue,
137     const DataTagged* tagsource=0);
138    
139     /**
140     \brief
141 phornby 1628 Destructor
142     */
143     ESCRIPT_DLL_API
144     inline virtual
145     ~DataTagged() {};
146    
147 jfenwick 2005 ESCRIPT_DLL_API
148     bool
149     isTagged() const
150     {
151     return true;
152     };
153    
154 jfenwick 2766 /**
155     \brief Return true if any one of the datapoints contains a NaN.
156     */
157     ESCRIPT_DLL_API
158     bool
159     hasNaN() const;
160 jfenwick 2005
161 phornby 1628 /**
162 jfenwick 1799 \brief Return a deep copy of the current object.
163     */
164     ESCRIPT_DLL_API
165     virtual
166     DataAbstract*
167     deepCopy();
168    
169    
170     /**
171 phornby 1628 \brief
172 jgs 82 getSampleDataByTag
173    
174     Description:
175 jgs 496 Return the data-point for the given tag. All of the data for the
176     sample will be visible via the returned pointer.
177    
178     ** This provides an interface into the data suitable for legacy C code.
179 jgs 507 ** NB: need to do array bounds checking when accessing returned value!
180     T
181 jgs 82 */
182 woo409 757 ESCRIPT_DLL_API
183 jgs 82 virtual
184     double*
185     getSampleDataByTag(int tag);
186    
187     /**
188     \brief
189     Write the data as a string.
190     Writes out each tag, including the default, and the data-point which is
191     associated with each tag.
192 jgs 507 T
193 jgs 82 */
194 woo409 757 ESCRIPT_DLL_API
195 jgs 82 virtual
196     std::string
197     toString() const;
198 gross 950 /**
199     \brief
200     dumps the object into a netCDF file
201     */
202     ESCRIPT_DLL_API
203     virtual
204     void
205     dump(const std::string fileName) const;
206 jgs 82
207 jfenwick 2742 /**
208     \brief invert square matricies
209     \param out - Where to store the results
210     */
211     ESCRIPT_DLL_API
212     virtual void
213     matrixInverse(DataAbstract* out) const;
214    
215 gross 1118 /**
216     \brief
217     sets all values to zero
218     */
219     ESCRIPT_DLL_API
220     virtual
221     void
222     setToZero();
223    
224 jgs 82 /**
225     \brief
226 jgs 496 Return the tag number associated with the given data-point number
227     according to the associated function space.
228 jgs 507 T
229 jgs 149 */
230 woo409 757 ESCRIPT_DLL_API
231 jgs 149 virtual
232     int
233     getTagNumber(int dpno);
234    
235     /**
236     \brief
237 jgs 82 getPointOffset
238    
239     Description:
240 jgs 496 Return the offset to the given data-point value in the underlying
241     data vector.
242 jgs 82
243     \param sampleNo - Input - sample number.
244     \param dataPointNo - Input - data-point number.
245 jgs 507 T
246     */
247 woo409 757 ESCRIPT_DLL_API
248 jgs 82 virtual
249 jgs 121 ValueType::size_type
250 jgs 82 getPointOffset(int sampleNo,
251     int dataPointNo) const;
252    
253 jfenwick 2005 ESCRIPT_DLL_API
254     virtual
255     ValueType::size_type
256     getPointOffset(int sampleNo,
257     int dataPointNo);
258 jfenwick 1796
259     /**
260 jgs 82 \brief
261     addTaggedValues
262    
263     Description:
264 jgs 509 Add the given tags and values to this DataTagged object.
265 jgs 507 \param tagKeys - Input - A vector of integer tags.
266 jfenwick 1796 \param values - Input - A vector of doubles. If this is empty, the default value for
267     this DataTagged will be used for all tags.
268     If it contains one value all tag values will be assigned
269 jgs 509 this value. Otherwise consecutive tags will be assigned
270     consecutive values. If there is a mismatch between the
271     number of keys and the number of values an exception
272     will be generated.
273 jfenwick 1796 \param vShape - shape of the datapoints in "values"
274 jgs 507 T
275 jfenwick 1796 */
276     ESCRIPT_DLL_API
277     void
278     addTaggedValues(const TagListType& tagKeys,
279     const ValueBatchType& values,
280     const ShapeType& vShape);
281    
282    
283     /**
284     Description:
285     Add the given tags and values to this DataTagged object.
286     \param tagKeys - Input - A vector of integer tags.
287     \param values - Input - A DataVector containing the datapoints.
288     If this is empty, the default value for
289     this DataTagged will be used for all tags.
290     If it contains one value all tag values will be assigned
291     this value. Otherwise consecutive tags will be assigned
292     consecutive values. If there is a mismatch between the
293     number of keys and the number of values an exception
294     will be generated.
295     \param vShape - shape of the datapoints in "values"
296    
297     TODO Makesure this is properly unit tested
298 jgs 82 */
299 woo409 757 ESCRIPT_DLL_API
300 jgs 82 void
301     addTaggedValues(const TagListType& tagKeys,
302 jfenwick 1796 const ValueType& values,
303     const ShapeType& vShape);
304 jgs 82
305 jfenwick 1796
306    
307    
308 jgs 82 /**
309     \brief
310     addTaggedValue
311    
312     Description:
313 jgs 121 Add a single tag and value to this DataTagged object. If this tag already has
314     a value associated with it, setTaggedValue will be used to update this value.
315 jgs 509 \param tagKey - Input - Integer tag.
316 jfenwick 1796 \param pointshape - Shape of the value parameter
317     \param value - Input - Single DataArrayView value to be assigned to the tag.
318     \param dataOffset - Input - Offset of the beginning of the point in the value parameter
319 jgs 82 */
320 woo409 757 ESCRIPT_DLL_API
321 jgs 82 void
322     addTaggedValue(int tagKey,
323 jfenwick 1796 const DataTypes::ShapeType& pointshape,
324     const ValueType& value,
325     int dataOffset=0);
326 jgs 82
327     /**
328     \brief
329 jfenwick 1796 addTag - does not modify the default value for this object. ** Not unit tested **
330 jgs 82
331     Description:
332 jfenwick 1796 Add a single tag. The default value for this DataTagged will be associated with the tag.
333     If this tag already has a value associated with it, then no change will be made.
334     \param tagKey - Input - Integer tag.
335     TODO: Make sure this is unit tested
336 jgs 82 */
337 woo409 757 ESCRIPT_DLL_API
338 jgs 82 void
339 jfenwick 1796 addTag(int tagKey);
340 jgs 82
341     /**
342     \brief
343     setTaggedValue
344    
345     Description:
346 jgs 509 Assign the given value to the given tag.
347     \param tagKey - Input - Integer tag.
348 jfenwick 1796 \param pointshape - the shape of the value parameter
349     \param value - Input - Vector storing the datapoint to be assigned to the tag.
350     \param dataOffset - beginning of the datapoint within "value".
351 jgs 507 T
352 jgs 82 */
353 woo409 757 ESCRIPT_DLL_API
354 jgs 82 void
355     setTaggedValue(int tagKey,
356 jfenwick 1796 const DataTypes::ShapeType& pointshape,
357     const ValueType& value,
358     int dataOffset=0);
359 jgs 82
360     /**
361     \brief
362 jfenwick 1796 getDataByTag
363 jgs 82
364 jfenwick 1796 Return a pointer to the beginning of the datapoint with the specified tag.
365     TODO Eventually these should be inlined.
366 jgs 82 \param tag - Input - Integer key.
367 jfenwick 1796 \param i - position in the underlying datastructure
368 jgs 82 */
369    
370 jfenwick 1796 ESCRIPT_DLL_API
371     DataTypes::ValueType::reference
372 jfenwick 2271 getDataByTagRW(int tag, DataTypes::ValueType::size_type i);
373 jfenwick 1796
374 jfenwick 2271 ESCRIPT_DLL_API
375     DataTypes::ValueType::const_reference
376     getDataByTagRO(int tag, DataTypes::ValueType::size_type i) const;
377 jfenwick 1796
378 jfenwick 2271
379    
380 jgs 82 /**
381 jfenwick 1796 \brief
382     getOffsetForTag
383 jgs 82
384 jfenwick 1796 \param tag
385     \return the offset of the beginning of the datapoint corresponding to tag.
386    
387     Note: If the tag is not valid, the offset of the default value is returned instead.
388 jgs 82 */
389 woo409 757 ESCRIPT_DLL_API
390 jfenwick 1796 DataTypes::ValueType::size_type
391     getOffsetForTag(int tag) const;
392 jgs 82
393 jfenwick 1796
394 jgs 82 /**
395 gross 983 \brief
396 jfenwick 1796 Return a reference to the underlying DataVector.
397     */
398    
399     ESCRIPT_DLL_API
400     DataTypes::ValueType&
401 jfenwick 2271 getVectorRW();
402 jfenwick 1796
403     ESCRIPT_DLL_API
404     const DataTypes::ValueType&
405 jfenwick 2271 getVectorRO() const;
406 jfenwick 1796
407    
408    
409     /**
410 jgs 82 \brief
411     getTagLookup
412    
413     Description:
414     Return a reference to the tag offset lookup table.
415 jgs 507 T
416 jgs 82 */
417 woo409 757 ESCRIPT_DLL_API
418 jgs 82 const DataMapType&
419     getTagLookup() const;
420    
421     /**
422     \brief
423     isCurrentTag
424    
425     Description:
426 jgs 496 Return true if the given tag exists within the DataTagged tag map.
427    
428 jgs 509 *** NB: The DataTagged tag map does not necessarily coincide with the tag
429 jgs 496 keys in the associated function space.
430 jgs 507 T
431     */
432 woo409 757 ESCRIPT_DLL_API
433 jgs 82 bool
434     isCurrentTag(int tag) const;
435    
436     /**
437     \brief
438     getDefaultValue
439    
440     Description:
441     Return the default value. This value is associated with any tag which
442 jgs 496 is not explicitly recorded in this DataTagged object's tag map.
443 jfenwick 1796 \param i - position in the underlying datastructure
444 jgs 82 */
445 woo409 757 ESCRIPT_DLL_API
446 jfenwick 1796 DataTypes::ValueType::reference
447 jfenwick 2271 getDefaultValueRW(DataTypes::ValueType::size_type i);
448 jgs 82
449 woo409 757 ESCRIPT_DLL_API
450 jfenwick 1796 DataTypes::ValueType::const_reference
451 jfenwick 2271 getDefaultValueRO(DataTypes::ValueType::size_type i) const;
452 jgs 82
453 jfenwick 1796
454    
455    
456 jfenwick 2271
457 jgs 82 /**
458     \brief
459     getLength
460    
461     Description:
462 jgs 509 Return the total number of doubles stored for this DataTagged object.
463 jgs 507 T
464 jgs 82 */
465 woo409 757 ESCRIPT_DLL_API
466 jgs 82 virtual
467     ValueType::size_type
468     getLength() const;
469    
470     /**
471     \brief
472     getSlice
473    
474     Description:
475 jgs 513 Factory method that returns a newly created DataTagged object generated
476     by taking the specified slice from this DataTagged object.
477     The caller is reponsible for managing the returned object.
478 jgs 535 T
479 jgs 82 */
480 woo409 757 ESCRIPT_DLL_API
481 jgs 82 virtual
482     DataAbstract*
483 jfenwick 1796 getSlice(const DataTypes::RegionType& region) const;
484 jgs 82
485     /**
486     \brief
487 jgs 513 Slice Constructor for DataTagged.
488 jgs 82
489     Description:
490 jgs 513 Creates a DataTagged object which is the specified slice
491 jgs 519 from the given DataTagged object.
492     \param other - Input - DataTagged object to slice from.
493     \param region - Input - Region to slice.
494 jgs 535 T
495 jgs 82 */
496 woo409 757 ESCRIPT_DLL_API
497 jgs 513 DataTagged(const DataTagged& other,
498 jfenwick 1796 const DataTypes::RegionType& region);
499 jgs 513
500     /**
501     \brief
502     setSlice
503    
504     Description:
505 jgs 519 Copy the given Data object into the specified region in this object.
506 jgs 513 \param other - Input - Data object to copy from.
507 jgs 519 \param region - Input - Region to copy into (NB: must have same shape as other!).
508 jgs 535 T
509 jgs 513 */
510 woo409 757 ESCRIPT_DLL_API
511 jgs 82 virtual
512     void
513 jgs 513 setSlice(const DataAbstract* other,
514 jfenwick 1796 const DataTypes::RegionType& region);
515 jgs 82
516 jgs 123
517     /**
518 jgs 509 \brief
519 ksteube 775 Computes a symmetric matrix (A + AT) / 2
520    
521     \param ev - Output - symmetric matrix
522    
523     */
524 gross 800 ESCRIPT_DLL_API
525 ksteube 775 virtual void
526     symmetric(DataAbstract* ev);
527    
528     /**
529     \brief
530     Computes a nonsymmetric matrix (A - AT) / 2
531    
532     \param ev - Output - nonsymmetric matrix
533    
534     */
535 gross 800 ESCRIPT_DLL_API
536 ksteube 775 virtual void
537     nonsymmetric(DataAbstract* ev);
538    
539     /**
540     \brief
541     Computes the trace of a matrix
542    
543     \param ev - Output - the trace of a matrix
544 jfenwick 2519 \param axis_offset
545 ksteube 775 */
546 gross 800 ESCRIPT_DLL_API
547 ksteube 775 virtual void
548 gross 800 trace(DataAbstract* ev, int axis_offset);
549 ksteube 775
550     /**
551     \brief
552 gross 804 swaps components axis0 and axis1
553 gross 800
554     \param ev - Output - swapped components
555 jfenwick 2519 \param axis0
556     \param axis1
557 gross 800 */
558     ESCRIPT_DLL_API
559     virtual void
560 gross 804 swapaxes(DataAbstract* ev, int axis0, int axis1);
561 gross 800
562     /**
563     \brief
564 ksteube 775 Transpose each data point of this Data object around the given axis.
565    
566     \param ev - Output - the transpose of a matrix
567 jfenwick 2519 \param axis_offset
568 ksteube 775 */
569 gross 800 ESCRIPT_DLL_API
570 ksteube 775 virtual void
571     transpose(DataAbstract* ev, int axis_offset);
572    
573     /**
574     \brief
575 gross 580 solves the eigenvalue problem this*V=ev*V for the eigenvalues ev
576    
577     \param ev - Output - eigenvalues in increasing order at each data point
578    
579     */
580 woo409 757 ESCRIPT_DLL_API
581 gross 580 virtual void
582     eigenvalues(DataAbstract* ev);
583    
584     /**
585     \brief
586     solves the eigenvalue problem this*V=ev*V for the eigenvalues ev and eigenvectors V
587    
588     \param ev - Output - eigenvalues in increasing order at each data point
589     \param V - Output - corresponding eigenvectors. They are normalized such that their length is one
590     and the first nonzero component is positive.
591     \param tol - Input - eigenvalue with relative distance tol are treated as equal.
592    
593     */
594    
595 woo409 757 ESCRIPT_DLL_API
596 gross 580 virtual void
597     eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol=1.e-13);
598    
599    
600 jfenwick 1796 /**
601     \brief Returns the offset in the structure which stores the default value
602     */
603     ESCRIPT_DLL_API
604     DataTypes::ValueType::size_type
605     getDefaultOffset() const;
606    
607 jgs 82 protected:
608    
609     private:
610    
611     //
612     // The offset lookup table
613     DataMapType m_offsetLookup;
614    
615     //
616 jgs 496 // the offset to the default value
617 jgs 119 static const int m_defaultValueOffset = 0;
618    
619     //
620 jgs 82 // The actual data
621 jgs 121 ValueType m_data;
622 jgs 82
623     };
624    
625     inline
626     bool
627     DataTagged::isCurrentTag(int tag) const
628     {
629     DataMapType::const_iterator pos(m_offsetLookup.find(tag));
630     return (pos!=m_offsetLookup.end());
631     }
632    
633 jfenwick 1796 inline
634     DataTypes::ValueType::size_type
635     DataTagged::getDefaultOffset() const
636 jgs 82 {
637 jfenwick 1796 return m_defaultValueOffset;
638 jgs 82 }
639    
640     inline
641 jfenwick 1796 DataTypes::ValueType::reference
642 jfenwick 2271 DataTagged::getDefaultValueRW(DataTypes::ValueType::size_type i)
643     {
644     return getVectorRW()[i]; // getVectorRW has exclusive write checks
645 jgs 82 }
646    
647 jgs 496 inline
648 jfenwick 1796 DataTypes::ValueType::const_reference
649 jfenwick 2271 DataTagged::getDefaultValueRO(DataTypes::ValueType::size_type i) const
650 gross 983 {
651 jfenwick 2271 return getVectorRO()[i];
652 gross 983 }
653    
654     inline
655 jgs 496 const DataTagged::DataMapType&
656     DataTagged::getTagLookup() const
657     {
658     return m_offsetLookup;
659     }
660    
661     inline
662 jfenwick 1796 DataTypes::ValueType::size_type
663 jgs 496 DataTagged::getLength() const
664     {
665     return m_data.size();
666     }
667    
668 jgs 82 } // end of namespace
669    
670     #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26