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

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

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

revision 474 by jgs, Mon Jan 30 04:23:44 2006 UTC revision 950 by gross, Tue Feb 6 07:01:11 2007 UTC
# Line 1  Line 1 
1  // $Id$  // $Id$
2  /*  /*
3   ******************************************************************************   ************************************************************
4   *                                                                            *   *          Copyright 2006 by ACcESS MNRF                   *
5   *       COPYRIGHT  ACcESS 2004 -  All Rights Reserved                        *   *                                                          *
6   *                                                                            *   *              http://www.access.edu.au                    *
7   * This software is the property of ACcESS. No part of this code              *   *       Primary Business: Queensland, Australia            *
8   * may be copied in any form or by any means without the expressed written    *   *  Licensed under the Open Software License version 3.0    *
9   * consent of ACcESS.  Copying, use or modification of this software          *   *     http://www.opensource.org/licenses/osl-3.0.php       *
10   * by any unauthorised person is illegal unless that person has a software    *   *                                                          *
11   * license agreement with ACcESS.                                             *   ************************************************************
  *                                                                            *  
  ******************************************************************************  
12  */  */
13    
 #include "DataException.h"  
14  #include "DataExpanded.h"  #include "DataExpanded.h"
15    #include "DataException.h"
16  #include "DataConstant.h"  #include "DataConstant.h"
17  #include "DataTagged.h"  #include "DataTagged.h"
 #include "DataArrayView.h"  
18    
19  #include <boost/python/extract.hpp>  #include <boost/python/extract.hpp>
20    
 #include <iostream>  
   
21  using namespace std;  using namespace std;
22  using namespace boost::python;  using namespace boost::python;
23  using namespace boost;  using namespace boost;
# Line 159  DataExpanded::~DataExpanded() Line 154  DataExpanded::~DataExpanded()
154  {  {
155  }  }
156    
 void  
 DataExpanded::reshapeDataPoint(const DataArrayView::ShapeType& shape)  
 {  
   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());  
   }  
   //  
   // create the new DataBlocks2D data array, and a corresponding DataArrayView  
   DataBlocks2D newData(getNumSamples(),getNumDPPSample(),DataArrayView::noValues(shape));  
   DataArrayView newView(newData.getData(),shape);  
   //  
   // Copy the original data to every value for the new shape  
   int i,j;  
   int nRows=m_data.getNumRows();  
   int nCols=m_data.getNumCols();  
   #pragma omp parallel for private(i,j) schedule(static)  
   for (i=0;i<nRows;i++) {  
     for (j=0;j<nCols;j++) {  
       // NOTE: An exception may be thown from this call if  
       // DOASSERT is on which of course will play  
       // havoc with the omp threads. Run single threaded  
       // if using DOASSERT.  
       newView.copy(newData.index(i,j),m_data(i,j));  
     }  
   }  
   // swap the new data array for the original  
   m_data.Swap(newData);  
   // set the corresponding DataArrayView  
   DataArrayView temp(m_data.getData(),shape);  
   setPointDataView(temp);  
 }  
   
