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

Diff of /trunk/escript/src/DataTagged.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 541 by jgs, Mon Feb 20 06:50:39 2006 UTC revision 1020 by phornby, Mon Mar 12 10:12:36 2007 UTC
# Line 1  Line 1 
1  // $Id$  // $Id$
2    
3  /*  /*
4   ******************************************************************************   ************************************************************
5   *                                                                            *   *          Copyright 2006 by ACcESS MNRF                   *
6   *       COPYRIGHT  ACcESS 2004 -  All Rights Reserved                        *   *                                                          *
7   *                                                                            *   *              http://www.access.edu.au                    *
8   * This software is the property of ACcESS. No part of this code              *   *       Primary Business: Queensland, Australia            *
9   * may be copied in any form or by any means without the expressed written    *   *  Licensed under the Open Software License version 3.0    *
10   * consent of ACcESS.  Copying, use or modification of this software          *   *     http://www.opensource.org/licenses/osl-3.0.php       *
11   * by any unauthorised person is illegal unless that person has a software    *   *                                                          *
12   * license agreement with ACcESS.                                             *   ************************************************************
  *                                                                            *  
  ******************************************************************************  
13  */  */
14    
15  #include "DataTagged.h"  #include "DataTagged.h"
16    
17  #include "DataConstant.h"  #include "DataConstant.h"
18  #include "DataException.h"  #include "DataException.h"
19    #include <netcdfcpp.h>
20    
21  using namespace std;  using namespace std;
22    
# Line 80  DataTagged::DataTagged(const FunctionSpa Line 79  DataTagged::DataTagged(const FunctionSpa
79    }    }
80  }  }
81    
82    DataTagged::DataTagged(const FunctionSpace& what,
83                           const DataArrayView::ShapeType &shape,
84                           const TagListType& tags,
85                           const ValueType& data)
86      : DataAbstract(what)
87    {
88      // alternative constructor
89      // not unit_tested tested yet
90    
91      // copy the data
92      m_data=data;
93    
94      // create the view of the data
95      DataArrayView tempView(m_data,shape);
96      setPointDataView(tempView);
97    
98      // create the tag lookup map
99      for (int sampleNo=0; sampleNo<getNumSamples(); sampleNo++) {
100        m_offsetLookup.insert(DataMapType::value_type(sampleNo,tags[sampleNo]));
101      }
102    }
103    
104    
105  DataTagged::DataTagged(const DataTagged& other)  DataTagged::DataTagged(const DataTagged& other)
106    : DataAbstract(other.getFunctionSpace()),    : DataAbstract(other.getFunctionSpace()),
107    m_data(other.m_data),    m_data(other.m_data),
# Line 110  DataTagged::DataTagged(const DataConstan Line 132  DataTagged::DataTagged(const DataConstan
132    setPointDataView(temp);    setPointDataView(temp);
133  }  }
134    
 void  
 DataTagged::reshapeDataPoint(const DataArrayView::ShapeType& shape)  
 {  
   // can only reshape a rank zero data point  
   if (getPointDataView().getRank()!=0) {  
     stringstream temp;  
     temp << "Error - Can only reshape Data with data points of rank 0. "  
      << "This Data has data points with rank: "  
      << getPointDataView().getRank();  
     throw DataException(temp.str());  
   }  
   
   // allocate enough space for all values  
   DataArrayView::ValueType newData(DataArrayView::noValues(shape)*(m_offsetLookup.size()+1));  
   DataArrayView newView(newData,shape);  
   newView.copy(0,getDefaultValue()());  
   
   // loop through the tag values  
   DataMapType::iterator pos;  
   DataArrayView::ValueType::size_type tagOffset=DataArrayView::noValues(shape);  
   for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++){  
     newView.copy(tagOffset,m_data[pos->second]);  
     pos->second=tagOffset;  
     tagOffset+=DataArrayView::noValues(shape);  
   }  
   m_data=newData;  
   DataArrayView temp(m_data,shape);  
   setPointDataView(temp);  
 }  
   
