I am not used to code in c and not good at it. I looked at Allen book, the art of md simulation and many other books but, I can’t fix it. I’m so sorry if this looks so dumb and obvious but I’m desperate because it has been months.
This is the code that should update cell_list using the coordinates of each particles(I uploaded part of my whole code, if I need to more, please tell me). Also, I will use this cell_list to calculate forces between particles.
I think updating the cell_list is not working properly. Can someone please help me what I can improve in my code?
typedef struct {
double x;
double y;
double z;
} Particle;
typedef struct Node {
int name;
Particle particle;
struct Node* next;
} Node;
Node* cell_list[NUM_CELL][NUM_CELL][NUM_CELL] = {NULL};
void modify_cell(int name, double Coord[NBEDS][NDIMS]) {
double x = Coord[name][0];
double y = Coord[name][1];
double z = Coord[name][2];
int cell_x = (int)(x / CELL_SIZE);
int cell_y = (int)(y / CELL_SIZE);
int cell_z = (int)(z / CELL_SIZE);
if (cell_x >= 0 && cell_x < NUM_CELL && cell_y >= 0 && cell_y < NUM_CELL && cell_z >= 0 && cell_z < NUM_CELL) {
if (cell_list[cell_x][cell_y][cell_z] != NULL) {
Node* current = cell_list[cell_x][cell_y][cell_z];
while (current != NULL) {
if (current->name == name) {
return;
}
current = current->next;
}
}
Node* current = cell_list[cell_x][cell_y][cell_z];
Node* prev = NULL;
while (current != NULL) {
if (current->name == name) {
if (prev != NULL) {
prev->next = current->next;
} else {
cell_list[cell_x][cell_y][cell_z] = current->next;
}
free(current);
break;
}
prev = current;
current = current->next;
}
Particle new_particle;
new_particle.x = x;
new_particle.y = y;
new_particle.z = z;
Node* new_node = (Node*)malloc(sizeof(Node));
if (new_node == NULL) {
printf("Memory allocation failedn");
return;
}
new_node->name = name;
new_node->particle = new_particle;
new_node->next = cell_list[cell_x][cell_y][cell_z];
cell_list[cell_x][cell_y][cell_z] = new_node;
} else {
printf("Particle coordinates out of range.n");
}
}
I tried to debug and correct this modify_cell() function, however, none of it worked well. This is my latest version of my code. Any help will be useful to me.
hermit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.