38 for (std::vector<int>::iterator it =
CellN.begin() ; it !=
CellN.end(); ++it)
54 for (
unsigned int i=0;i<
CellN.size();i++)
56 std::cout<<
"From"<<std::setw(8)<<
cell2Min(i)<<
" to"<<std::setw(8)<<
cell2Max(i)<<
" number="<<std::setw(5)<<
CellN[i]<<std::endl;
101 if(startCell<0||startCell>=
NCells)
103 std::cout<<
"Error in startCell (startCell="<<startCell<<
" start="<<start<<
" rMin="<<
rMin<<
" rMax="<<
rMax<<
" NCells="<<
NCells<<std::endl;
105 if(endCell<0||endCell>=
NCells)
107 std::cout<<
"Error in endCell (endCell="<<endCell<<
" end="<<end<<
" rMin="<<
rMin<<
" rMax="<<
rMax<<
" NCells="<<
NCells<<std::endl;
111 if(startCell==endCell)
113 teller=
CellN[startCell]/(p+1)*(pow(end,p+1)-pow(start,p+1));
115 else if(endCell<startCell)
121 teller=
CellN[startCell]/(p+1)*(pow(
cell2Max(startCell),p+1)-pow(start,p+1));
122 for(
int i=startCell+1;i<endCell;i++)
126 teller+=
CellN[endCell]/(p+1)*(pow(end,p+1)-pow(
cell2Min(endCell),p+1));
133 return teller/noemer;
145 return CellN[xCell]*pow(x,p)/noemer;
159 if(startCell==endCell)
161 teller=
CellN[startCell]*h/2/(p+1)*(pow(2/h*end+2,p+1)-pow(2/h*start+2,p+1));
162 noemer=
CellN[startCell]*(end-start);
166 teller=
CellN[startCell]*h/2/(p+1)*(pow(2/h*
cell2Max(startCell)+2,p+1)-pow(2/h*start+2,p+1));
168 for(
int i=startCell+1;i<endCell;i++)
173 teller+=
CellN[endCell]*h/2/(p+1)*(pow(2/h*end+2,p+1)-pow(2/h*
cell2Min(endCell)+2,p+1));
179 return teller/noemer;
189 double diffTeller=-
CellN[startCell]*(pow(2/h*start+2,p));
190 double diffNoemer=-
CellN[startCell];
191 if(startCell==endCell)
193 teller=
CellN[startCell]*h/2/(p+1)*(pow(2/h*end+2,p+1)-pow(2/h*start+2,p+1));
194 noemer=
CellN[startCell]*(end-start);
198 teller=
CellN[startCell]*h/2/(p+1)*(pow(2/h*
cell2Max(startCell)+2,p+1)-pow(2/h*start+2,p+1));
200 for(
int i=startCell+1;i<endCell;i++)
205 teller+=
CellN[endCell]*h/2/(p+1)*(pow(2/h*end+2,p+1)-pow(2/h*
cell2Min(endCell)+2,p+1));
211 return (diffTeller*noemer-teller*diffNoemer)/(noemer*noemer);
221 double diffTeller=
CellN[endCell]*(pow(2/h*end+2,p));
222 double diffNoemer=
CellN[endCell];
223 if(startCell==endCell)
225 teller=
CellN[startCell]*h/2/(p+1)*(pow(2/h*end+2,p+1)-pow(2/h*start+2,p+1));
226 noemer=
CellN[startCell]*(end-start);
230 teller=
CellN[startCell]*h/2/(p+1)*(pow(2/h*
cell2Max(startCell)+2,p+1)-pow(2/h*start+2,p+1));
232 for(
int i=startCell+1;i<endCell;i++)
237 teller+=
CellN[endCell]*h/2/(p+1)*(pow(2/h*end+2,p+1)-pow(2/h*
cell2Min(endCell)+2,p+1));
243 return (diffTeller*noemer-teller*diffNoemer)/(noemer*noemer);
254 if(startCell==endCell)
256 teller=
CellN[startCell]*h/2/(p+1)*(pow(2/h*end+2,p+1)-pow(2/h*start+2,p+1));
257 diffTeller=
CellN[startCell]/2/(p+1)*(pow(2/h*end+2,p+1)-pow(2/h*start+2,p+1))-
CellN[startCell]/h*(end*pow(2/h*end+2,p)-start*pow(2/h*start+2,p));
258 noemer=
CellN[startCell]*(end-start);
262 teller =0.5*
CellN[startCell]*h/(p+1)*(pow(2/h*
cell2Max(startCell)+2,p+1)-pow(2/h*start+2,p+1));
263 diffTeller=0.5*
CellN[startCell] /(p+1)*(pow(2/h*
cell2Max(startCell)+2,p+1)-pow(2/h*start+2,p+1))-
CellN[startCell]/h*(
cell2Max(startCell)*pow(2/h*
cell2Max(startCell)+2,p)-start*pow(2/h*start+2,p));
265 for(
int i=startCell+1;i<endCell;i++)
271 teller +=0.5*
CellN[endCell]*h/(p+1)*(pow(2/h*end+2,p+1)-pow(2/h*
cell2Min(endCell)+2,p+1));
272 diffTeller+=0.5*
CellN[endCell] /(p+1)*(pow(2/h*end+2,p+1)-pow(2/h*
cell2Min(endCell)+2,p+1))-
CellN[endCell]/h*(end*pow(2/h*end+2,p)-
cell2Min(endCell)*pow(2/h*
cell2Min(endCell)+2,p));
278 return diffTeller/noemer;
283 unsigned int NLevels=hGridCellSizes.size()-1;
284 unsigned int NParams=hGridCellSizes.size();
288 std::cout<<
"Length scale="<<
length<<std::endl;
292 std::cout<<
"Level sizes:";
293 for(
unsigned int i=0;i<NParams;i++)
295 std::cout<<
" "<<hGridCellSizes[i];
297 std::cout<<std::endl;
300 std::vector<double> particlesPerLevel;
301 for(
unsigned int i=0;i<NLevels;i++)
303 particlesPerLevel.push_back(
pdfInt(0.5*hGridCellSizes[i],0.5*hGridCellSizes[i+1],0));
304 if(particlesPerLevel.back()<0)
306 particlesPerLevel.back()=0;
311 std::cout<<
"Particles per level: ";
312 for(
unsigned int i=0;i<NLevels;i++)
314 std::cout<<
" "<<particlesPerLevel[i];
316 std::cout<<std::endl;
320 std::vector< std::vector< double > > diffParticlesPerLevel;
321 diffParticlesPerLevel.resize(NLevels);
322 for(
unsigned int i=0;i<NLevels;i++)
324 for(
unsigned int j=0;j<NParams;j++)
326 diffParticlesPerLevel[i].push_back(0.0);
329 diffParticlesPerLevel[i][j]+=-0.5*
diffPdfInt(0.5*hGridCellSizes[i],0);
333 diffParticlesPerLevel[i][j]+= 0.5*
diffPdfInt(0.5*hGridCellSizes[i+1],0);
339 std::cout<<
"Diff Particles per level: "<<std::endl;
340 for(
unsigned int i=0;i<NLevels;i++)
342 for(
unsigned int j=0;j<NParams;j++)
344 std::cout<<
" "<<std::setw(12)<<diffParticlesPerLevel[i][j];
346 std::cout<<std::endl;
350 std::vector<double> cellsPerLevel;
351 for(
unsigned int i=0;i<NLevels;i++)
357 std::cout<<
"Cells per level: ";
358 for(
unsigned int i=0;i<NLevels;i++)
360 std::cout<<
" "<<cellsPerLevel[i];
362 std::cout<<std::endl;
366 std::vector< std::vector< double > > diffCellsPerLevel;
367 diffCellsPerLevel.resize(hGridCellSizes.size());
368 for(
unsigned int i=0;i<NLevels;i++)
370 for(
unsigned int j=0;j<NParams;j++)
378 diffCellsPerLevel[i].push_back(0.0);
384 std::cout<<
"Diff Cells per level: "<<std::endl;
385 for(
unsigned int i=0;i<NLevels;i++)
387 for(
unsigned int j=0;j<NParams;j++)
389 std::cout<<
" "<<std::setw(12)<<diffCellsPerLevel[i][j];
391 std::cout<<std::endl;
395 std::vector<double> particlesPerCell;
396 for(
unsigned int i=0;i<NLevels;i++)
398 particlesPerCell.push_back(particlesPerLevel[i]/cellsPerLevel[i]);
402 std::cout<<
"Particles per cell: ";
403 for(
unsigned int i=0;i<particlesPerCell.size();i++)
405 std::cout<<
" "<<particlesPerCell[i];
407 std::cout<<std::endl;
411 std::vector< std::vector< double > > diffParticlesPerCell;
412 diffParticlesPerCell.resize(NLevels);
413 for(
unsigned int i=0;i<NLevels;i++)
415 for(
unsigned int j=0;j<NParams;j++)
417 diffParticlesPerCell[i].push_back((diffParticlesPerLevel[i][j]*cellsPerLevel[i]-particlesPerLevel[i]*diffCellsPerLevel[i][j])/(cellsPerLevel[i]*cellsPerLevel[i]));
422 std::cout<<
"Diff Particles per Cell: "<<std::endl;
423 for(
unsigned int i=0;i<NLevels;i++)
425 for(
unsigned int j=0;j<NParams;j++)
427 std::cout<<
" "<<std::setw(12)<<diffParticlesPerCell[i][j];
429 std::cout<<std::endl;
433 double contactWork=0, overheadWork=0;
434 std::vector<double> diffContactWork;
435 std::vector<double> diffOverheadWork;
436 diffContactWork.resize(NParams);
437 diffOverheadWork.resize(NParams);
438 for(
unsigned int j=0;j<NParams;j++)
440 diffContactWork[j]=0;
441 diffOverheadWork[j]=0;
444 for(
unsigned int i=0;i<NLevels;i++)
446 contactWork+=0.5*pow(3,
dimension)*particlesPerCell[i]*particlesPerLevel[i];
447 overheadWork+=0.5*(pow(3,
dimension)+1)*particlesPerLevel[i];
448 for(
unsigned int j=0;j<NParams;j++)
450 diffContactWork[j]+=0.5*pow(3,
dimension)*(diffParticlesPerCell[i][j]*particlesPerLevel[i]+particlesPerCell[i]*diffParticlesPerLevel[i][j]);
451 diffOverheadWork[j]+=0.5*(pow(3,
dimension)+1)*diffParticlesPerLevel[i][j];
470 for(
unsigned int j=startJ;j<endJ;j++)
472 double numberOfCellToCheck;
475 std::vector<double> diffNumberOfCellToCheck;
476 for(
unsigned int k=0;
k<NParams;
k++)
478 diffNumberOfCellToCheck.push_back(0.0);
492 contactWork+=particlesPerLevel[i]*numberOfCellToCheck*particlesPerCell[j];
493 overheadWork+=particlesPerLevel[i]*numberOfCellToCheck;
495 for(
unsigned int k=0;
k<NParams;
k++)
497 diffContactWork[
k]+=(diffParticlesPerLevel[i][
k]*numberOfCellToCheck*particlesPerCell[j]+particlesPerLevel[i]*diffNumberOfCellToCheck[
k]*particlesPerCell[j]+particlesPerLevel[i]*numberOfCellToCheck*diffParticlesPerCell[j][
k]);
498 diffOverheadWork[
k]+=(diffParticlesPerLevel[i][
k]*numberOfCellToCheck+particlesPerLevel[i]*diffNumberOfCellToCheck[
k]);
504 std::cout<<
"Contact work: "<<contactWork<<std::endl;
505 std::cout<<
"Overhead work: "<<
k*overheadWork<<std::endl;
506 std::cout<<
"Sum work: "<<contactWork+
k*overheadWork<<std::endl;
507 std::cout<<
"diff Contactwork: ";
508 for(
unsigned int j=0;j<NParams;j++)
510 std::cout<<
" "<<diffContactWork[j];
512 std::cout<<std::endl;
513 std::cout<<
"diff Overheadwork: ";
514 for(
unsigned int j=0;j<NParams;j++)
516 std::cout<<
" "<<
k*diffOverheadWork[j];
518 std::cout<<std::endl;
519 std::cout<<
"diff sum: ";
520 for(
unsigned int j=0;j<NParams;j++)
522 std::cout<<
" "<<diffContactWork[j]+
k*diffOverheadWork[j];
524 std::cout<<std::endl;
527 for(
unsigned int j=0;j<NParams;j++)
529 dfdx.push_back(diffContactWork[j]+
k*diffOverheadWork[j]);
535 unsigned int NLevels=hGridCellSizes.size()-1;
536 unsigned int NParams=hGridCellSizes.size();
540 std::cout<<
"Length scale="<<
length<<std::endl;
544 std::cout<<
"Level sizes:";
545 for(
unsigned int i=0;i<NParams;i++)
547 std::cout<<
" "<<hGridCellSizes[i];
549 std::cout<<std::endl;
552 std::vector<double> particlesPerLevel;
553 for(
unsigned int i=0;i<NLevels;i++)
555 particlesPerLevel.push_back(
pdfInt(0.5*hGridCellSizes[i],0.5*hGridCellSizes[i+1],0));
556 if(particlesPerLevel.back()<0)
558 particlesPerLevel.back()=0;
563 std::cout<<
"Particles per level: ";
564 for(
unsigned int i=0;i<NLevels;i++)
566 std::cout<<
" "<<particlesPerLevel[i];
568 std::cout<<std::endl;
571 std::vector<double> cellsPerLevel;
572 for(
unsigned int i=0;i<NLevels;i++)
578 std::cout<<
"Cells per level: ";
579 for(
unsigned int i=0;i<NLevels;i++)
581 std::cout<<
" "<<cellsPerLevel[i];
583 std::cout<<std::endl;
586 std::vector<double> particlesPerCell;
587 for(
unsigned int i=0;i<NLevels;i++)
589 particlesPerCell.push_back(particlesPerLevel[i]/cellsPerLevel[i]);
593 std::cout<<
"Particles per cell: ";
594 for(
unsigned int i=0;i<particlesPerCell.size();i++)
596 std::cout<<
" "<<particlesPerCell[i];
598 std::cout<<std::endl;
601 double contactWork=0, overheadWork=0;
602 for(
unsigned int i=0;i<NLevels;i++)
604 contactWork+=0.5*pow(3,
dimension)*particlesPerCell[i]*particlesPerLevel[i];
605 overheadWork+=0.5*(pow(3,
dimension)+1)*particlesPerLevel[i];
623 for(
unsigned int j=startJ;j<endJ;j++)
625 double numberOfCellToCheck;
627 contactWork+=particlesPerLevel[i]*numberOfCellToCheck*particlesPerCell[j];
628 overheadWork+=particlesPerLevel[i]*numberOfCellToCheck;
635 std::cout<<
"Contact work: "<<contactWork<<std::endl;
636 std::cout<<
"Overhead work: "<<
k*overheadWork<<std::endl;
638 return contactWork+
k*overheadWork;
645 std::cout<<
"calcDfDx"<<std::endl;
651 for(
unsigned int i=0;i<hGridCellSizes.size();i++)
653 hGridCellSizes[i]+=dx;
655 dfdx.push_back((nW-W)/dx);
656 hGridCellSizes[i]-=dx;
659 std::cout<<
"i="<<i<<
" Value="<<hGridCellSizes[i]<<
" Change="<<nW-W<<
" dfdx="<<dfdx.back()<<std::endl;
668 std::cout<<
"checkLimits part 1 remove levels"<<std::endl;
673 for(
unsigned int i=1;i<hGridCellSizes.size();i++)
675 if(std::abs(hGridCellSizes[i-1]-hGridCellSizes[i])<1e-6)
677 if(dfdx[i-1]<dfdx[i])
681 std::cout<<
"Remove level"<<i<<std::endl;
683 if(i>0.5*hGridCellSizes.size())
685 hGridCellSizes.erase(hGridCellSizes.begin()+i-1);
686 dfdx.erase(dfdx.begin()+i-1);
690 hGridCellSizes.erase(hGridCellSizes.begin()+i);
691 dfdx.erase(dfdx.begin()+i);
700 std::cout<<
"checkLimits part 2 calculate limit"<<std::endl;
701 for(
unsigned int i=0;i<hGridCellSizes.size();i++)
703 std::cout<<
"i="<<i<<
" Value="<<hGridCellSizes[i]<<
" dfdx="<<dfdx[i]<<std::endl;
706 double maxStepSize=-std::numeric_limits<double>::max();
708 for(
unsigned int i=1;i<hGridCellSizes.size();i++)
710 nmax=(hGridCellSizes[i-1]-hGridCellSizes[i])/(dfdx[i]-dfdx[i-1]);
713 std::cout<<
"Max f="<<nmax<<
" because otherwise "<<hGridCellSizes[i-1]<<
"+x*"<<dfdx[i-1]<<
">"<<hGridCellSizes[i]<<
"+x*"<<dfdx[i]<<std::endl;
717 maxStepSize=std::max(nmax,maxStepSize);
722 std::cout<<
"maxStepSize="<<maxStepSize<<std::endl;
731 std::cout<<
"Apply step:"<<std::endl;
733 for(
unsigned int i=0;i<hGridCellSizes.size()-1;i++)
735 hGridCellSizes[i]+=stepsize*dfdx[i];
738 std::cout<<
"hGridCellSizes[i]"<<
"+"<<stepsize<<
"*"<<dfdx[i]<<
"="<<hGridCellSizes[i]<<std::endl;
745 std::vector<double> curHGridCellSizes=startHGridCellSizes;
746 std::vector<double> xHGridCellSizes=startHGridCellSizes;
748 applyStep(curHGridCellSizes, searchDirection, cur, verbosity-1);
749 double curWork=
calculateWork(curHGridCellSizes, method, verbosity-1);
752 double resphi=2.0-0.5*(1+std::sqrt(5));
758 x=cur+resphi*(max-cur);
763 x=cur-resphi*(cur-min);
766 if (std::abs(max-min)<1e-7*(std::abs(cur)+std::abs(x)))
768 return 0.5*(min + max);
771 applyStep(xHGridCellSizes, searchDirection, x, 0);
775 std::cout<<
"min="<<min<<
" max="<<max<<
" cur="<<cur<<
" curWork="<<curWork<<
" x="<<x<<
" xWork="<<xWork<<std::endl;
781 return goldenSectionSearch(startHGridCellSizes, searchDirection, cur, x, max, method, verbosity);
785 return goldenSectionSearch(startHGridCellSizes, searchDirection, min, x, cur, method, verbosity);
792 return goldenSectionSearch(startHGridCellSizes, searchDirection, min, cur, x, method, verbosity);
796 return goldenSectionSearch(startHGridCellSizes, searchDirection, x, cur, max, method, verbosity);
803 hGridCellSizes.clear();
804 for (
unsigned int i=0; i<=numberOfLevels; i++)
806 hGridCellSizes.push_back(2.0*(
rMin+(
rMax-
rMin)*i/numberOfLevels));
809 std::vector<double> dfdx;
810 double step,max,nW,W;
812 for(
int i=0;i<100;i++)
815 max=
checkLimit(hGridCellSizes,dfdx,verbosity-3);
817 applyStep(hGridCellSizes,dfdx,step,verbosity-3);
821 std::cout<<
"Correct step: old work="<<W<<
" new work="<<nW<<
" difference="<<nW-W<<
" step="<<step/max<<std::endl;
828 std::cout<<
"Work="<<nW<<std::endl;
829 std::cout<<
"Optimal cell sizes:";
830 for(
unsigned int i=0;i<hGridCellSizes.size();i++)
832 std::cout<<
" "<<hGridCellSizes[i];
834 std::cout<<std::endl;
double goldenSectionSearch(std::vector< double > &startHGridCellSizes, std::vector< double > &searchDirection, double min, double cur, double max, HGridMethod method, int verbosity)
BaseParticle * getLargestParticle() const
Gets a pointer to the largest BaseParticle (by interactionRadius) in this ParticleHandler.
Mdouble get_zmin()
Gets zmin.
double diffPdfInt(double x, int power)
diff(int(f(r)*r^power*dr,r=s..e)/int(f(r)*dr,r=0..omega),e)=f(e)*e^power/int(f(r)*dr,r=0..omega)
This is the base class for both HGRID_2D and HGRID_3D.
void Initialise(HGRID_base &problem, int verbosity)
Mdouble get_InteractionRadius() const
double calculateWork(std::vector< double > &hGridCellSizes, HGridMethod method, int verbosity)
double diffStartExpectedCellsIntegral(double start, double end, int p, double h)
const std::vector< T * >::const_iterator begin() const
Gets the begin of the const_iterator over all Object in this BaseHandler.
void getOptimalDistribution(std::vector< double > &hGridCellSizes, int numberOfLevels, HGridMethod method, int verbosity)
int get_dim()
Allows the dimension of the simulation to be accessed.
Mdouble get_ymax()
Gets ymax.
double expectedCellsIntegral(double start, double end, int p, double h)
This function calculates: int((2*r/h+2)^d f(r) dr,r=s..e)/int(f(r) dr,r=s..e)+ Used to calculated the...
void calculateDiffWork(std::vector< double > &hGridCellSizes, std::vector< double > &dfdx, HGridMethod method, int verbosity)
Mdouble get_ymin()
Gets ymin.
void calcDfDx(std::vector< double > &hGridCellSizes, std::vector< double > &dfdx, HGridMethod method, int verbosity)
BaseParticle * getSmallestParticle() const
Gets a pointer to the smallest BaseParticle (by interactionRadius) in this ParticleHandler.
Mdouble get_xmin()
Get xmin.
const std::vector< T * >::const_iterator end() const
Gets the end of the const_iterator over all BaseBoundary in this BaseHandler.
int radius2Cell(double r)
double checkLimit(std::vector< double > &hGridCellSizes, std::vector< double > &dfdx, int verbosity)
unsigned int getNumberOfObjects() const
Gets the number of Object in this BaseHandler.
void applyStep(std::vector< double > &hGridCellSizes, std::vector< double > &dfdx, double stepsize, int verbosity)
ParticleHandler & getParticleHandler()
Mdouble get_xmax()
Get xmax.
double diffHExpectedCellsIntegral(double start, double end, int p, double h)
double pdfInt(double start, double end, int power)
This function calculates: int(f(r)*r^power*dr,r=start..end)/int(f(r)*dr,r=0..omega) ...
Mdouble get_zmax()
Gets zmax.
double diffEndExpectedCellsIntegral(double start, double end, int p, double h)