Hello C Forum,
I'm trying to build a moving mesh. What's a moving mesh, you ask. Brilliant, it's a mesh that rearranges the grid points to have a higher density of points in a localized region. So let's let talk about my node structure:
Code:
struct node {
struct particle *parts;
double p[3];
double *spacing; //0 and 1 : negative and positive z
//2 and 3 : " " " x
//4 and 5 : " " " y
};
As you can see, my nodes need positions (for particles) and there's a spacing which governs the distance between each node in all 6 possible Cartesian directions.
And let's look at my allocation procedure:
Code:
#define gl 128#define len 635
struct node*** build_mesh(void) {
struct node ***mesh;
mesh = malloc(gl*sizeof(*mesh));
for (int i=0; i<gl; i++) {
mesh[i] = malloc(gl*sizeof(*mesh[i]));
for (int j=0; j<gl; j++) {
mesh[i][j] = malloc(gl*sizeof(*mesh[i][j]));
for (int k=0; k<gl; k++) {
double *soft;
soft = malloc(6*sizeof(*soft));
for (int x=0; x<6; x++) {
soft[x] = len/(gl-1);
}
mesh[i][j][k].spacing = soft;
mesh[i][j][k].p[0] = k*len/(gl-1);
mesh[i][j][k].p[1] = j*len/(gl-1);
mesh[i][j][k].p[2] = i*len/(gl-1);
//printf("%e, %e, %e\n", mesh[i][j][k].p[0], mesh[i][j][k].p[1], mesh[i][j][k].p[2]);
}
}
}
return mesh;
}
void free_mesh(struct node ***mesh) {
for (int i=0; i<gl; i++) {
for (int j=0; j<gl; j++) {
for (int k=0; k<gl; k++) {
free(mesh[i][j][k].spacing);
}
free(mesh[i][j]);
}
free(mesh[i]);
}
free(mesh);
}
This code seems to allocate a 3D grid with proper positions and spacings but I'm unsure if I'm allocating this correctly. I have to dynamically allocate the spacing because I was getting stack overflows or whatever error you get when the stack size has been exceeded. Basically, this is eating up a crap ton of memory and I want to be sure that I'm doing this the way it's supposed to be done.
Edit: Note that this code, when run, eats up ~1.85 GB of RAM so be careful if you try to make it go. I could just lower the size but I have plenty to spare.