Skip to content

Commit

Permalink
Messing testcase, but it seems to behave
Browse files Browse the repository at this point in the history
  • Loading branch information
mlxd committed Mar 7, 2017
1 parent d3f464e commit b7aec1d
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 54 deletions.
18 changes: 9 additions & 9 deletions include/vort.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,27 @@ namespace Vtx {
double2 coordsD;
int winding;
bool isOn;
std::size_t timeStep;
std::size_t timeStep;

protected:
std::size_t uid;
int uid;

public:
Vortex();
Vortex(int2 coords, double2 coordsD, int winding, int isOn, std::size_t timeStep);
Vortex(int2 coords, double2 coordsD, int winding, bool isOn, std::size_t timeStep);

~Vortex();

void updateUID(std::size_t uid);
void updateUID(int uid);
void updateWinding(int winding);
void updateIsOn(bool isOn);
void updateCoords(int2 coords);
void updateCoordsD(double2 coordsD);
void updateTimeStep(std::size_t timeStep);

std::size_t getUID();
int getUID();
int getWinding();
bool getIsOn();
bool getIsOn();
int2 getCoords();
double2 getCoordsD();
std::size_t getTimeStep();
Expand Down Expand Up @@ -111,7 +111,7 @@ namespace Vtx {
* @ingroup vtx
* @return shared_ptr<Vortex> Shared pointer to vortex by index
*/
std::shared_ptr<Vortex> getVtx_Uid(std::size_t uid);
std::shared_ptr<Vortex> getVtx_Uid(int uid);

/**
* @brief Returns a shared_ptr to the vortex by an index in VtxList
Expand All @@ -125,14 +125,14 @@ namespace Vtx {
* @ingroup vtx
* @return unsigned int Vortex index for UID
*/
std::size_t getVtxIdx_Uid(std::size_t uid);
std::size_t getVtxIdx_Uid(int uid);

/**
* @brief Returns the largest UID given
* @ingroup vtx
* @return unsigned int Largest vortex UID
*/
std::size_t getMax_Uid();
std::size_t& getMax_Uid();

/**
* @brief Returns index of vortex with shortest coordinate distance from current vortex
Expand Down
117 changes: 95 additions & 22 deletions src/VtxTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int main(){
double2 cD = {3.1,4.2};

std::mt19937 rng(time(0));
std::uniform_real_distribution<> dist(0,1);
std::uniform_real_distribution<> dist(-0.1,0.1);

//VtxList::VtxList()
std::shared_ptr<Vtx::VtxList> vl = std::make_shared<Vtx::VtxList>(11);
Expand All @@ -29,9 +29,9 @@ int main(){
//########################################################################//
// Vortex::Vortex(int2 coords, double2 coordsD, int winding, int isOn, std::size_t timeStep)
//########################################################################//

/*
//Create UID -1 vortex, and check values
std::shared_ptr<Vtx::Vortex> vtx = std::make_shared<Vtx::Vortex>(c,cD, 1, true, 0);
std::shared_ptr<Vtx::Vortex> vtx = std::make_shared<Vtx::Vortex>(c,cD, 1, true, true, 0);
assert( vtx->getUID() == -1 );
assert( vtx->getCoords().x == 1 );
Expand Down Expand Up @@ -61,11 +61,12 @@ int main(){
c.y = i;
cD.x = i+dist(rng);
cD.y = i+dist(rng);
vl->addVtx(std::make_shared<Vtx::Vortex>(c, cD, 1, true, 0));
vl->addVtx(std::make_shared<Vtx::Vortex>(c, cD, 1, true, false, 0));
}
for (int i = 0; i < vl->getVortices().size(); ++i){
assert(vl->getVtx_Idx(i)->getUID() == i);
//std::cout << vl->getVtx_Idx(i)->getUID() << "\n";
assert(vl->getVtx_Idx(i)->getUID() == -1);
}
std::cout << "Passed linear UID check passed"<<std::endl;
Expand All @@ -76,14 +77,14 @@ int main(){
vl->removeVtx(2);
assert(vl->getVortices().size() == 10);
std::cout << "Passed remove UID 2 check passed"<<std::endl;
std::cout << "Passed remove Idx 2 check passed"<<std::endl;
//########################################################################//
//
//########################################################################//
c.x++; c.y++; cD.x += 1.; cD.y += 1.;
vl->addVtx(std::make_shared<Vtx::Vortex>(c,cD, -1, false, 1000));
assert(vl->getVtx_Uid(11)->getIsOn() == false);
vl->addVtx(std::make_shared<Vtx::Vortex>(c,cD, -1, false, false, 1000));
assert(vl->getVtx_Uid(-1)->getIsOn() == true);
assert(vl->getVortices().size() == 11);
std::cout << "Passed add new vortex with uid=suid++ (11), and getVtx_Uid"<<std::endl;
Expand All @@ -92,17 +93,19 @@ int main(){
assert(vl->getVtx_Idx(10)->getIsOn() == false);
assert(vl->getVtx_Idx(10)->getWinding() == -1);
assert(vl->getVtx_Idx(10)->getWinding() == vl->getVtx_Uid(11)->getWinding());
assert(vl->getVtx_Idx(10)->getWinding() == -vl->getVtx_Uid(-1)->getWinding());
std::cout << "Passed getVtx_Idx "<<std::endl;
//########################################################################//
// VtxList::addVtx(std::shared_ptr<Vtx::Vortex> vtx, std::size_t idx)
//########################################################################//
vl->addVtx(std::make_shared<Vtx::Vortex>(c,cD, 3, false, 1000),5);
vl->addVtx(std::make_shared<Vtx::Vortex>(c,cD, 3, false, true, 1000),5);
assert(vl->getVtx_Idx(5)->getIsOn() == false);
assert(vl->getVtx_Idx(5)->getWinding() == 3);
assert(vl->getVtxIdx_Uid(12) == 5);
std::cout << (vl->getVtxIdx_Uid(12) == -1) << "\n";
assert(vl->getVtxIdx_Uid(12) == -1);
assert(vl->getVtx_Idx(11)->getWinding() == vl->getVtx_Uid(11)->getWinding());
std::cout << "Passed addVtx at arbitrary position "<<std::endl;
Expand Down Expand Up @@ -144,30 +147,100 @@ int main(){
assert( vl->getVtx_Idx(6)->getUID() == 7 );
std::cout << "Passed sortVortices check"<<std::endl;
*/
//########################################################################//
//########################################################################//


std::cout << "Resetting current and previous list for testing"<<std::endl;

vl.reset(); vl = std::make_shared<Vtx::VtxList>(8);
vlp.reset(); vlp = std::make_shared<Vtx::VtxList>(8);

//########################################################################//
// VtxList::arrangeVtx(std::vector<std::shared_ptr<Vtx::Vortex> > &vPrev)
// Populate vortex arrays
//########################################################################//
//Vtx::Vortex::resetSUID();
for( int i = 0; i < 11; ++i ){
for( int i = 0; i < 4; ++i ){
c.x = i;
c.y = i;
cD.x = i+dist(rng);
cD.y = i+dist(rng);
vlp->addVtx(std::make_shared<Vtx::Vortex>(c, cD, 1, true, 0));
vlp->getVtx_Idx(i)->updateUID(vlp->getMax_Uid());
}
vl->arrangeVtx(vlp->getVortices());

c.x = 12;
c.y = 16;
cD.x = 12+dist(rng);
cD.y = 16+dist(rng);
vlp->addVtx(std::make_shared<Vtx::Vortex>(c, cD, 1, true, 0));
vlp->getVtx_Idx(4)->updateUID(vlp->getMax_Uid());

for( int i = 5; i < 8; ++i ){
c.x = i;
c.y = i;
cD.x = i+dist(rng);
cD.y = i+dist(rng);
vlp->addVtx(std::make_shared<Vtx::Vortex>(c, cD, 1, true, 0));
vlp->getVtx_Idx(i)->updateUID(vlp->getMax_Uid());
}
int loop=0;
test:
if (loop==3)
exit(0);
int size=9;
for( int i = size; i >= 0; --i ){
c.x = i;
c.y = i;
cD.x = i+dist(rng);
cD.y = i+dist(rng);
auto v = std::make_shared<Vtx::Vortex>(c, cD, 1, false, 0);
vl->addVtx(v);
}


for(auto a: vlp->getVortices())
std::cout << a->getUID() << "\n";
//########################################################################//
// VtxList::minDistPair(std::shared_ptr<Vortex> vtx, double minRange)
//########################################################################//
for (int i=0; i < 11; ++i){
auto e = vl->minDistPair(vlp->getVtx_Idx(i), 5);
if(e.second != nullptr)
std::cout << "UIDc=" << e.second->getUID() << " UIDp=" << i << "\n\n";
else
1;
// This entire section would be more efficiently implemented by unioning
// the two lists, and then selecting behaviour based upon the required
// operations. But, that is more effort right now than it is worth...
//########################################################################//
for (auto vtxPrev : vlp->getVortices()) {
auto vtxMin = vl->minDistPair(vtxPrev, 1);
if (vtxMin.second != nullptr && vtxMin.second->getUID() < 0 ) {
vtxMin.second->updateUID(vtxPrev->getUID());
vtxMin.second->updateIsOn(true);
std::cout << "UIDc=" << vtxMin.second->getUID() << " UIDp=" << vtxPrev->getUID() << "\n";
}
else{
vtxPrev->updateIsOn(false);
vl->addVtx(vtxPrev);
std::cout << "No min found for UID=" << vtxPrev->getUID() << " X,Y="<< vtxPrev->getCoordsD().x << "," << vtxPrev->getCoordsD().y <<"\n";
}
}
std::cout << "\n";
int count=0;
for (auto v: vl->getVortices()) {
count ++;
if (!(v->getUID() < 0))
std::cout << "ON:="<< v->getIsOn() << " UIDc:=" << v->getUID()<< "\n";
else {
std::cout << "Index=" << count << "\n";
v->updateUID(vl->getMax_Uid()++);
v->updateIsOn(true);
std::cout << "ON:="<< v->getIsOn() << " UIDc:=" << v->getUID()<< "\n";
}
}
std::cout << std::endl;
//vl->arrangeVtx(vlp->getVortices());
vlp->getVortices().swap(vl->getVortices());
loop++;
goto test;



//nvcc -g -G ./vort.cc -o vort.o -std=c++11 -c -Wno-deprecated-gpu-targets; nvcc -g -G ./VtxTest.cc -std=c++11 -o VtxTest.o -c -Wno-deprecated-gpu-targets; nvcc vort.o VtxTest.o -Wno-deprecated-gpu-targets; ./a.out
return 0;
}
47 changes: 24 additions & 23 deletions src/vort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
namespace Vtx {

Vortex::Vortex():uid(-1){ }
Vortex::Vortex(int2 coords, double2 coordsD, int winding, int isOn, std::size_t timeStep):uid(-1){
this->coords = coords;
this->coordsD = coordsD;
this->winding = winding;
this->isOn = isOn;
Vortex::Vortex(int2 coords, double2 coordsD, int winding, bool isOn, std::size_t timeStep):uid(-1){
this->coords = coords; //Coords to the grid
this->coordsD = coordsD; //Subgrid coords
this->winding = winding; //Charge of vortex
this->isOn = isOn; //Whether the vortex still exists, or has died/gone outside boundary
this->timeStep = timeStep;
}
Vortex::~Vortex(){ }

void Vortex::updateUID(std::size_t uid){
void Vortex::updateUID(int uid){
this->uid = uid;
}
void Vortex::updateWinding(int winding){
Expand All @@ -33,7 +33,7 @@ namespace Vtx {
this->timeStep = timeStep;
}

std::size_t Vortex::getUID(){
int Vortex::getUID(){
return this->uid;
}
int Vortex::getWinding(){
Expand All @@ -56,9 +56,9 @@ namespace Vtx {
//######################################################################################################################
//######################################################################################################################

VtxList::VtxList():suid(-1) {
VtxList::VtxList():suid(0) {
}
VtxList::VtxList(std::size_t reserveSize):suid(-1) {
VtxList::VtxList(std::size_t reserveSize):suid(0) {
vortices.reserve(reserveSize);
}
VtxList::~VtxList() {
Expand All @@ -67,7 +67,7 @@ namespace Vtx {

//Add vortex to end of list.
void VtxList::addVtx(std::shared_ptr<Vtx::Vortex> vtx) {
vtx->updateUID(++this->suid);
this->suid++;
this->getVortices().push_back(vtx);
}
//Add vortex to list at the given idx
Expand All @@ -91,7 +91,7 @@ namespace Vtx {
}

//Assumes UID exists.
std::shared_ptr<Vtx::Vortex> VtxList::getVtx_Uid(std::size_t uid){
std::shared_ptr<Vtx::Vortex> VtxList::getVtx_Uid(int uid){
for(auto a : this->vortices){
if(a->getUID() != uid){
continue;
Expand All @@ -107,7 +107,7 @@ namespace Vtx {
}

//Assumes UID exists
std::size_t VtxList::getVtxIdx_Uid(std::size_t uid){
std::size_t VtxList::getVtxIdx_Uid(int uid){
for(std::size_t t = 0; t < this->vortices.size(); ++t){
if(this->vortices[t]->getUID() != uid){
continue;
Expand All @@ -117,7 +117,7 @@ namespace Vtx {
}
}

std::size_t VtxList::getMax_Uid(){
std::size_t& VtxList::getMax_Uid(){
return this->suid;
}

Expand Down Expand Up @@ -149,7 +149,6 @@ namespace Vtx {
this->vortices[idx1]->updateUID(uid0);
}


void VtxList::sortVtxUID(){
std::sort(this->getVortices().begin(), this->getVortices().end(),
[](std::shared_ptr<Vtx::Vortex> v0, std::shared_ptr<Vtx::Vortex> v1)
Expand All @@ -168,7 +167,6 @@ namespace Vtx {
}
}


void VtxList::arrangeVtx(std::vector<std::shared_ptr<Vtx::Vortex> > &vPrev){
std::set<std::shared_ptr<Vtx::Vortex> > sVtx_d01, sVtx_d10, sVtx_inter;
//Find the intersection of the UIDs, as well as elements unique to prev or current
Expand Down Expand Up @@ -202,21 +200,21 @@ namespace Vtx {
return (v0->getUID() < v1->getUID());
}
);
/*
std::cout << "#######\n";

std::cout << "####Inter####\n";
for (auto e : sVtx_inter){
std::cout << (e)->getUID() << std::endl;
}
std::cout << "#######\n";
std::cout << "####Diff01####\n";
for (auto e : sVtx_d01){
std::cout << (e)->getUID() << std::endl;
}
std::cout << "#######\n";
std::cout << "####Diff10####\n";
for (auto e : sVtx_d10){
std::cout << (e)->getUID() << std::endl;
}
std::cout << "#######\n";
*/


}

Expand All @@ -241,8 +239,7 @@ namespace Vtx {
v
)
);
//std::cout << "Uc=" << v->getUID() << "\t Xc=" << v->coordsD.x << "\t Yc=" << v->coordsD.x;
// std::cout << "\t Up=" << vtx->getUID() <<"\t Xp=" << vtx->coordsD.x << "\tYp=" << vtx->coordsD.x << "\n";
//std::cout << "UIDin=" << vtx->getUID() << " UIDout=" << v->getUID() << " R=" << pow(v->coordsD.x - vtx->coordsD.x, 2) + pow(v->coordsD.y - vtx->coordsD.y, 2) << "\n" ;
}
};
pairRDist(this);
Expand All @@ -258,7 +255,11 @@ namespace Vtx {
* Outside of this range, the vortex is considered a different vortex, and so returns
* a nullptr, meaning that no vortex is found. */
auto pairMin = std::min_element( r_uid.begin(), r_uid.end(), compMin);
return {pairMin->first,(pairMin->first <= minRange) ? pairMin->second : nullptr};
return {pairMin->first,(pairMin->first <= minRange && pairMin->second->getWinding() == vtx->getWinding()) ? pairMin->second : nullptr};
}


/*void VtxList::increaseList(){
}*/
}

0 comments on commit b7aec1d

Please sign in to comment.