Skip to content

Commit

Permalink
fish the binary search of the hammer mass, oopsgit add .
Browse files Browse the repository at this point in the history
q
.
  • Loading branch information
Rust404 committed Jul 23, 2020
1 parent 4f01b30 commit 104182b
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 34 deletions.
19 changes: 10 additions & 9 deletions core.cpp
Expand Up @@ -18,7 +18,7 @@
/* -------------------------------------------------------------------------------- */
/* ---------------------------- PREVIOUS & NEXT ---------------------------- */
/* -------------------------------------------------------------------------------- */
void nextDude(Dude& preDude,Dude& currentDude, uint32_t index, double depth)
void nextDude(Dude& preDude,Dude& currentDude, uint32_t index, double depth,double hammerMass)
{
/* the dummy dude, contain no data */
if (index == 0){
Expand Down Expand Up @@ -84,8 +84,9 @@ void nextDude(Dude& preDude,Dude& currentDude, uint32_t index, double depth)
double tractionOne=preDude._tractionTwo;

// 1) ball
double gravityBall=HAMMER_MASS*G;
double ballVolumn=HAMMER_MASS/HAMMER_DENSITY;
// CAUTION HERE!!!! THE HAMMER_MASS IS IMPORTANT!
double gravityBall=hammerMass*G;
double ballVolumn=hammerMass/HAMMER_DENSITY;
double floatageBall=ballVolumn*SEA_WATER_DENSITY*G;

// 2) cylinder
Expand Down Expand Up @@ -163,7 +164,7 @@ void nextDude(Dude& preDude,Dude& currentDude, uint32_t index, double depth)
/* -------------------------------------------------------------------------------- */


void OneTry(std::vector<Dude>& dudes, double depth)
void OneTry(std::vector<Dude>& dudes, double depth,double hammerMass)
{
if(dudes.size()!=0){
dudes.clear();
Expand All @@ -174,28 +175,28 @@ void OneTry(std::vector<Dude>& dudes, double depth)
// 1) dummy dude
Dude nullDude;
Dude dummyDude;
nextDude(nullDude,dummyDude,count,depth);
nextDude(nullDude,dummyDude,count,depth,hammerMass);
dudes.push_back(dummyDude);
++count;


// 2) buoy
Dude buoyDude;
nextDude(dudes.back(),buoyDude,count,depth);
nextDude(dudes.back(),buoyDude,count,depth,hammerMass);
dudes.push_back(buoyDude);
++count;

// 3) tube
while(count<=5){
Dude currentTube;
nextDude(dudes.back(),currentTube,count,depth);
nextDude(dudes.back(),currentTube,count,depth,hammerMass);
dudes.push_back(currentTube);
++count;
}

// 4) cylinder
Dude cylinderDude;
nextDude(dudes.back(),cylinderDude,count,depth);
nextDude(dudes.back(),cylinderDude,count,depth,hammerMass);
dudes.push_back(cylinderDude);
++count;

Expand All @@ -204,7 +205,7 @@ void OneTry(std::vector<Dude>& dudes, double depth)
bool reachBottom=false;
while(count<=TOTAL_COUNT+6){
Dude currentChain;
nextDude(dudes.back(),currentChain,count,depth);
nextDude(dudes.back(),currentChain,count,depth,hammerMass);

//check whether the chain get the bottom
if(currentChain._thetaTwo<=0){
Expand Down
5 changes: 2 additions & 3 deletions core.h
Expand Up @@ -4,13 +4,12 @@
#include "dude.h"
#include "indicator.h"

void nextDude(Dude& preDude,Dude& currentDude, uint32_t index, double depth);
void nextDude(Dude& preDude,Dude& currentDude, uint32_t index, double depth, double hammerMass);

void OneTry(std::vector<Dude>& dudes, double depth);
void OneTry(std::vector<Dude>& dudes, double depth,double hammerMass);

void oneValidation(std::vector<Dude>& dudes, Indicators& indicators);

std::vector<std::pair<double,double>> getChainShape(std::vector<Dude>& dudes);


void saveTheChainShapeToFile(std::vector<std::pair<double,double> >& points);
20 changes: 20 additions & 0 deletions indicator.cpp
Expand Up @@ -11,9 +11,29 @@ void Indicators::display() {
std::cout<<"height: "<< this->_height<<std::endl;
std::cout<<"distance: "<< this->_distance<<std::endl;
std::cout<<"depth: "<< this->_depth<<std::endl;
std::cout<<std::endl;
}


void Indicators::init() {
this-> _phiCylinder=0;
this-> _phiLastChain=0;
this-> _height=0;
this-> _distance=0;
this-> _depth=0;
}

bool Indicators::validate(){
if(
((PHI/2-this->_phiCylinder*PHI/180)<=MAX_PHI_CYLINDER)
&&
(this->_phiLastChain*PHI/180<=MAX_PHI_LASTCHAIN)
){
return true;
}
return false;
}




Expand Down
1 change: 1 addition & 0 deletions indicator.h
Expand Up @@ -26,6 +26,7 @@ struct Indicators
}

void display();
void init();
bool validate();
};

Expand Down
84 changes: 66 additions & 18 deletions main.cpp
Expand Up @@ -6,49 +6,97 @@
#include "dude.h"
#include "indicator.h"

void seachTheDepth(double depth,std::vector<Dude>& dudes,Indicators& indicator){
OneTry(dudes,depth);
// give a immersion depth and get the shape of the whole system
void seachTheDepth(
double depth,
std::vector<Dude>& dudes,
Indicators& indicator,
double hammerMass
){
OneTry(dudes,depth,hammerMass);
oneValidation(dudes,indicator);
}

void binarySearch(double targetHeight,double bias,double left,double right)
{
// give the targetHeight and the bias, get the suitable immersion depth
void binarySearchImmersionDepth(
double targetHeight,
double bias,
double left,
double right,
double hammerMass,
Indicators& indicator
){
double leftHeight=targetHeight*(1-bias);
double rightHeight=targetHeight*(1+bias);

std::vector<Dude> dudes;
Indicators indicator;

while(indicator._height<leftHeight||indicator._height>rightHeight){
double mid=left+(right-left)/2;
seachTheDepth(mid,dudes,indicator);
seachTheDepth(mid,dudes,indicator,hammerMass);
if(indicator._height>=rightHeight){
right=mid;
}else if(indicator._height<=leftHeight){
left=mid;
}
}

indicator.display();

return ;
}

// check the status, judge valid or not
bool isOK(Indicators& indicator){
if(
((PHI/2-indicator._phiCylinder*PHI/180)<=MAX_PHI_CYLINDER)
&&
(indicator._phiLastChain*PHI/180<=MAX_PHI_LASTCHAIN)
){
return true;
}
return false;
}

// get the minimum mass of the hammer ball
// calculate the region of the hammer mass first
double binarySearchHammerMass(
double leftMass,
double rightMass,
double hammerMassBias,
Indicators& indicator
){
if((rightMass-leftMass)<=DEFUALT_HAMMER_MASS_BIAS){
binarySearchImmersionDepth(18.0f,DEFUALT_BIAS,0.5,1.8,leftMass,indicator);
return leftMass;
}
double midMass;
while((rightMass-leftMass)>=hammerMassBias){
midMass=leftMass+(rightMass-leftMass)/2;

indicator.init();
binarySearchImmersionDepth(18.0f,DEFUALT_BIAS,0.5,1.8,midMass,indicator);
//indicator.display();
if(indicator.validate()){
rightMass=midMass;
}else{
leftMass=midMass;
}
}
return midMass;
}



int main()
{
/* std::vector<Dude> dudes;
//OneTry(dudes,0.7348f);
Indicators currentIndicator;

Indicators indicator; */
//seachTheDepth(0.7348f,dudes,indicator);
binarySearch(18.0f,0.00005,0.5,1.5);

//oneValidation(dudes,indicator);
/* indicator.display();
double minMass=binarySearchHammerMass(1200,3000,DEFUALT_HAMMER_MASS_BIAS,currentIndicator);
std::cout<<"the minimum hammer mass: "<<minMass<<std::endl;
currentIndicator.display();

auto points=getChainShape(dudes);
/* Indicators indi;
binarySearchImmersionDepth(18.0f,DEFUALT_BIAS,0.5,1.5,2100,indi);
indi.display(); */

saveTheChainShapeToFile(points); */
return 0;
}
10 changes: 6 additions & 4 deletions para.h
Expand Up @@ -36,22 +36,24 @@


// nature
#define WIND_SPEED 12 /* m */
#define WIND_SPEED 36 /* m */
#define WATER_SPEED 1.5 /* m */
#define SEA_DEPTH 18 /* m */
#define SEA_WATER_DENSITY 1025 /* kg/m3 */
#define G 9.8 /* m/s2 */
#define PHI 3.1415926

// constrain
#define MAX_PHI_CYLINDER (5.0/180*PHI) /* radian */
#define MAX_PHI_LASTCHAIN (16.0/180*PHI) /* radian */



// precise level
#define DEFUALT_BIAS 0.0005 /* 0-1 */
#define DEFUALT_HAMMER_MASS_BIAS 5 /* kg */

// config
#define CHAIN_SHAPE_FILE "./chainShape.txt"




#endif //PARA_H
Binary file modified test
Binary file not shown.

0 comments on commit 104182b

Please sign in to comment.