/[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 580 by gross, Wed Mar 8 05:45:51 2006 UTC revision 1455 by phornby, Thu Feb 28 17:19:44 2008 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 305  DataTagged::addTaggedValues(const TagLis Line 300  DataTagged::addTaggedValues(const TagLis
300         << " doesn't match number of values: " << values.size();         << " doesn't match number of values: " << values.size();
301        throw DataException(temp.str());        throw DataException(temp.str());
302      } else {      } else {
303        for (int i=0;i<tagKeys.size();i++) {        unsigned int i;
304          for (i=0;i<tagKeys.size();i++) {
305          addTaggedValue(tagKeys[i],values[i]);          addTaggedValue(tagKeys[i],values[i]);
306        }        }
307      }      }
# Line 422  DataTagged::extractData(ifstream& archiv Line 418  DataTagged::extractData(ifstream& archiv
418    return(m_data.extractData(archiveFile, noValues));    return(m_data.extractData(archiveFile, noValues));
419  }  }
420  void  void
421    DataTagged::symmetric(DataAbstract* ev)
422    {
423      DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
424      if (temp_ev==0) {
425        throw DataException("Error - DataTagged::symmetric casting to DataTagged failed (probably a programming error).");
426      }
427      const DataTagged::DataMapType& thisLookup=getTagLookup();
428      DataTagged::DataMapType::const_iterator i;
429      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
430      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
431          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
432          DataArrayView thisView=getDataPointByTag(i->first);
433          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
434          DataArrayView::symmetric(thisView,0,evView,0);
435      }
436      DataArrayView::symmetric(getDefaultValue(),0,temp_ev->getDefaultValue(),0);
437    }
438    void
439    DataTagged::nonsymmetric(DataAbstract* ev)
440    {
441      DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
442      if (temp_ev==0) {
443        throw DataException("Error - DataTagged::nonsymmetric casting to DataTagged failed (probably a programming error).");
444      }
445      const DataTagged::DataMapType& thisLookup=getTagLookup();
446      DataTagged::DataMapType::const_iterator i;
447      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
448      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
449          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
450          DataArrayView thisView=getDataPointByTag(i->first);
451          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
452          DataArrayView::nonsymmetric(thisView,0,evView,0);
453      }
454      DataArrayView::nonsymmetric(getDefaultValue(),0,temp_ev->getDefaultValue(),0);
455    }
456    void
457    DataTagged::trace(DataAbstract* ev, int axis_offset)
458    {
459      DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
460      if (temp_ev==0) {
461        throw DataException("Error - DataTagged::trace casting to DataTagged failed (probably a programming error).");
462      }
463      const DataTagged::DataMapType& thisLookup=getTagLookup();
464      DataTagged::DataMapType::const_iterator i;
465      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
466      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
467          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
468          DataArrayView thisView=getDataPointByTag(i->first);
469          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
470          DataArrayView::trace(thisView,0,evView,0, axis_offset);
471      }
472      DataArrayView::trace(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis_offset);
473    }
474    
475    void
476    DataTagged::transpose(DataAbstract* ev, int axis_offset)
477    {
478      DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
479      if (temp_ev==0) {
480        throw DataException("Error - DataTagged::transpose casting to DataTagged failed (probably a programming error).");
481      }
482      const DataTagged::DataMapType& thisLookup=getTagLookup();
483      DataTagged::DataMapType::const_iterator i;
484      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
485      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
486          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
487          DataArrayView thisView=getDataPointByTag(i->first);
488          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
489          DataArrayView::transpose(thisView,0,evView,0, axis_offset);
490      }
491      DataArrayView::transpose(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis_offset);
492    }
493    
494    void
495    DataTagged::swapaxes(DataAbstract* ev, int axis0, int axis1)
496    {
497      DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
498      if (temp_ev==0) {
499        throw DataException("Error - DataTagged::swapaxes casting to DataTagged failed (probably a programming error).");
500      }
501      const DataTagged::DataMapType& thisLookup=getTagLookup();
502      DataTagged::DataMapType::const_iterator i;
503      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
504      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
505          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
506          DataArrayView thisView=getDataPointByTag(i->first);
507          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
508          DataArrayView::swapaxes(thisView,0,evView,0,axis0,axis1);
509      }
510      DataArrayView::swapaxes(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis0,axis1);
511    }
512    
513    void
514  DataTagged::eigenvalues(DataAbstract* ev)  DataTagged::eigenvalues(DataAbstract* ev)
515  {  {
516    DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);    DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
# Line 435  DataTagged::eigenvalues(DataAbstract* ev Line 524  DataTagged::eigenvalues(DataAbstract* ev
524        temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());        temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
525        DataArrayView thisView=getDataPointByTag(i->first);        DataArrayView thisView=getDataPointByTag(i->first);
526        DataArrayView evView=temp_ev->getDataPointByTag(i->first);        DataArrayView evView=temp_ev->getDataPointByTag(i->first);
 cout << i->first << thisView(0,0) << "\n";  
