Skip to content

Commit

Permalink
had a huge bug in the program from the dust pools. this is what I get…
Browse files Browse the repository at this point in the history
… for committing code before vacation
  • Loading branch information
sirus20x6 committed Oct 27, 2024
1 parent 4d51c8b commit 7cdebf3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 93 deletions.
14 changes: 6 additions & 8 deletions accrete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "structs.h" // for planet, dust, gen, sun
#include "utils.h" // for toString, random_eccentricity, random_number

DustPool dustPool;

/**
* @brief Set Initial Conditions
Expand All @@ -22,7 +21,7 @@ void accrete::set_initial_conditions(long double inner_limit_of_dust,
planet_head = nullptr;
hist_head = nullptr;

dust_head = dustPool.acquireDust();
dust_head = new dust();
dust_head->next_band = nullptr;
dust_head->setOuterEdge(outer_limit_of_dust);
dust_head->setInnerEdge(inner_limit_of_dust);
Expand Down Expand Up @@ -167,7 +166,7 @@ void accrete::update_dust_lanes(long double min, long double max, long double ma
node1 = dust_head;
while (node1 != nullptr) {
if (node1->getInnerEdge() < min && node1->getOuterEdge() > max) {
node2 = dustPool.acquireDust();
node2 = new dust();
node2->setInnerEdge(min);
node2->setOuterEdge(max);
if (node1->getGasPresent()) {
Expand All @@ -176,7 +175,7 @@ void accrete::update_dust_lanes(long double min, long double max, long double ma
node2->setGasPresent(false);
}
node2->setDustPresent(false);
node3 = dustPool.acquireDust();
node3 = new dust();
node3->setInnerEdge(max);
node3->setOuterEdge(node1->getOuterEdge());
node3->setGasPresent(node1->getGasPresent());
Expand All @@ -188,7 +187,7 @@ void accrete::update_dust_lanes(long double min, long double max, long double ma
node1 = node3->next_band;
} else {
if (node1->getInnerEdge() < max && node1->getOuterEdge() > max) {
node2 = dustPool.acquireDust();
node2 = new dust();
node2->next_band = node1->next_band;
node2->setDustPresent(node1->getDustPresent());
node2->setGasPresent(node1->getGasPresent());
Expand All @@ -205,7 +204,7 @@ void accrete::update_dust_lanes(long double min, long double max, long double ma
node1 = node2->next_band;
} else {
if (node1->getInnerEdge() < min && node1->getOuterEdge() > min) {
node2 = dustPool.acquireDust();
node2 = new dust();
node2->next_band = node1->next_band;
node2->setDustPresent(false);
if (node1->getGasPresent()) {
Expand Down Expand Up @@ -962,8 +961,7 @@ void accrete::free_generations() {
next = node->next;

if (node->dusts != nullptr) {
//free_dust(node->dusts);
dustPool.releaseDust(node->dusts);
free_dust(node->dusts);
}

if (node->planets != nullptr) {
Expand Down
19 changes: 17 additions & 2 deletions structs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include "utils.h" // for quicksort, fix_inclination, my_strtoupper

using namespace std;
int dust::activeCount = 0;
int dust::peakCount = 0;

/**
* @brief Construct a new star::star object
Expand Down Expand Up @@ -1642,6 +1640,23 @@ auto planet::operator==(planet& right) -> bool {
}
return false;
}

dust::dust() : innerEdge(0), outerEdge(0), dustPresent(true), gasPresent(true), next_band(NULL) {





}

dust::~dust() {
innerEdge = 0;
outerEdge = 0;
dustPresent = false;
gasPresent = false;
// next_band = NULL;
}

auto dust::getDustPresent() -> bool { return dustPresent; }

auto dust::getGasPresent() -> bool { return gasPresent; }
Expand Down
92 changes: 9 additions & 83 deletions structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include <iosfwd> // for ostream, std
#include <string> // for string, basic_string
#include <vector> // for vector
#include <iostream>
#include <memory>
class ChemTable; // lines 496-496
class Chemical; // lines 427-427
class catalog; // lines 395-395
Expand Down Expand Up @@ -311,40 +309,15 @@ class planet {
};

class dust {
private:
long double innerEdge{0};
long double outerEdge{0};
bool dustPresent{true};
bool gasPresent{true};
bool inUse{false};

static int activeCount;
static int peakCount;

public:
dust() = default;

void reset() {
innerEdge = 0;
outerEdge = 0;
dustPresent = true;
gasPresent = true;
inUse = false;
}

static void resetCounts() {
activeCount = 0;
peakCount = 0;
std::cout << "Dust counts reset." << std::endl;
}

static int getActiveCount() { return activeCount; }
static int getPeakCount() { return peakCount; }

friend class DustPool;

// Initialize static members
private:
long double innerEdge{0};
long double outerEdge{0};
bool dustPresent{true};
bool gasPresent{true};

public:
dust();
~dust();
void setInnerEdge(long double);
auto getInnerEdge() -> long double;
void setOuterEdge(long double);
Expand All @@ -356,53 +329,6 @@ class dust {
dust *next_band;
};

class DustPool {
private:
static const int POOL_SIZE = 20; // Adjust based on your maximum observed usage
std::vector<std::unique_ptr<dust>> pool;

public:
DustPool() {
for (int i = 0; i < POOL_SIZE; ++i) {
pool.push_back(std::make_unique<dust>());
}
std::cout << "Dust pool initialized with " << POOL_SIZE << " objects." << std::endl;
}

dust* acquireDust() {
for (auto& dustPtr : pool) {
if (!dustPtr->inUse) {
dustPtr->inUse = true;
dust::activeCount++;
if (dust::activeCount > dust::peakCount) {
dust::peakCount = dust::activeCount;
}
std::cout << "Dust object acquired. Active count: " << dust::activeCount
<< ", Peak count: " << dust::peakCount << std::endl;
return dustPtr.get();
}
}
std::cerr << "Warning: Dust pool exhausted!" << std::endl;
return nullptr;
}

void releaseDust(dust* d) {
if (d && d->inUse) {
d->reset();
d->inUse = false;
dust::activeCount--;
std::cout << "Dust object released. Active count: " << dust::activeCount << std::endl;
}
}

~DustPool() {
std::cout << "Dust pool destroyed. Final active count: " << dust::activeCount
<< ", Peak count: " << dust::peakCount << std::endl;
}
};



class star;

auto operator<<(ostream &, star &) -> ostream &;
Expand Down Expand Up @@ -588,4 +514,4 @@ class ChemTable {
friend auto operator<<(ostream &, ChemTable &) -> ostream &;
};

#endif
#endif
2 changes: 2 additions & 0 deletions testrun.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
./stargen -m1.09 -y1.12609 -BG0V -b6215 -M -r -s1 -n1000 -g

0 comments on commit 7cdebf3

Please sign in to comment.