/[escript]/branches/arrexp_2137_win/escript/src/DataTagged.cpp
ViewVC logotype

Diff of /branches/arrexp_2137_win/escript/src/DataTagged.cpp

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

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

Legend:
Removed from v.519  
changed lines
  Added in v.1312

  ViewVC Help
Powered by ViewVC 1.1.26