/[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 800 by gross, Tue Aug 8 11:23:18 2006 UTC revision 1141 by gross, Tue May 15 04:24:51 2007 UTC
# Line 16  Line 16 
16    
17  #include "DataConstant.h"  #include "DataConstant.h"
18  #include "DataException.h"  #include "DataException.h"
19    #ifdef USE_NETCDF
20    #include "netcdfcpp.h"
21    #endif
22    
23  using namespace std;  using namespace std;
24    
# Line 131  DataTagged::DataTagged(const DataConstan Line 134  DataTagged::DataTagged(const DataConstan
134    setPointDataView(temp);    setPointDataView(temp);
135  }  }
136    
 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);  
 }  
   
137  DataAbstract*  DataAbstract*
138  DataTagged::getSlice(const DataArrayView::RegionType& region) const  DataTagged::getSlice(const DataArrayView::RegionType& region) const
139  {  {
# Line 391  DataTagged::toString() const Line 364  DataTagged::toString() const
364      tempView.setOffset(i->second);      tempView.setOffset(i->second);
365      temp << tempView.toString() << endl;      temp << tempView.toString() << endl;
366    }    }
367    return temp.str();    return string(temp.str());
368  }  }
369    
370  DataArrayView::ValueType::size_type  DataArrayView::ValueType::size_type
# Line 517  DataTagged::transpose(DataAbstract* ev, Line 490  DataTagged::transpose(DataAbstract* ev,
490  }  }
491    
492  void  void
493  DataTagged::swap(DataAbstract* ev, int axis_offset)  DataTagged::swapaxes(DataAbstract* ev, int axis0, int axis1)
494  {  {
495    DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);    DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
496    if (temp_ev==0) {    if (temp_ev==0) {
497      throw DataException("Error - DataTagged::swap casting to DataTagged failed (probably a programming error).");      throw DataException("Error - DataTagged::swapaxes casting to DataTagged failed (probably a programming error).");
498    }    }
499    const DataTagged::DataMapType& thisLookup=getTagLookup();    const DataTagged::DataMapType& thisLookup=getTagLookup();
500    DataTagged::DataMapType::const_iterator i;    DataTagged::DataMapType::const_iterator i;
# Line 530  DataTagged::swap(DataAbstract* ev, int a Line 503  DataTagged::swap(DataAbstract* ev, int a
503        temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());        temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
504        DataArrayView thisView=getDataPointByTag(i->first);        DataArrayView thisView=getDataPointByTag(i->first);
505        DataArrayView evView=temp_ev->getDataPointByTag(i->first);        DataArrayView evView=temp_ev->getDataPointByTag(i->first);
506        DataArrayView::swap(thisView,0,evView,0, axis_offset);        DataArrayView::swapaxes(thisView,0,evView,0,axis0,axis1);
507    }    }
508    DataArrayView::swap(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis_offset);    DataArrayView::swapaxes(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis0,axis1);
509  }  }
510    
511  void  void
# Line 583  DataTagged::eigenvalues_and_eigenvectors Line 556  DataTagged::eigenvalues_and_eigenvectors
556    
557  }  }
558    
559    void
560    DataTagged::setToZero(){
561        DataArrayView::ValueType::size_type n=m_data.size();
562        for (int i=0; i<n ;++i) m_data[i]=0.;
563    }
564    
565    void
566    DataTagged::dump(const std::string fileName) const
567    {
568       #ifdef PASO_MPI
569       throw DataException("Error - DataTagged:: dump is not implemented for MPI yet.")
570       #endif
571       #ifdef USE_NETCDF
572       const int ldims=DataArrayView::maxRank+1;
573       const NcDim* ncdims[ldims];
574       NcVar *var, *tags_var;
575       int rank = getPointDataView().getRank();
576       int type=  getFunctionSpace().getTypeCode();
577       int ndims =0;
578       long dims[ldims];
579       const double* d_ptr=&(m_data[0]);
580       DataArrayView::ShapeType shape = getPointDataView().getShape();
581    
582       // netCDF error handler
583       NcError err(NcError::verbose_nonfatal);
584       // Create the file.
585       NcFile dataFile(fileName.c_str(), NcFile::Replace);
586       // check if writing was successful
587       if (!dataFile.is_valid())
588            throw DataException("Error - DataTagged:: opening of netCDF file for output failed.");
589       if (!dataFile.add_att("type_id",1) )
590            throw DataException("Error - DataTagged:: appending data type to netCDF file failed.");
591       if (!dataFile.add_att("rank",rank) )
592            throw DataException("Error - DataTagged:: appending rank attribute to netCDF file failed.");
593       if (!dataFile.add_att("function_space_type",type))
594            throw DataException("Error - DataTagged:: appending function space attribute to netCDF file failed.");
595       ndims=rank+1;
596       if ( rank >0 ) {
597           dims[0]=shape[0];
598           if (! (ncdims[0] = dataFile.add_dim("d0",shape[0])) )
599                throw DataException("Error - DataTagged:: appending ncdimsion 0 to netCDF file failed.");
600       }
601       if ( rank >1 ) {
602           dims[1]=shape[1];
603           if (! (ncdims[1] = dataFile.add_dim("d1",shape[1])) )
604                throw DataException("Error - DataTagged:: appending ncdimsion 1 to netCDF file failed.");
605       }
606       if ( rank >2 ) {
607           dims[2]=shape[2];
608           if (! (ncdims[2] = dataFile.add_dim("d2", shape[2])) )
609                throw DataException("Error - DataTagged:: appending ncdimsion 2 to netCDF file failed.");
610       }
611       if ( rank >3 ) {
612           dims[3]=shape[3];
613           if (! (ncdims[3] = dataFile.add_dim("d3", shape[3])) )
614                throw DataException("Error - DataTagged:: appending ncdimsion 3 to netCDF file failed.");
615       }
616       const DataTagged::DataMapType& thisLookup=getTagLookup();
617       DataTagged::DataMapType::const_iterator i;
618       DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
619       int ntags=1;
620       for (i=thisLookup.begin();i!=thisLookupEnd;i++) ntags++;
621       int* tags =(int*) malloc(ntags*sizeof(int));
622       int c=1;
623       tags[0]=-1;
624       for (i=thisLookup.begin();i!=thisLookupEnd;i++) tags[c++]=i->first;
625       dims[rank]=ntags;
626       if (! (ncdims[rank] = dataFile.add_dim("num_tags", dims[rank])) )
627       {
628           free(tags);
629               throw DataException("Error - DataTagged:: appending num_tags to netCDF file failed.");
630       }
631       if (! ( tags_var = dataFile.add_var("tags", ncInt, ncdims[rank])) )
632       {
633        free(tags);
634            throw DataException("Error - DataTagged:: appending tags to netCDF file failed.");
635       }
636       if (! (tags_var->put(tags,dims[rank])) )
637       {
638        free(tags);
639            throw DataException("Error - DataTagged:: copy tags to netCDF buffer failed.");
640       }
641       if (! ( var = dataFile.add_var("data", ncDouble, ndims, ncdims)) )
642       {
643        free(tags);
644            throw DataException("Error - DataTagged:: appending variable to netCDF file failed.");
645       }
646       if (! (var->put(d_ptr,dims)) )
647       {
648        free(tags);
649            throw DataException("Error - DataTagged:: copy data to netCDF buffer failed.");
650       }
651       #else
652       throw DataException("Error - DataTagged:: dump is not configured with netCDF. Please contact your installation manager.");
653       #endif
654    }
655  }  // end of namespace  }  // end of namespace

Legend:
Removed from v.800  
changed lines
  Added in v.1141

  ViewVC Help
Powered by ViewVC 1.1.26