Grid.h

00001 #ifndef gridripper_amr1d_Grid_h
00002 #define gridripper_amr1d_Grid_h
00003 
00004 #include <gridripper/config.h>
00005 #include <gridripper/amr1d/PDE.h>
00006 #include <gridripper/amr1d/FieldWrapper.h>
00007 #include <gridripper/math/XFArray.h>
00008 #include <gridripper/lang/IllegalArgumentException.h>
00009 #include <gridripper/tvalarray.h>
00010 #include <gridripper/tvector.h>
00011 #include <iostream>
00012 #include <map>
00013 
00014 namespace gridripper { namespace amr1d {
00015 
00016 class GridIterator;
00017 class Integrator;
00018 class Regridder;
00019 class IntervalList;
00020 class Sigma;
00021 
00022 using namespace std;
00023 
00030 class Grid
00031 {
00032     friend class GridIterator;
00033     friend class Regridder;
00034     friend class Integrator;
00035     friend class AMRCore;
00036     friend class PDE::FiniteIntegral;
00037 
00038 public:
00039     enum {LEFT_BOUND_FLAG = 1, RIGHT_BOUND_FLAG = 2};
00040 
00041     enum {IC_CONSTR_MASK = 3, // Constraint mask in options
00042           IC_CONSTR_NONE = 0, // Constraints are not applied in the points of the
00043                            // new subgrid.
00044           IC_CONSTR_INTERPOLATED_ONLY = 1, // Constraints are applied in the
00045                                         // interpolated points of the new
00046                                         // subgrid.
00047           IC_CONSTR_ALL = 2 // Constraints are applied in all points of the new
00048                          // subgrid.
00049     };
00050 
00051 protected:
00052     int flags;
00053 
00055     tvalarray<GReal_t> theData;
00056 
00057     tvalarray<bool> refinedDataExists;
00058 
00059 private:
00061     GReal_t deltaX;
00062 
00064     GReal_t baseMinX;
00065 
00067     GReal_t baseMaxX;
00068 
00070     GReal_t locationX;
00071 
00073     int locationI;
00074 
00076     GReal_t currentTime;
00077 
00079     int timeStepCount;
00080 
00082     int lastErrorCheckTime;
00083 
00085     int numComponents;
00086 
00088     int refinementRatio;
00089 
00090     int refinedMaximumI;
00091 
00097     int dataIndexMultiplier;
00098 
00100     int maximumI;
00101 
00102     tvalarray<GReal_t> energyFlow;
00103 
00104     tvalarray<GReal_t> energyFlowLastUpdateTime;
00105 
00106     tvalarray<int> energyFlowLastUpdateLevel;
00107 
00108     tvalarray<GReal_t> energyLoss;
00109 
00110     Regridder* regridder;
00111 
00112     tvalarray<Grid*> subGrids;
00113 
00114     tvalarray<Grid*> newSubSubgrids;
00115 
00117     int levelInTree;
00118 
00119     Integrator* integrator;
00120 
00122     Grid* parentGrid;
00123 
00125     Grid* nextSibling;
00126 
00128     Grid* offspringGrid;
00129 
00130 protected:
00136     Grid(const Grid& g);
00137 
00141     Grid(GReal_t minx, GReal_t maxx, int maxi, int nc,
00142          Regridder* refproto, int r, GReal_t dx);
00143 
00149     Grid(const Grid& p, GReal_t xshift);
00150 
00162     Grid(Grid* p, int r1, int loci, int maxi,
00163          int margin, int vmargin, int minLevel);
00164 
00165 public:
00166     virtual ~Grid();
00167 
00168 protected:
00174     void copy(Grid* orig, map<Grid*, Grid*>& gridmap);
00175 
00182     virtual int getInternalIndex(int i) const =0;
00183 
00190     int getRealIndex(int i) const {
00191         return i;
00192     }
00193 
00194     virtual int getSubgridIndexFromIndex(const Grid* g, int i) const =0;
00195 
00196     virtual int getIndexFromSubgridIndex(const Grid* g, int i) const =0;
00197 
00209     virtual Grid* createRefinedGrid(IntervalList* iv,
00210                                     int margin, int vmargin,
00211                                     int minLevel, int r1) =0;
00212 
00219     void init(Integrator* proto, const Sigma* sigma, int n);
00220 
00221     void initEnergyLoss(PDE& pde);
00222 
00223     void updateEnergyLoss(const PDE& pde, int i, Grid& baseg, int jbase,
00224                           GReal_t epsilon);
00225 
00226     XFArray* getDensityData(const PDE::DensityQuantity& q,
00227                             GReal_t xmin, GReal_t xmax) const;
00228 
00235     virtual void blur(tvalarray<unsigned char>& pf, int i) const =0;
00236 
00242     virtual int getFirstIndexForRefinement(const tvalarray<unsigned char>& pf)
00243             const =0;
00244 
00251     virtual int findIntervalEndForRefinement(
00252             const tvalarray<unsigned char>& pf, int i) const =0;
00253 
00254     virtual int intervalRefinedSize(const IntervalList* iv, int r) const =0;
00255 
00256     virtual int gridIndexLeftOf(int i, int dmax) const =0;
00257 
00258     virtual int gridIndexRightOf(int i, int dmax) const =0;
00259 
00260     virtual void fixSubgridNearBounds(IntervalList* l, IntervalList* r,
00261                                       int margin) const =0;
00262 
00271     virtual int calcNewSubSubgridLocation(int ssloc,
00272                                           int osloc, int nsloc) =0;
00273 
00274 public:
00279     virtual Grid* cloneGrid() const =0;
00280 
00281     GReal_t getBaseMinX() const {
00282         return baseMinX;
00283     }
00284 
00285     GReal_t getBaseMaxX() const {
00286         return baseMaxX;
00287     }
00288 
00292     GReal_t getLocationX() const {
00293         return locationX;
00294     }
00295 
00296     GridIterator getXIterator() const;
00297 
00302     virtual GReal_t getSizeX() const =0;
00303 
00304     virtual int getLeftmostI() const =0;
00305 
00306     virtual int getRightmostI() const =0;
00307 
00308     virtual bool isPeriodic() const =0;
00309 
00314     virtual GReal_t getLocationX(int i) const =0;
00315 
00316     int getIndexFromX(GReal_t x, double maxerr) const
00317         throw(IllegalArgumentException&);
00318 
00322     int getLocationI() const {
00323         return locationI;
00324     }
00325 
00326     GReal_t getDeltaX() const {
00327         return deltaX;
00328     }
00329 
00330     void setTime(GReal_t t) {
00331         currentTime = t;
00332     }
00333 
00334     GReal_t getTime() const {
00335         return currentTime;
00336     }
00337 
00338     void setTimeStepCount(int tsc) {
00339         timeStepCount = tsc;
00340     }
00341 
00342     int getTimeStepCount() const {
00343         return timeStepCount;
00344     }
00345 
00346     int getLastErrorCheckTime() const {
00347         return lastErrorCheckTime;
00348     }
00349 
00354     int getNumComponents() const {
00355         return numComponents;
00356     }
00357 
00362     int getRefinementRatio() const {
00363         return refinementRatio;
00364     }
00365 
00371     virtual int getMarginLeft(int ir) const =0;
00372 
00378     virtual int getMarginRight(int ir) const =0;
00379 
00384     int getMaxI() const {
00385         return maximumI;
00386     }
00387 
00392     virtual int getNumPointsAboveMaxX() const {
00393         return 0;
00394     }
00395 
00400     int getLevel() const {
00401         return levelInTree;
00402     }
00403 
00405     Grid* getParent() const {
00406         return parentGrid;
00407     }
00408 
00410     Grid* getOffspring() const {
00411         return offspringGrid;
00412     }
00413 
00415     Grid* getNextSibling() const {
00416         return nextSibling;
00417     }
00418 
00419     bool containsLeftBound() const {
00420         return (flags & LEFT_BOUND_FLAG) != 0;
00421     }
00422 
00423     bool containsRightBound() const {
00424         return (flags & RIGHT_BOUND_FLAG) != 0;
00425     }
00426 
00433     virtual int distanceFromLeft(int i) const =0;
00434 
00441     virtual int distanceFromRight(int i) const =0;
00442 
00443     int getAbsoluteIndex(int i) const {
00444         return getAbsoluteIndex(i, 1);
00445     }
00446 
00447     virtual int getAbsoluteIndex(int i, int mul) const =0;
00448 
00454     void getField(int i, FieldWrapper& f) const {
00455         i = getInternalIndex(i);
00456         int off = i*dataIndexMultiplier;
00457         GReal_t* fdata = f.data();
00458         unsigned nc = f.size();
00459         for(unsigned j = 0; j < nc; ++j) {
00460             fdata[j] = theData[off + j];
00461         }
00462     }
00463 
00464     void setEnergyLosses(const tvalarray<GReal_t>& e, PDE* pde);
00465 
00472     GReal_t getEnergyFlow(int i) const {
00473         return energyFlow[i];
00474     }
00475 
00481     GReal_t getEnergyLoss(int i) const {
00482         return energyLoss[i];
00483     }
00484 
00490     void addTo(int i, GReal_t v[]) const {
00491         i = getInternalIndex(i)*dataIndexMultiplier;
00492         for(int j = 0; j < numComponents; ++j) {
00493             v[j] += theData[i + j];
00494         }
00495     }
00496 
00503     void addMultipliedTo(int i, GReal_t c, GReal_t v[]) const {
00504         i = getInternalIndex(i)*dataIndexMultiplier;
00505         for(int j = 0; j < numComponents; ++j) {
00506             v[j] += c*theData[i + j];
00507         }
00508     }
00509 
00518     void addMultipliedTo(int i, int exclude, GReal_t c, const GReal_t arr[],
00519                          GReal_t v[]) const {
00520         int b = 1;
00521         i = getInternalIndex(i)*dataIndexMultiplier;
00522         for(int j = 0; j < numComponents; ++j) {
00523             if((exclude & b) == 0) {
00524                 v[j] += c*arr[j]*theData[i + j];
00525             }
00526             b <<= 1;
00527         }
00528     }
00529 
00536     void get(int i, GReal_t v[], int off = 0) const {
00537         i = getInternalIndex(i);
00538         int off0 = i*dataIndexMultiplier;
00539         for(int j = 0; j < numComponents; ++j) {
00540             v[off + j] = theData[off0 + j];
00541         }
00542     }
00543 
00549     void set(int i, GReal_t v[]) {
00550         int j = getInternalIndex(i);
00551         int off0 = j*dataIndexMultiplier;
00552         for(int k = 0; k < numComponents; ++k) {
00553             theData[off0 + k] = v[k];
00554         }
00555     }
00556 
00563     GReal_t getComponent(int i, int k) const {
00564         i = getInternalIndex(i);
00565         return theData[i*dataIndexMultiplier + k];
00566     }
00567 
00571     virtual void initRefinedDataExistenceArray() =0;
00572 
00577     int getRefinedMaxI() const {
00578         return refinedMaximumI;
00579     }
00580 
00586     virtual bool hasRefinedData(int i) const =0;
00587 
00595     int getRefinedDataExistence(int i, int n, const PDE& pde) const;
00596 
00602     const Grid* getSubgridAt(int i) const {
00603         return subGrids.size() == 0? NULL : subGrids[i];
00604     }
00605 
00611     const Grid* getFinestSubgridAt(GReal_t x) const;
00612 
00618     virtual void getRefinedData(int i, GReal_t yout[]) const =0;
00619 
00621     void setZero();
00622 
00623     Regridder* getRegridder() {
00624         return regridder;
00625     }
00626 
00627     void setRegridder(Regridder* r);
00628 
00634     bool hasUncoveredFlaggedPoint(int margin);
00635 
00636     virtual Grid* createTemporaryGrid(int dmargin_L, int dmargin_R,
00637                                       GReal_t xshift) =0;
00638 
00644     void setTemporaryGridShift(const Grid& p, GReal_t xshift) {
00645         locationX = p.getLocationX() + xshift;
00646     }
00647 
00654     virtual void setTemporaryGridMargins(const Grid& p, int dmargin_L,
00655                                          int dmargin_R) =0;
00656 
00658     Integrator* getIntegrator() const {
00659         return integrator;
00660     }
00661 
00663     void injectFineToCoarse(PDE* pde);
00664 
00669     void checkErrors(double critical);
00670 
00682     int generateRefinedGrids(int minLevel, int maxLevel,
00683                              int numVelocity, int bufzoneradius,
00684                              int thislevel, int gridi);
00685 
00689     void interpolateNewSubgrids(PDE* pde) throw(IllegalArgumentException&);
00690 
00694     void initConstraints(PDE* pde, FieldWrapper& tmp, int opts);
00695 
00696     bool isEnergyLossStored() const {
00697         return energyLoss.size() != 0;
00698     }
00699 
00700     string getDataAsString() const;
00701 
00702     string toString() const;
00703 
00704     string sprintHierarchy() const;
00705 
00706     virtual void interpolateMargins(PDE* pde) =0;
00707 
00708 protected:
00713     virtual void fillRefinedDataArray() =0;
00714 
00718     void initConstraints(PDE* pde, int left, int right,
00719                          FieldWrapper& tmp, int opts);
00720 
00721 private:
00722     string sprintHierarchy(int tabs, const Grid* gg) const;
00723 
00724     static void fillGridArray(tvalarray<Grid*>& d, int left, int right,
00725                               Grid* g);
00726 
00731     void initSubsub(Grid* p, int loci, int maxi, int minLevel);
00732 };
00733 
00734 } } // namespace gridripper::amr1d
00735 
00737 inline std::ostream& operator <<(std::ostream& os,
00738                                  const gridripper::amr1d::Grid& g)
00739 {
00740     return os << g.toString();
00741 }
00742 
00743 #endif /* gridripper_amr1d_Grid_h */

Generated on Wed Jun 17 18:46:47 2009 for GridRipper by  doxygen 1.5.6