Skip to content

Instantly share code, notes, and snippets.

@PtrMan
Created May 5, 2016 16:35
Show Gist options
  • Select an option

  • Save PtrMan/f789f17f7848e39db2eac1dcad8da570 to your computer and use it in GitHub Desktop.

Select an option

Save PtrMan/f789f17f7848e39db2eac1dcad8da570 to your computer and use it in GitHub Desktop.
CA fluid simulation
// part of the code useful for a stochastic fluid dynamics simulation based on a triangle grid of celular automata
typedef unsigned uint;
struct Dot {
bool current, next;
uint nextCounters; // number of potential particles which want to occupy this place
};
//bool unevenLineInZDirection = ((x) % 2) ^ ((y) % 2);
const uint NUMBER_OF_NEIGHBORS = 8;
Dot *searchDotWithIndexWhichIsNotNextOccupied(Dot *neighbors[NUMBER_OF_NEIGHBORS], uint usedNeighbors, uint index) {
uint currentIndex = 0;
for( uint i = 0; i < usedNeighbors; i++ ) {
if( !neighbors[i]->next ) {
if( currentIndex == index ) {
return neighbors[i];
}
currentIndex++;
}
}
// TODO< throw something >
return (Dot*)0;
}
uint countUnoccupiedNeighbors(Dot *neighbors[NUMBER_OF_NEIGHBORS], uint usedNeighbors) {
uint neightborOccupiedCounter = 0;
for( uint i = 0; i < usedNeighbors; i++ ) {
if( !neighbors[i]->next ) {
neightborOccupiedCounter++;
}
}
return neightborOccupiedCounter;
}
void moveToNeighborByRandom(Dot *currentDot, Dot *neighbors[NUMBER_OF_NEIGHBORS], uint usedNeighbors, uint rng) {
if( !currentDot->current ) {
return;
}
uint neightborOccupiedCounter = countUnoccupiedNeighbors(neighbors, usedNeighbors);
if( neightborOccupiedCounter == usedNeighbors ) {
// if all neighbors are occupied we can't move it to a enightbor node
}
else {
// choose another dot and move the current dot to it
uint destinationIndex = rng % neightborOccupiedCounter;
Dot *notOccupiedDestination = searchDotWithIndexWhichIsNotNextOccupied(neighbors, usedNeighbors, destinationIndex);
notOccupiedDestination->nextCounters++;
currentDot->next = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment