/[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 757 by woo409, Mon Jun 26 13:12:56 2006 UTC revision 1020 by phornby, Mon Mar 12 10:12:36 2007 UTC
# Line 16  Line 16 
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 131  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 443  DataTagged::extractData(ifstream& archiv Line 414  DataTagged::extractData(ifstream& archiv
414    return(m_data.extractData(archiveFile, noValues));    return(m_data.extractData(archiveFile, noValues));
415  }  }
416  void  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)  DataTagged::eigenvalues(DataAbstract* ev)
511  {  {
512    DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);    DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
# Line 490  DataTagged::eigenvalues_and_eigenvectors Line 554  DataTagged::eigenvalues_and_eigenvectors
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.757  
changed lines
  Added in v.1020

  ViewVC Help
Powered by ViewVC 1.1.26