527        DataArrayView::eigenvalues(thisView,0,evView,0);        DataArrayView::eigenvalues(thisView,0,evView,0);
528    }    }
529    DataArrayView::eigenvalues(getDefaultValue(),0,temp_ev->getDefaultValue(),0);    DataArrayView::eigenvalues(getDefaultValue(),0,temp_ev->getDefaultValue(),0);
# Line 470  DataTagged::eigenvalues_and_eigenvectors Line 558  DataTagged::eigenvalues_and_eigenvectors
558    
559  }  }
560    
561    void
562    DataTagged::setToZero(){
563        DataArrayView::ValueType::size_type n=m_data.size();
564        for (int i=0; i<n ;++i) m_data[i]=0.;
565    }
566    
567    void
568    DataTagged::dump(const std::string fileName) const
569    {
570       #ifdef PASO_MPI
571       throw DataException("Error - DataTagged:: dump is not implemented for MPI yet.");
572       #endif
573       #ifdef USE_NETCDF
574       const int ldims=DataArrayView::maxRank+1;
575       const NcDim* ncdims[ldims];
576       NcVar *var, *tags_var;
577       int rank = getPointDataView().getRank();
578       int type=  getFunctionSpace().getTypeCode();
579       int ndims =0;
580       long dims[ldims];
581       const double* d_ptr=&(m_data[0]);
582       DataArrayView::ShapeType shape = getPointDataView().getShape();
583    
584       // netCDF error handler
585       NcError err(NcError::verbose_nonfatal);
586       // Create the file.
587       NcFile dataFile(fileName.c_str(), NcFile::Replace);
588       // check if writing was successful
589       if (!dataFile.is_valid())
590            throw DataException("Error - DataTagged:: opening of netCDF file for output failed.");
591       if (!dataFile.add_att("type_id",1) )
592            throw DataException("Error - DataTagged:: appending data type to netCDF file failed.");
593       if (!dataFile.add_att("rank",rank) )
594            throw DataException("Error - DataTagged:: appending rank attribute to netCDF file failed.");
595       if (!dataFile.add_att("function_space_type",type))
596            throw DataException("Error - DataTagged:: appending function space attribute to netCDF file failed.");
597       ndims=rank+1;
598       if ( rank >0 ) {
599           dims[0]=shape[0];
600           if (! (ncdims[0] = dataFile.add_dim("d0",shape[0])) )
601                throw DataException("Error - DataTagged:: appending ncdimsion 0 to netCDF file failed.");
602       }
603       if ( rank >1 ) {
604           dims[1]=shape[1];
605           if (! (ncdims[1] = dataFile.add_dim("d1",shape[1])) )
606                throw DataException("Error - DataTagged:: appending ncdimsion 1 to netCDF file failed.");
607       }
608       if ( rank >2 ) {
609           dims[2]=shape[2];
610           if (! (ncdims[2] = dataFile.add_dim("d2", shape[2])) )
611                throw DataException("Error - DataTagged:: appending ncdimsion 2 to netCDF file failed.");
612       }
613       if ( rank >3 ) {
614           dims[3]=shape[3];
615           if (! (ncdims[3] = dataFile.add_dim("d3", shape[3])) )
616                throw DataException("Error - DataTagged:: appending ncdimsion 3 to netCDF file failed.");
617       }
618       const DataTagged::DataMapType& thisLookup=getTagLookup();
619       DataTagged::DataMapType::const_iterator i;
620       DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
621       int ntags=1;
622       for (i=thisLookup.begin();i!=thisLookupEnd;i++) ntags++;
623       int* tags =(int*) malloc(ntags*sizeof(int));
624       int c=1;
625       tags[0]=-1;
626       for (i=thisLookup.begin();i!=thisLookupEnd;i++) tags[c++]=i->first;
627       dims[rank]=ntags;
628       if (! (ncdims[rank] = dataFile.add_dim("num_tags", dims[rank])) )
629       {
630           free(tags);
631               throw DataException("Error - DataTagged:: appending num_tags to netCDF file failed.");
632       }
633       if (! ( tags_var = dataFile.add_var("tags", ncInt, ncdims[rank])) )
634       {
635        free(tags);
636            throw DataException("Error - DataTagged:: appending tags to netCDF file failed.");
637       }
638       if (! (tags_var->put(tags,dims[rank])) )
639       {
640        free(tags);
641            throw DataException("Error - DataTagged:: copy tags to netCDF buffer failed.");
642       }
643       if (! ( var = dataFile.add_var("data", ncDouble, ndims, ncdims)) )
644       {
645        free(tags);
646            throw DataException("Error - DataTagged:: appending variable to netCDF file failed.");
647       }
648       if (! (var->put(d_ptr,dims)) )
649       {
650        free(tags);
651            throw DataException("Error - DataTagged:: copy data to netCDF buffer failed.");
652       }
653       #else
654       throw DataException("Error - DataTagged:: dump is not configured with netCDF. Please contact your installation manager.");
655       #endif
656    }
657  }  // end of namespace  }  // end of namespace

Legend:
Removed from v.580  
changed lines
  Added in v.1455

  ViewVC Help
Powered by ViewVC 1.1.26