157  DataAbstract*  DataAbstract*
158  DataExpanded::getSlice(const DataArrayView::RegionType& region) const  DataExpanded::getSlice(const DataArrayView::RegionType& region) const
159  {  {
# Line 218  DataExpanded::setSlice(const DataAbstrac Line 177  DataExpanded::setSlice(const DataAbstrac
177    if (getPointDataView().getRank()!=region.size()) {    if (getPointDataView().getRank()!=region.size()) {
178      throw DataException("Error - Invalid slice region.");      throw DataException("Error - Invalid slice region.");
179    }    }
180    if (tempDataExp->getPointDataView().getRank()>0 and !value->getPointDataView().checkShape(shape)) {    if (tempDataExp->getPointDataView().getRank()>0 && !value->getPointDataView().checkShape(shape)) {
181      throw DataException (value->getPointDataView().createShapeErrorMessage(      throw DataException (value->getPointDataView().createShapeErrorMessage(
182          "Error - Couldn't copy slice due to shape mismatch.",shape));          "Error - Couldn't copy slice due to shape mismatch.",shape));
183    }    }
# Line 294  string Line 253  string
253  DataExpanded::toString() const  DataExpanded::toString() const
254  {  {
255    stringstream temp;    stringstream temp;
256      FunctionSpace fs=getFunctionSpace();
257    //    //
258    // create a temporary view as the offset will be changed    // create a temporary view as the offset will be changed
259    DataArrayView tempView(getPointDataView().getData(),getPointDataView().getShape(),getPointDataView().getOffset());    DataArrayView tempView(getPointDataView().getData(),getPointDataView().getShape(),getPointDataView().getOffset());
# Line 301  DataExpanded::toString() const Line 261  DataExpanded::toString() const
261      for (int j=0;j<m_data.getNumCols();j++) {      for (int j=0;j<m_data.getNumCols();j++) {
262        tempView.setOffset(m_data.index(i,j));        tempView.setOffset(m_data.index(i,j));
263        stringstream suffix;        stringstream suffix;
264        suffix << "(" << i << "," << j << ")";        suffix << "( id: " << i << ", ref: " << fs.getReferenceNoFromSampleNo(i) << ", pnt: " << j << ")";
265        temp << tempView.toString(suffix.str());        temp << tempView.toString(suffix.str());
266        if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {        if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {
267          temp << endl;          temp << endl;
# Line 333  DataExpanded::getLength() const Line 293  DataExpanded::getLength() const
293  }  }
294    
295  void  void
296  DataExpanded::setRefValue(int ref,  DataExpanded::setValueByReferenceNumber(int ref,
297                            const DataArray& value)                            const DataArray& value)
298  {  {
299    //    //
# Line 353  DataExpanded::setRefValue(int ref, Line 313  DataExpanded::setRefValue(int ref,
313      }      }
314    }    }
315    if (sampleNo == -1) {    if (sampleNo == -1) {
316      throw DataException("DataExpanded::setRefValue error: invalid ref number supplied.");      throw DataException("DataExpanded::setValueByReferenceNumber error: invalid ref number supplied.");
317    }    }
318    
319    for (int n=0; n<numDPPSample; n++) {    for (int n=0; n<numDPPSample; n++) {
# Line 367  DataExpanded::setRefValue(int ref, Line 327  DataExpanded::setRefValue(int ref,
327  }  }
328    
329  void  void
330  DataExpanded::getRefValue(int ref,  DataExpanded::getValueByReferenceNumber(int ref,
331                            DataArray& value)                            DataArray& value)
332  {  {
333    //    //
# Line 387  DataExpanded::getRefValue(int ref, Line 347  DataExpanded::getRefValue(int ref,
347      }      }
348    }    }
349    if (sampleNo == -1) {    if (sampleNo == -1) {
350      throw DataException("DataExpanded::getRefValue error: invalid ref number supplied.");      throw DataException("DataExpanded::getValueByReferenceNumber error: invalid ref number supplied.");
351    }    }
352    
353    //    //
# Line 413  DataExpanded::extractData(ifstream& arch Line 373  DataExpanded::extractData(ifstream& arch
373    return(m_data.extractData(archiveFile, noValues));    return(m_data.extractData(archiveFile, noValues));
374  }  }
375    
376    void
377    DataExpanded::copyToDataPoint(const int sampleNo, const int dataPointNo, const double value) {
378      //
379      // Get the number of samples and data-points per sample.
380      int numSamples = getNumSamples();
381      int numDataPointsPerSample = getNumDPPSample();
382      int dataPointRank = getPointDataView().getRank();
383      ShapeType dataPointShape = getPointDataView().getShape();
384      if (numSamples*numDataPointsPerSample > 0) {
385         //TODO: global error handling
386         if ((sampleNo >= numSamples) || (sampleNo < 0 )) {
387              throw DataException("Error - DataExpanded::copyDataPoint invalid sampleNo.");
388         }
389         if ((dataPointNo >= numDataPointsPerSample) || (dataPointNo < 0)) {
390               throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");
391         }
392         DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);
393         if (dataPointRank==0) {
394             dataPointView()=value;
395         } else if (dataPointRank==1) {
396            for (int i=0; i<dataPointShape[0]; i++) {
397                dataPointView(i)=value;
398            }
399         } else if (dataPointRank==2) {
400            for (int i=0; i<dataPointShape[0]; i++) {
401               for (int j=0; j<dataPointShape[1]; j++) {
402                  dataPointView(i,j)=value;
403               }
404            }
405         } else if (dataPointRank==3) {
406            for (int i=0; i<dataPointShape[0]; i++) {
407               for (int j=0; j<dataPointShape[1]; j++) {
408                  for (int k=0; k<dataPointShape[2]; k++) {
409                     dataPointView(i,j,k)=value;
410                  }
411               }
412            }
413         } else if (dataPointRank==4) {
414             for (int i=0; i<dataPointShape[0]; i++) {
415               for (int j=0; j<dataPointShape[1]; j++) {
416                 for (int k=0; k<dataPointShape[2]; k++) {
417                   for (int l=0; l<dataPointShape[3]; l++) {
418                      dataPointView(i,j,k,l)=value;
419                   }
420                 }
421               }
422             }
423         }
424      }
425    }
426    void
427    DataExpanded::copyToDataPoint(const int sampleNo, const int dataPointNo, const boost::python::numeric::array& value) {
428      //
429      // Get the number of samples and data-points per sample.
430      int numSamples = getNumSamples();
431      int numDataPointsPerSample = getNumDPPSample();
432      int dataPointRank = getPointDataView().getRank();
433      ShapeType dataPointShape = getPointDataView().getShape();
434      //
435      // check rank:
436      if (value.getrank()!=dataPointRank)
437           throw DataException("Rank of numarray does not match Data object rank");
438      if (numSamples*numDataPointsPerSample > 0) {
439         //TODO: global error handling
440         if ((sampleNo >= numSamples) || (sampleNo < 0 )) {
441              throw DataException("Error - DataExpanded::copyDataPoint invalid sampleNo.");
442         }
443         if ((dataPointNo >= numDataPointsPerSample) || (dataPointNo < 0)) {
444               throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");
445         }
446         DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);
447         if (dataPointRank==0) {
448             dataPointView()=extract<double>(value[0]);
449         } else if (dataPointRank==1) {
450            for (int i=0; i<dataPointShape[0]; i++) {
451                dataPointView(i)=extract<double>(value[i]);
452            }
453         } else if (dataPointRank==2) {
454            for (int i=0; i<dataPointShape[0]; i++) {
455               for (int j=0; j<dataPointShape[1]; j++) {
456                  dataPointView(i,j)=extract<double>(value[i][j]);
457               }
458            }
459         } else if (dataPointRank==3) {
460            for (int i=0; i<dataPointShape[0]; i++) {
461               for (int j=0; j<dataPointShape[1]; j++) {
462                  for (int k=0; k<dataPointShape[2]; k++) {
463                     dataPointView(i,j,k)=extract<double>(value[i][j][k]);
464                  }
465               }
466            }
467         } else if (dataPointRank==4) {
468             for (int i=0; i<dataPointShape[0]; i++) {
469               for (int j=0; j<dataPointShape[1]; j++) {
470                 for (int k=0; k<dataPointShape[2]; k++) {
471                   for (int l=0; l<dataPointShape[3]; l++) {
472                      dataPointView(i,j,k,l)=extract<double>(value[i][j][k][l]);
473                   }
474                 }
475               }
476             }
477         }
478      }
479    }
480  void  void
481  DataExpanded::copyAll(const boost::python::numeric::array& value) {  DataExpanded::copyAll(const boost::python::numeric::array& value) {
482    //    //
# Line 467  DataExpanded::copyAll(const boost::pytho Line 531  DataExpanded::copyAll(const boost::pytho
531      }      }
532    }    }
533  }  }
534    void
535    DataExpanded::symmetric(DataAbstract* ev)
536    {
537      int sampleNo,dataPointNo;
538      int numSamples = getNumSamples();
539      int numDataPointsPerSample = getNumDPPSample();
540      DataExpanded* temp_ev=dynamic_cast<DataExpanded*>(ev);
541      if (temp_ev==0) {
542        throw DataException("Error - DataExpanded::symmetric: casting to DataExpanded failed (propably a programming error).");
543      }
544      DataArrayView& thisView=getPointDataView();
545      DataArrayView& evView=ev->getPointDataView();
546      #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
547      for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
548        for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
549             DataArrayView::symmetric(thisView,getPointOffset(sampleNo,dataPointNo),
550                                        evView,ev->getPointOffset(sampleNo,dataPointNo));
551        }
552      }
553    }
554    void
555    DataExpanded::nonsymmetric(DataAbstract* ev)
556    {
557      int sampleNo,dataPointNo;
558      int numSamples = getNumSamples();
559      int numDataPointsPerSample = getNumDPPSample();
560      DataExpanded* temp_ev=dynamic_cast<DataExpanded*>(ev);
561      if (temp_ev==0) {
562        throw DataException("Error - DataExpanded::nonsymmetric: casting to DataExpanded failed (propably a programming error).");
563      }
564      DataArrayView& thisView=getPointDataView();
565      DataArrayView& evView=ev->getPointDataView();
566      #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
567      for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
568        for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
569             DataArrayView::nonsymmetric(thisView,getPointOffset(sampleNo,dataPointNo),
570                                        evView,ev->getPointOffset(sampleNo,dataPointNo));
571        }
572      }
573    }
574    void
575    DataExpanded::trace(DataAbstract* ev, int axis_offset)
576    {
577      int sampleNo,dataPointNo;
578      int numSamples = getNumSamples();
579      int numDataPointsPerSample = getNumDPPSample();
580      DataExpanded* temp_ev=dynamic_cast<DataExpanded*>(ev);
581      if (temp_ev==0) {
582        throw DataException("Error - DataExpanded::trace: casting to DataExpanded failed (propably a programming error).");
583      }
584      DataArrayView& thisView=getPointDataView();
585      DataArrayView& evView=ev->getPointDataView();
586      #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
587      for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
588        for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
589             DataArrayView::trace(thisView,getPointOffset(sampleNo,dataPointNo),
590                                        evView,ev->getPointOffset(sampleNo,dataPointNo),axis_offset);
591        }
592      }
593    }
594    
595    void
596    DataExpanded::transpose(DataAbstract* ev, int axis_offset)
597    {
598      int sampleNo,dataPointNo;
599      int numSamples = getNumSamples();
600      int numDataPointsPerSample = getNumDPPSample();
601      DataExpanded* temp_ev=dynamic_cast<DataExpanded*>(ev);
602      if (temp_ev==0) {
603        throw DataException("Error - DataExpanded::transpose: casting to DataExpanded failed (propably a programming error).");
604      }
605      DataArrayView& thisView=getPointDataView();
606      DataArrayView& evView=ev->getPointDataView();
607      #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
608      for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
609        for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
610             DataArrayView::transpose(thisView,getPointOffset(sampleNo,dataPointNo),
611                                        evView,ev->getPointOffset(sampleNo,dataPointNo),axis_offset);
612        }
613      }
614    }
615    
616    void
617    DataExpanded::swapaxes(DataAbstract* ev, int axis0, int axis1)
618    {
619      int sampleNo,dataPointNo;
620      int numSamples = getNumSamples();
621      int numDataPointsPerSample = getNumDPPSample();
622      DataExpanded* temp_ev=dynamic_cast<DataExpanded*>(ev);
623      if (temp_ev==0) {
624        throw DataException("Error - DataExpanded::swapaxes: casting to DataExpanded failed (propably a programming error).");
625      }
626      DataArrayView& thisView=getPointDataView();
627      DataArrayView& evView=ev->getPointDataView();
628      #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
629      for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
630        for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
631             DataArrayView::swapaxes(thisView,getPointOffset(sampleNo,dataPointNo),
632                                        evView,ev->getPointOffset(sampleNo,dataPointNo),axis0,axis1);
633        }
634      }
635    }
636    void
637    DataExpanded::eigenvalues(DataAbstract* ev)
638    {
639      int sampleNo,dataPointNo;
640      int numSamples = getNumSamples();
641      int numDataPointsPerSample = getNumDPPSample();
642      DataExpanded* temp_ev=dynamic_cast<DataExpanded*>(ev);
643      if (temp_ev==0) {
644        throw DataException("Error - DataExpanded::eigenvalues: casting to DataExpanded failed (propably a programming error).");
645      }
646      DataArrayView& thisView=getPointDataView();
647      DataArrayView& evView=ev->getPointDataView();
648      #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
649      for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
650        for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
651             DataArrayView::eigenvalues(thisView,getPointOffset(sampleNo,dataPointNo),
652                                        evView,ev->getPointOffset(sampleNo,dataPointNo));
653        }
654      }
655    }
656    void
657    DataExpanded::eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol)
658    {
659      int numSamples = getNumSamples();
660      int numDataPointsPerSample = getNumDPPSample();
661      int sampleNo,dataPointNo;
662      DataExpanded* temp_ev=dynamic_cast<DataExpanded*>(ev);
663      if (temp_ev==0) {
664        throw DataException("Error - DataExpanded::eigenvalues_and_eigenvectors: casting to DataExpanded failed (propably a programming error).");
665      }
666      DataExpanded* temp_V=dynamic_cast<DataExpanded*>(V);
667      if (temp_V==0) {
668        throw DataException("Error - DataExpanded::eigenvalues_and_eigenvectors: casting to DataExpanded failed (propably a programming error).");
669      }
670      DataArrayView& thisView=getPointDataView();
671      DataArrayView& evView=ev->getPointDataView();
672      DataArrayView& VView=V->getPointDataView();
673      #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
674      for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
675        for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
676             DataArrayView::eigenvalues_and_eigenvectors(thisView,getPointOffset(sampleNo,dataPointNo),
677                                                         evView,ev->getPointOffset(sampleNo,dataPointNo),
678                                                         VView,V->getPointOffset(sampleNo,dataPointNo),
679                                                         tol);
680        }
681      }
682    }
683    
684    void
685    DataExpanded::dump(const std::string fileName) const
686    {
687        throw DataException("Error - DataExpanded:: dump: not implemented.");
688    }
689    
690    
691  }  // end of namespace  }  // end of namespace

Legend:
Removed from v.474  
changed lines
  Added in v.950

  ViewVC Help
Powered by ViewVC 1.1.26