5#ifndef RUN_STENCILSGPP_H
6#define RUN_STENCILSGPP_H
9#include "../../../SGpp/base/src/sgpp_base.hpp"
16 MonteCarlo_Hierarchichal,
22class StencilSGpp:
public StencilTemplate {
28 int numberofgridpoints=0;
29 CellDimension cellDimension;
30 QuadratureType quadrature_type;
31 int gridpoints=2147483647;
32 std::unique_ptr<sgpp::base::Grid> grid_MC;
34 int getMinimumGridPointsOfACell(){
35 int localvalue = gridpoints;
37 MPI_Reduce(&localvalue, &global_min, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
43 StencilSGpp(
AdaptiveSparseGrid& grid_,
const F &varCoeff_,
int level_=1,QuadratureType quadrature_type_=SGadaptive) : StencilTemplate(grid_), varCoeff(varCoeff_), level(level_), quadrature_type(quadrature_type_){
45 if(quadrature_type==MonteCarloFixed) {
46 grid_MC = std::unique_ptr<sgpp::base::Grid>(sgpp::base::Grid::createLinearGrid(DimensionSparseGrid));
47 grid_MC->getGenerator().regular(level);
52 mcPoints=grid.getDOFS();
59 inline double returnValue(
const IndexDimension &Index,
const MultiDimCompass &mc){cout <<
"StencilSGPP returnValue not implmented" << endl; exit(1);}
61 inline double integration(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ) {
65 if(quadrature_type == SGadaptive) {
66 value = integration_adaptive(cell,dirP,dirQ);
67 }
else if(quadrature_type == SGregular){
68 value = integration_regular(cell,dirP,dirQ);
69 }
else if(quadrature_type == MonteCarlo){
70 value = integration_MC(cell,dirP,dirQ);
71 }
else if(quadrature_type == MonteCarlo_Hierarchichal){
72 value = integration_MC_AD(cell,dirP,dirQ);
73 }
else if(quadrature_type == MonteCarloFixed) {
74 value = integration_MC_fixedCell(cell, dirP, dirQ);
80 inline double integration_regular(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ);
82 inline double integrationLocallyAdaptive(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ);
85 inline double integration_adaptive(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ);
87 inline double integration_gausslegendre(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ);
91 inline double integration_MC(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ);
94 inline double integration_MC_fixedCell(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ);
96 inline double integration_MC_fixedCell2(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ);
100 inline double integration_MC_fixed(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ);
104 inline double integration_MC_AD(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ);
116 FunctionClass (CellDimension& cell_, CellIndexDirection& dirP_, CellIndexDirection &dirQ_,F varCoeff_ ): cell(cell_),dirP(dirP_),dirQ(dirQ_),varCoeff(varCoeff_){
121 for(
int i=0; i<DimensionSparseGrid; i++){
122 double h = cell.getRightBoundary(i)-cell.getLeftBoundary(i);
129 double evaluation(
int dim,
double* x,
void* clientdata){
134 for(
int i=0; i<DimensionSparseGrid; i++){
135 x[i]=meshwidth[i]*x[i]+cell.getLeftBoundary(i);
145 for(
int i = 0; i < DimensionSparseGrid; ++i )
149 if(dirP.getDirection(i)==Left){
151 value *= (cell.getRightBoundary(i)-x[i]) / (meshwidth[i]);
155 value *= (x[i] - cell.getLeftBoundary(i)) / (meshwidth[i]);
159 if(dirQ.getDirection(i)==Left){
161 value *= (cell.getRightBoundary(i)-x[i]) / (meshwidth[i]);
166 value *= (x[i] - cell.getLeftBoundary(i)) / (meshwidth[i]);
173 value *= varCoeff(x);
182 double detJPrime(){
return det_J_prime;};
184 static double evaluationWrapper(
int dim,
double* x,
void* clientdata) {
185 return static_cast<FunctionClass*
>(clientdata)->evaluation(dim, x,
nullptr);
188 double det_J_prime = 1.0;
189 double meshwidth[DimensionSparseGrid];
191 CellIndexDirection dirP;
192 CellIndexDirection dirQ;
201double StencilSGpp<F>::integration_MC_AD(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ) {
204 FunctionClass function(cell,dirP,dirQ,varCoeff);
206 int levels[DimensionSparseGrid];
208 for(
int d=0; d<DimensionSparseGrid;d++){
209 levels[d]=grid.getMaxDepth(d)-(cell.getDepth(d));
210 maxlevel<levels[d]?maxlevel=levels[d]:maxlevel;
217 int dim = DimensionSparseGrid;
218 std::unique_ptr<sgpp::base::Grid> grid_sgpp(sgpp::base::Grid::createLinearGrid(dim));
219 sgpp::base::GridStorage& gridStorage = grid_sgpp->getStorage();
222 grid_sgpp->getGenerator().regular(1);
225 sgpp::base::DataVector alpha(gridStorage.getSize());
226 sgpp::base::DataVector refined(gridStorage.getSize());
229 std::vector<size_t> addedPoints;
232 for (
int step = 0; step < maxlevel; step++) {
235 for (
size_t i = 0; i < gridStorage.getSize(); i++) {
236 sgpp::base::GridPoint& gp = gridStorage.getPoint(i);
241 for(
size_t k=0; k< DimensionSparseGrid; k++){
242 if(gp.getLevel(k)> levels[k]){
252 if(refined[i]>1.0) alpha[i]=0.0;
267 sgpp::base::SurplusRefinementFunctor functor(alpha,alpha_c, 0.5);
268 grid_sgpp->getGenerator().refine(functor, &addedPoints);
271 alpha.resize(gridStorage.getSize());
272 refined.resize(gridStorage.getSize());
274 if(addedPoints.size()<1)
break;
278 double p[DimensionSparseGrid];
279 for (
size_t i = 0; i < gridStorage.getSize(); i++) {
280 sgpp::base::GridPoint& gp = gridStorage.getPoint(i);
281 for(
int j=0; j<DimensionSparseGrid;j++){
282 p[j] = gp.getStandardCoordinate(j);
284 alpha[i] = function.evaluation(DimensionSparseGrid, p, NULL);
287 if(numberofgridpoints<gridStorage.getSize()){
288 numberofgridpoints=gridStorage.getSize();
289 cellDimension = cell;
294 std::unique_ptr<sgpp::base::OperationHierarchisation>(sgpp::op_factory::createOperationHierarchisation(*grid_sgpp))
295 ->doHierarchisation(alpha);
297 sgpp::base::OperationQuadratureMC opMC(*grid_sgpp,gridStorage.getSize());
303 double res = opMC.doQuadrature(alpha);
308 res *= function.detJPrime();
314double StencilSGpp<F>::integration_MC_fixed(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ) {
316 FunctionClass function(cell,dirP,dirQ,varCoeff);
318 int dim = DimensionSparseGrid;
322 std::unique_ptr<sgpp::base::Grid> grid2(sgpp::base::Grid::createLinearBoundaryGrid(DimensionSparseGrid));
327 sgpp::base::OperationQuadratureMC opMC(*grid2, grid.getDOFS());
329 double res = opMC.doQuadratureFunc(FunctionClass::evaluationWrapper, &function);
330 res *= function.detJPrime();
336double StencilSGpp<F>::integration_MC_fixedCell(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ) {
339 FunctionClass function(cell,dirP,dirQ,varCoeff);
342 for(
int d=0; d<DimensionSparseGrid;d++){
343 l+=cell.getDepth(d)-1;
346 int vol_cell= POW2(
unsigned (l));
348 if(vol_cell>grid_MC->getSize()) mcPoints=1;
349 else mcPoints = grid_MC->getSize()/vol_cell;
353 sgpp::base::OperationQuadratureMC opMC(*grid_MC,mcPoints);
355 double res = opMC.doQuadratureFunc(FunctionClass::evaluationWrapper, &function);
359 res *= function.detJPrime();
365double StencilSGpp<F>::integration_MC_fixedCell2(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ) {
368 FunctionClass function(cell,dirP,dirQ,varCoeff);
372 mcPoints = grid_MC->getSize();
373 sgpp::base::OperationQuadratureMC opMC(*grid_MC,mcPoints);
375 double res = opMC.doQuadratureFunc(FunctionClass::evaluationWrapper, &function);
379 res *= function.detJPrime();
387double StencilSGpp<F>::integration_MC(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ) {
390 FunctionClass function(cell,dirP,dirQ,varCoeff);
392 int levels[DimensionSparseGrid];
394 for(
int d=0; d<DimensionSparseGrid;d++){
395 levels[d]=grid.getMaxDepth(d)-(cell.getDepth(d));
396 maxlevel<levels[d]?maxlevel=levels[d]:maxlevel;
403 int dim = DimensionSparseGrid;
404 std::unique_ptr<sgpp::base::Grid> grid_sgpp(sgpp::base::Grid::createLinearGrid(dim));
405 sgpp::base::GridStorage& gridStorage = grid_sgpp->getStorage();
408 grid_sgpp->getGenerator().regular(1);
411 sgpp::base::DataVector alpha(gridStorage.getSize());
412 sgpp::base::DataVector refined(gridStorage.getSize());
415 std::vector<size_t> addedPoints;
418 for (
int step = 0; step < maxlevel; step++) {
421 for (
size_t i = 0; i < gridStorage.getSize(); i++) {
422 sgpp::base::GridPoint& gp = gridStorage.getPoint(i);
427 for(
size_t k=0; k< DimensionSparseGrid; k++){
428 if(gp.getLevel(k)> levels[k]){
438 if(refined[i]>1.0) alpha[i]=0.0;
453 sgpp::base::SurplusRefinementFunctor functor(alpha,alpha_c, 0.5);
454 grid_sgpp->getGenerator().refine(functor, &addedPoints);
457 alpha.resize(gridStorage.getSize());
458 refined.resize(gridStorage.getSize());
460 if(addedPoints.size()<1)
break;
483 sgpp::base::OperationQuadratureMC opMC(*grid_sgpp,gridStorage.getSize());
491 double res = opMC.doQuadratureFunc(FunctionClass::evaluationWrapper, &function);
495 res *= function.detJPrime();
502StencilSGpp<F>::integration_gausslegendre(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ) {
505 FunctionClass function(cell,dirP,dirQ,varCoeff);
507 int levels[DimensionSparseGrid];
509 for(
int d=0; d<DimensionSparseGrid;d++){
510 levels[d]=grid.getMaxDepth(d)-(cell.getDepth(d));
511 maxlevel<levels[d]?maxlevel=levels[d]:maxlevel;
518 int dim = DimensionSparseGrid;
519 std::unique_ptr<sgpp::base::Grid> grid_sgpp(sgpp::base::Grid::createLinearGrid(dim));
520 sgpp::base::GridStorage& gridStorage = grid_sgpp->getStorage();
523 grid_sgpp->getGenerator().regular(1);
526 sgpp::base::DataVector alpha(gridStorage.getSize());
527 sgpp::base::DataVector refined(gridStorage.getSize());
530 std::vector<size_t> addedPoints;
533 for (
int step = 0; step < maxlevel; step++) {
536 for (
size_t i = 0; i < gridStorage.getSize(); i++) {
537 sgpp::base::GridPoint& gp = gridStorage.getPoint(i);
542 for(
size_t k=0; k< DimensionSparseGrid; k++){
543 if(gp.getLevel(k)> levels[k]){
553 if(refined[i]>1.0) alpha[i]=0.0;
568 sgpp::base::SurplusRefinementFunctor functor(alpha,alpha_c, 0.5);
569 grid_sgpp->getGenerator().refine(functor, &addedPoints);
572 alpha.resize(gridStorage.getSize());
573 refined.resize(gridStorage.getSize());
575 if(addedPoints.size()<1)
break;
580 double p[DimensionSparseGrid];
581 for (
size_t i = 0; i < gridStorage.getSize(); i++) {
582 sgpp::base::GridPoint& gp = gridStorage.getPoint(i);
583 for(
int j=0; j<DimensionSparseGrid;j++){
584 p[j] = gp.getStandardCoordinate(j);
586 alpha[i] = function.evaluation(DimensionSparseGrid, p, NULL);
594 res*= function.detJPrime();
600double StencilSGpp<F>::integration_adaptive(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ) {
603 FunctionClass function(cell,dirP,dirQ,varCoeff);
605 int levels[DimensionSparseGrid];
607 for(
int d=0; d<DimensionSparseGrid;d++){
608 levels[d]=grid.getMaxDepth(d)-(cell.getDepth(d));
609 maxlevel<levels[d]?maxlevel=levels[d]:maxlevel;
616 int dim = DimensionSparseGrid;
617 std::unique_ptr<sgpp::base::Grid> grid_sgpp(sgpp::base::Grid::createLinearGrid(dim));
618 sgpp::base::GridStorage& gridStorage = grid_sgpp->getStorage();
621 grid_sgpp->getGenerator().regular(1);
624 sgpp::base::DataVector alpha(gridStorage.getSize());
625 sgpp::base::DataVector refined(gridStorage.getSize());
628 std::vector<size_t> addedPoints;
631 for (
int step = 0; step < maxlevel; step++) {
634 for (
size_t i = 0; i < gridStorage.getSize(); i++) {
635 sgpp::base::GridPoint& gp = gridStorage.getPoint(i);
640 for(
size_t k=0; k< DimensionSparseGrid; k++){
641 if(gp.getLevel(k)> levels[k]){
651 if(refined[i]>1.0) alpha[i]=0.0;
666 sgpp::base::SurplusRefinementFunctor functor(alpha,alpha_c, 0.5);
667 grid_sgpp->getGenerator().refine(functor, &addedPoints);
670 alpha.resize(gridStorage.getSize());
671 refined.resize(gridStorage.getSize());
673 if(addedPoints.size()<1)
break;
678 double p[DimensionSparseGrid];
679 for (
size_t i = 0; i < gridStorage.getSize(); i++) {
680 sgpp::base::GridPoint& gp = gridStorage.getPoint(i);
681 for(
int j=0; j<DimensionSparseGrid;j++){
682 p[j] = gp.getStandardCoordinate(j);
684 alpha[i] = function.evaluation(DimensionSparseGrid, p, NULL);
687 if(numberofgridpoints<gridStorage.getSize()){
688 numberofgridpoints=gridStorage.getSize();
689 cellDimension = cell;
700 std::unique_ptr<sgpp::base::OperationHierarchisation>(sgpp::op_factory::createOperationHierarchisation(*grid_sgpp))
701 ->doHierarchisation(alpha);
704 std::unique_ptr<sgpp::base::OperationQuadrature> opQ(
705 sgpp::op_factory::createOperationQuadrature(*grid_sgpp));
706 double res = opQ->doQuadrature(alpha);
708 res *= function.detJPrime();
715StencilSGpp<F>::integrationLocallyAdaptive(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ) {
718 FunctionClass function(cell,dirP,dirQ,varCoeff);
721 int dim = DimensionSparseGrid;
722 std::unique_ptr<sgpp::base::Grid> grid_sgpp(sgpp::base::Grid::createLinearBoundaryGrid(dim));
723 sgpp::base::GridStorage& gridStorage = grid_sgpp->getStorage();
726 grid_sgpp->getGenerator().regular(level);
729 sgpp::base::DataVector alpha(gridStorage.getSize());
730 sgpp::base::DataVector refined(gridStorage.getSize());
732 double p[DimensionSparseGrid];
733 sgpp::base::DataVector funEvals(gridStorage.getSize());
734 for (
size_t i = 0; i < gridStorage.getSize(); i++) {
735 sgpp::base::GridPoint& gp = gridStorage.getPoint(i);
736 for(
size_t k=0; k<DimensionSparseGrid; k++){
737 p[k]=gp.getStandardCoordinate(k);
739 funEvals[i] = function.evaluation(DimensionSparseGrid,p,NULL);
744 std::vector<size_t> addedPoints;
746 alpha.copyFrom(funEvals);
747 sgpp::op_factory::createOperationHierarchisation(*grid_sgpp)->doHierarchisation(alpha);
753 int number_refinements=10;
754 for (
int step = 0; step < number_refinements-1; step++) {
758 for (
size_t i = 0; i < gridStorage.getSize(); i++) {
759 sgpp::base::GridPoint& gp = gridStorage.getPoint(i);
761 for(
size_t k=0; k< DimensionSparseGrid; k++){
762 if(gp.getLevel(k)>= grid.getMaxDepth(
int(k))-cell.getDepth(
int(k))){
770 if(refined[i]>1.0) alpha[i]=0.0;
784 cout <<
" alpha > 0 " << alpha_c << endl;
788 sgpp::base::SurplusRefinementFunctor functor(alpha,alpha_c, 0.0);
789 grid_sgpp->getGenerator().refine(functor, &addedPoints);
791 cout <<
" added points " << addedPoints.size() << endl;
799 alpha.resize(gridStorage.getSize());
800 funEvals.resize(gridStorage.getSize());
801 refined.resize(gridStorage.getSize());
804 for (
size_t i = 0; i < addedPoints.size(); i++) {
805 size_t seq = addedPoints[i];
806 sgpp::base::GridPoint& gp = gridStorage.getPoint(seq);
807 for(
size_t k=0; k<DimensionSparseGrid; k++){
808 p[k]=gp.getStandardCoordinate(k);
810 funEvals[seq] = function.evaluation(DimensionSparseGrid,p,NULL);
815 alpha.copyFrom(funEvals);
816 sgpp::op_factory::createOperationHierarchisation(*grid_sgpp)->doHierarchisation(alpha);
820 for(
size_t i=0; i<gridStorage.getSize(); i++){
821 sgpp::base::GridPoint& gp = gridStorage.getPoint(i);
822 for(
int d=0; d<DimensionSparseGrid;d++){
829 cout <<
"max " << max << endl;
835 if(addedPoints.size()<1)
break;
840 cout <<
" final points " << gridStorage.getSize() << endl;
846 std::unique_ptr<sgpp::base::OperationQuadrature> opQ(
847 sgpp::op_factory::createOperationQuadrature(*grid_sgpp));
848 double res = opQ->doQuadrature(alpha);
850 res *= function.detJPrime();
856double StencilSGpp<F>::integration_regular(CellDimension &cell, CellIndexDirection &dirP, CellIndexDirection &dirQ) {
859 FunctionClass function(cell,dirP,dirQ,varCoeff);
861 int dim = DimensionSparseGrid;
862 std::unique_ptr<sgpp::base::Grid> grid_sgpp(sgpp::base::Grid::createLinearBoundaryGrid(dim));
863 sgpp::base::GridStorage& gridStorage = grid_sgpp->getStorage();
867 grid_sgpp->getGenerator().regular(level);
872 sgpp::base::DataVector alpha(gridStorage.getSize());
873 double p[DimensionSparseGrid];
874 for (
size_t i = 0; i < gridStorage.getSize(); i++) {
875 sgpp::base::GridPoint& gp = gridStorage.getPoint(i);
876 for(
int j=0; j<DimensionSparseGrid;j++){
877 p[j] = gp.getStandardCoordinate(j);
879 alpha[i] = function.evaluation(DimensionSparseGrid, p, NULL);
882 std::unique_ptr<sgpp::base::OperationHierarchisation>(sgpp::op_factory::createOperationHierarchisation(*grid_sgpp))
883 ->doHierarchisation(alpha);
886 std::unique_ptr<sgpp::base::OperationQuadrature> opQ(
887 sgpp::op_factory::createOperationQuadrature(*grid_sgpp));
888 double res = opQ->doQuadrature(alpha);
890 res *= function.detJPrime();
Definition sparseGrid.h:277