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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 775 - (show annotations)
Mon Jul 10 04:00:08 2006 UTC (12 years, 11 months ago) by ksteube
File MIME type: text/plain
File size: 14524 byte(s)
Modified the following python methods in escript/py_src/util.py to
call faster C++ methods:
	escript_trace
	escript_transpose
	escript_symmetric
	escript_nonsymmetric

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 /**
176 \brief
177 Return the tag number associated with the given data-point number
178 according to the associated function space.
179 T
180 */
181 ESCRIPT_DLL_API
182 virtual
183 int
184 getTagNumber(int dpno);
185
186 /**
187 \brief
188 getPointOffset
189
190 Description:
191 Return the offset to the given data-point value in the underlying
192 data vector.
193
194 \param sampleNo - Input - sample number.
195 \param dataPointNo - Input - data-point number.
196 T
197 */
198 ESCRIPT_DLL_API
199 virtual
200 ValueType::size_type
201 getPointOffset(int sampleNo,
202 int dataPointNo) const;
203
204 /**
205 \brief
206 addTaggedValues
207
208 Description:
209 Add the given tags and values to this DataTagged object.
210 \param tagKeys - Input - A vector of integer tags.
211 \param values - Input - A vector of DataArrayViews. If this is empty
212 all tag values will be assigned a scalar data-point of value
213 0. If it contains one value all tag values will be assigned
214 this value. Otherwise consecutive tags will be assigned
215 consecutive values. If there is a mismatch between the
216 number of keys and the number of values an exception
217 will be generated.
218 T
219 */
220 ESCRIPT_DLL_API
221 void
222 addTaggedValues(const TagListType& tagKeys,
223 const ValueListType& values);
224
225 /**
226 \brief
227 addTaggedValue
228
229 Description:
230 Add a single tag and value to this DataTagged object. If this tag already has
231 a value associated with it, setTaggedValue will be used to update this value.
232 \param tagKey - Input - Integer tag.
233 \param value - Input - Single DataArrayView value to be assigned to the tag.
234 T
235 */
236 ESCRIPT_DLL_API
237 void
238 addTaggedValue(int tagKey,
239 const DataArrayView& value);
240
241 /**
242 \brief
243 setTaggedValues
244
245 Description:
246 Set the given tags to the given values in this DataTagged object.
247 \param tagKeys - Input - A vector of integer tag.
248 \param values - Input - A vector of DataArrayViews. If this is empty
249 all tag values will be assigned a scalar data-point of value
250 0. If it contains one value all tag values will be assigned
251 this value. Otherwise consecutive tags will be assigned
252 consecutive values. If there is a mismatch between the
253 number of keys and the number of values an exception
254 will be generated.
255 T
256 */
257 ESCRIPT_DLL_API
258 void
259 setTaggedValues(const TagListType& tagKeys,
260 const ValueListType& values);
261
262 /**
263 \brief
264 setTaggedValue
265
266 Description:
267 Assign the given value to the given tag.
268 \param tagKey - Input - Integer tag.
269 \param value - Input - Single DataArrayView value to be assigned to the tag.
270 T
271 */
272 ESCRIPT_DLL_API
273 virtual
274 void
275 setTaggedValue(int tagKey,
276 const DataArrayView& value);
277
278 /**
279 \brief
280 getDataPointByTag
281
282 Description:
283 Return data-point associated with the given tag as a DataArrayView.
284 \param tag - Input - Integer key.
285 T
286 */
287 ESCRIPT_DLL_API
288 DataArrayView
289 getDataPointByTag(int tag) const;
290
291 /**
292 \brief
293 getDataPoint
294
295 Description:
296 Return the data-point specified by the given sample and data-point
297 numbers as a DataArrayView.
298 \param sampleNo - Input.
299 \param dataPointNo - Input.
300 T
301 */
302 ESCRIPT_DLL_API
303 virtual
304 DataArrayView
305 getDataPoint(int sampleNo,
306 int dataPointNo);
307
308 /**
309 \brief
310 getTagLookup
311
312 Description:
313 Return a reference to the tag offset lookup table.
314 T
315 */
316 ESCRIPT_DLL_API
317 const DataMapType&
318 getTagLookup() const;
319
320 /**
321 \brief
322 isCurrentTag
323
324 Description:
325 Return true if the given tag exists within the DataTagged tag map.
326
327 *** NB: The DataTagged tag map does not necessarily coincide with the tag
328 keys in the associated function space.
329 T
330 */
331 ESCRIPT_DLL_API
332 bool
333 isCurrentTag(int tag) const;
334
335 /**
336 \brief
337 getDefaultValue
338
339 Description:
340 Return the default value. This value is associated with any tag which
341 is not explicitly recorded in this DataTagged object's tag map.
342 T
343 */
344 ESCRIPT_DLL_API
345 DataArrayView&
346 getDefaultValue();
347
348 ESCRIPT_DLL_API
349 const DataArrayView&
350 getDefaultValue() const;
351
352 /**
353 \brief
354 getLength
355
356 Description:
357 Return the total number of doubles stored for this DataTagged object.
358 T
359 */
360 ESCRIPT_DLL_API
361 virtual
362 ValueType::size_type
363 getLength() const;
364
365 /**
366 \brief
367 getSlice
368
369 Description:
370 Factory method that returns a newly created DataTagged object generated
371 by taking the specified slice from this DataTagged object.
372 The caller is reponsible for managing the returned object.
373 T
374 */
375 ESCRIPT_DLL_API
376 virtual
377 DataAbstract*
378 getSlice(const DataArrayView::RegionType& region) const;
379
380 /**
381 \brief
382 Slice Constructor for DataTagged.
383
384 Description:
385 Creates a DataTagged object which is the specified slice
386 from the given DataTagged object.
387 \param other - Input - DataTagged object to slice from.
388 \param region - Input - Region to slice.
389 T
390 */
391 ESCRIPT_DLL_API
392 DataTagged(const DataTagged& other,
393 const DataArrayView::RegionType& region);
394
395 /**
396 \brief
397 setSlice
398
399 Description:
400 Copy the given Data object into the specified region in this object.
401 \param other - Input - Data object to copy from.
402 \param region - Input - Region to copy into (NB: must have same shape as other!).
403 T
404 */
405 ESCRIPT_DLL_API
406 virtual
407 void
408 setSlice(const DataAbstract* other,
409 const DataArrayView::RegionType& region);
410
411 /**
412 \brief
413 reshapeDataPoint
414
415 Description:
416 Reshape each data-point in this object to the given shape, only
417 if current data-points are scalars. An exception is thrown if
418 the current data-points have rank other than 0.
419 The original values of the data-points are used for all values
420 of the new data-points.
421 T
422 */
423 ESCRIPT_DLL_API
424 void
425 reshapeDataPoint(const DataArrayView::ShapeType& shape);
426
427 /**
428 \brief
429 Archive the underlying data values to the file referenced
430 by ofstream. A count of the number of values expected to be written
431 is provided as a cross-check.
432
433 The return value indicates success (0) or otherwise (1).
434 */
435 ESCRIPT_DLL_API
436 int
437 archiveData(std::ofstream& archiveFile,
438 const DataArrayView::ValueType::size_type noValues) const;
439
440 /**
441 \brief
442 Extract the number of values specified by noValues from the file
443 referenced by ifstream to the underlying data structure.
444
445 The return value indicates success (0) or otherwise (1).
446 */
447 ESCRIPT_DLL_API
448 int
449 extractData(std::ifstream& archiveFile,
450 const DataArrayView::ValueType::size_type noValues);
451
452 /**
453 \brief
454 Computes a symmetric matrix (A + AT) / 2
455
456 \param ev - Output - symmetric matrix
457
458 */
459 virtual void
460 symmetric(DataAbstract* ev);
461
462 /**
463 \brief
464 Computes a nonsymmetric matrix (A - AT) / 2
465
466 \param ev - Output - nonsymmetric matrix
467
468 */
469 virtual void
470 nonsymmetric(DataAbstract* ev);
471
472 /**
473 \brief
474 Computes the trace of a matrix
475
476 \param ev - Output - the trace of a matrix
477
478 */
479 virtual void
480 matrixtrace(DataAbstract* ev, int axis_offset);
481
482 /**
483 \brief
484 Transpose each data point of this Data object around the given axis.
485
486 \param ev - Output - the transpose of a matrix
487
488 */
489 virtual void
490 transpose(DataAbstract* ev, int axis_offset);
491
492 /**
493 \brief
494 solves the eigenvalue problem this*V=ev*V for the eigenvalues ev
495
496 \param ev - Output - eigenvalues in increasing order at each data point
497
498 */
499 ESCRIPT_DLL_API
500 virtual void
501 eigenvalues(DataAbstract* ev);
502
503 /**
504 \brief
505 solves the eigenvalue problem this*V=ev*V for the eigenvalues ev and eigenvectors V
506
507 \param ev - Output - eigenvalues in increasing order at each data point
508 \param V - Output - corresponding eigenvectors. They are normalized such that their length is one
509 and the first nonzero component is positive.
510 \param tol - Input - eigenvalue with relative distance tol are treated as equal.
511
512 */
513
514 ESCRIPT_DLL_API
515 virtual void
516 eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol=1.e-13);
517
518
519 protected:
520
521 private:
522
523 //
524 // The offset lookup table
525 DataMapType m_offsetLookup;
526
527 //
528 // the offset to the default value
529 static const int m_defaultValueOffset = 0;
530
531 //
532 // The actual data
533 ValueType m_data;
534
535 };
536
537 inline
538 bool
539 DataTagged::isCurrentTag(int tag) const
540 {
541 DataMapType::const_iterator pos(m_offsetLookup.find(tag));
542 return (pos!=m_offsetLookup.end());
543 }
544
545 inline
546 DataArrayView&
547 DataTagged::getDefaultValue()
548 {
549 // The default value is always the first value.
550 return getPointDataView();
551 }
552
553 inline
554 const DataArrayView&
555 DataTagged::getDefaultValue() const
556 {
557 // The default value is always the first value.
558 return getPointDataView();
559 }
560
561 inline
562 const DataTagged::DataMapType&
563 DataTagged::getTagLookup() const
564 {
565 return m_offsetLookup;
566 }
567
568 inline
569 DataArrayView::ValueType::size_type
570 DataTagged::getLength() const
571 {
572 return m_data.size();
573 }
574
575 } // end of namespace
576
577 #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26