135  DataAbstract*  DataAbstract*
136  DataTagged::getSlice(const DataArrayView::RegionType& region) const  DataTagged::getSlice(const DataArrayView::RegionType& region) const
137  {  {
# Line 200  DataTagged::setSlice(const DataAbstract* Line 192  DataTagged::setSlice(const DataAbstract*
192    if (getPointDataView().getRank()!=region.size()) {    if (getPointDataView().getRank()!=region.size()) {
193      throw DataException("Error - Invalid slice region.");      throw DataException("Error - Invalid slice region.");
194    }    }
195    if (otherTemp->getPointDataView().getRank()>0 and !other->getPointDataView().checkShape(regionShape)) {    if (otherTemp->getPointDataView().getRank()>0 && !other->getPointDataView().checkShape(regionShape)) {
196      throw DataException (other->getPointDataView().createShapeErrorMessage(      throw DataException (other->getPointDataView().createShapeErrorMessage(
197                           "Error - Couldn't copy slice due to shape mismatch.",regionShape));                           "Error - Couldn't copy slice due to shape mismatch.",regionShape));
198    }    }
# Line 421  DataTagged::extractData(ifstream& archiv Line 413  DataTagged::extractData(ifstream& archiv
413  {  {
414    return(m_data.extractData(archiveFile, noValues));    return(m_data.extractData(archiveFile, noValues));
415  }  }
416    void
417    DataTagged::symmetric(DataAbstract* ev)
418    {
419      DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
420      if (temp_ev==0) {
421        throw DataException("Error - DataTagged::symmetric casting to DataTagged failed (probably a programming error).");
422      }
423      const DataTagged::DataMapType& thisLookup=getTagLookup();
424      DataTagged::DataMapType::const_iterator i;
425      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
426      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
427          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
428          DataArrayView thisView=getDataPointByTag(i->first);
429          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
430          DataArrayView::symmetric(thisView,0,evView,0);
431      }
432      DataArrayView::symmetric(getDefaultValue(),0,temp_ev->getDefaultValue(),0);
433    }
434    void
435    DataTagged::nonsymmetric(DataAbstract* ev)
436    {
437      DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
438      if (temp_ev==0) {
439        throw DataException("Error - DataTagged::nonsymmetric casting to DataTagged failed (probably a programming error).");
440      }
441      const DataTagged::DataMapType& thisLookup=getTagLookup();
442      DataTagged::DataMapType::const_iterator i;
443      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
444      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
445          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
446          DataArrayView thisView=getDataPointByTag(i->first);
447          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
448          DataArrayView::nonsymmetric(thisView,0,evView,0);
449      }
450      DataArrayView::nonsymmetric(getDefaultValue(),0,temp_ev->getDefaultValue(),0);
451    }
452    void
453    DataTagged::trace(DataAbstract* ev, int axis_offset)
454    {
455      DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
456      if (temp_ev==0) {
457        throw DataException("Error - DataTagged::trace casting to DataTagged failed (probably a programming error).");
458      }
459      const DataTagged::DataMapType& thisLookup=getTagLookup();
460      DataTagged::DataMapType::const_iterator i;
461      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
462      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
463          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
464          DataArrayView thisView=getDataPointByTag(i->first);
465          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
466          DataArrayView::trace(thisView,0,evView,0, axis_offset);
467      }
468      DataArrayView::trace(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis_offset);
469    }
470    
471    void
472    DataTagged::transpose(DataAbstract* ev, int axis_offset)
473    {
474      DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
475      if (temp_ev==0) {
476        throw DataException("Error - DataTagged::transpose casting to DataTagged failed (probably a programming error).");
477      }
478      const DataTagged::DataMapType& thisLookup=getTagLookup();
479      DataTagged::DataMapType::const_iterator i;
480      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
481      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
482          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
483          DataArrayView thisView=getDataPointByTag(i->first);
484          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
485          DataArrayView::transpose(thisView,0,evView,0, axis_offset);
486      }
487      DataArrayView::transpose(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis_offset);
488    }
489    
490    void
491    DataTagged::swapaxes(DataAbstract* ev, int axis0, int axis1)
492    {
493      DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
494      if (temp_ev==0) {
495        throw DataException("Error - DataTagged::swapaxes casting to DataTagged failed (probably a programming error).");
496      }
497      const DataTagged::DataMapType& thisLookup=getTagLookup();
498      DataTagged::DataMapType::const_iterator i;
499      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
500      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
501          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
502          DataArrayView thisView=getDataPointByTag(i->first);
503          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
504          DataArrayView::swapaxes(thisView,0,evView,0,axis0,axis1);
505      }
506      DataArrayView::swapaxes(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis0,axis1);
507    }
508    
509    void
510    DataTagged::eigenvalues(DataAbstract* ev)
511    {
512      DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
513      if (temp_ev==0) {
514        throw DataException("Error - DataTagged::eigenvalues casting to DataTagged failed (propably a programming error).");
515      }
516      const DataTagged::DataMapType& thisLookup=getTagLookup();
517      DataTagged::DataMapType::const_iterator i;
518      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
519      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
520          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
521          DataArrayView thisView=getDataPointByTag(i->first);
522          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
523          DataArrayView::eigenvalues(thisView,0,evView,0);
524      }
525      DataArrayView::eigenvalues(getDefaultValue(),0,temp_ev->getDefaultValue(),0);
526    }
527    void
528    DataTagged::eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol)
529    {
530      DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
531      if (temp_ev==0) {
532        throw DataException("Error - DataTagged::eigenvalues_and_eigenvectors casting to DataTagged failed (propably a programming error).");
533      }
534      DataTagged* temp_V=dynamic_cast<DataTagged*>(V);
535      if (temp_V==0) {
536        throw DataException("Error - DataTagged::eigenvalues_and_eigenvectors casting to DataTagged failed (propably a programming error).");
537      }
538      const DataTagged::DataMapType& thisLookup=getTagLookup();
539      DataTagged::DataMapType::const_iterator i;
540      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
541      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
542          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
543          temp_V->addTaggedValue(i->first,temp_V->getDefaultValue());
544          DataArrayView thisView=getDataPointByTag(i->first);
545          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
546          DataArrayView VView=temp_V->getDataPointByTag(i->first);
547          DataArrayView::eigenvalues_and_eigenvectors(thisView,0,evView,0,VView,0,tol);
548      }
549      DataArrayView::eigenvalues_and_eigenvectors(getDefaultValue(),0,
550                                                  temp_ev->getDefaultValue(),0,
551                                                  temp_V->getDefaultValue(),0,
552                                                  tol);
553    
554    
555    }
556    
557    void
558    DataTagged::dump(const std::string fileName) const
559    {
560       const int ldims=DataArrayView::maxRank+1;
561       const NcDim* ncdims[ldims];
562       NcVar *var, *tags_var;
563       int rank = getPointDataView().getRank();
564       int type=  getFunctionSpace().getTypeCode();
565       int ndims =0;
566       long dims[ldims];
567       DataArrayView::ShapeType shape = getPointDataView().getShape();
568    
569       // netCDF error handler
570       NcError err(NcError::verbose_nonfatal);
571       // Create the file.
572       NcFile dataFile(fileName.c_str(), NcFile::Replace);
573       // check if writing was successful
574       if (!dataFile.is_valid())
575            throw DataException("Error - DataTagged:: opening of netCDF file for output failed.");
576       if (!dataFile.add_att("type","tagged") )
577            throw DataException("Error - DataTagged:: appending data type to netCDF file failed.");
578       if (!dataFile.add_att("rank",rank) )
579            throw DataException("Error - DataTagged:: appending rank attribute to netCDF file failed.");
580       if (!dataFile.add_att("function_space_type",type))
581            throw DataException("Error - DataTagged:: appending function space attribute to netCDF file failed.");
582       ndims=rank+1;
583       if ( rank >0 ) {
584           dims[0]=shape[0];
585           if (! (ncdims[0] = dataFile.add_dim("d0",shape[0])) )
586                throw DataException("Error - DataTagged:: appending ncdimsion 0 to netCDF file failed.");
587       }
588       if ( rank >1 ) {
589           dims[1]=shape[1];
590           if (! (ncdims[1] = dataFile.add_dim("d1",shape[1])) )
591                throw DataException("Error - DataTagged:: appending ncdimsion 1 to netCDF file failed.");
592       }
593       if ( rank >2 ) {
594           dims[2]=shape[2];
595           if (! (ncdims[2] = dataFile.add_dim("d2", shape[2])) )
596                throw DataException("Error - DataTagged:: appending ncdimsion 2 to netCDF file failed.");
597       }
598       if ( rank >3 ) {
599           dims[3]=shape[3];
600           if (! (ncdims[3] = dataFile.add_dim("d3", shape[3])) )
601                throw DataException("Error - DataTagged:: appending ncdimsion 3 to netCDF file failed.");
602       }
603       const DataTagged::DataMapType& thisLookup=getTagLookup();
604       DataTagged::DataMapType::const_iterator i;
605       DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
606       int ntags=1;
607       for (i=thisLookup.begin();i!=thisLookupEnd;i++) ntags++;
608       int* tags =(int*) malloc(ntags*sizeof(int));
609       int c=1;
610       tags[0]=-1;
611       for (i=thisLookup.begin();i!=thisLookupEnd;i++) tags[c++]=i->first;
612       dims[rank]=ntags;
613       if (! (ncdims[rank] = dataFile.add_dim("num_tags", dims[rank])) )
614       {
615           free(tags);
616               throw DataException("Error - DataTagged:: appending num_tags to netCDF file failed.");
617       }
618       if (! ( tags_var = dataFile.add_var("tags", ncInt, ncdims[rank])) )
619       {
620        free(tags);
621            throw DataException("Error - DataTagged:: appending tags to netCDF file failed.");
622       }
623       if (! (tags_var->put(tags,dims[rank])) )
624       {
625        free(tags);
626            throw DataException("Error - DataTagged:: copy tags to netCDF buffer failed.");
627       }
628       if (! ( var = dataFile.add_var("data", ncDouble, ndims, ncdims)) )
629       {
630        free(tags);
631            throw DataException("Error - DataTagged:: appending variable to netCDF file failed.");
632       }
633       if (! (var->put(&m_data[0],dims)) )
634       {
635        free(tags);
636            throw DataException("Error - DataTagged:: copy data to netCDF buffer failed.");
637       }
638    }
639    
640    
641  }  // end of namespace  }  // end of namespace

Legend:
Removed from v.541  
changed lines
  Added in v.1020

  ViewVC Help
Powered by ViewVC 1.1.26