sandbox/Antoonvh/tie_coord_to_cell.c
Is it possible to reference arrays from cell data?
Yes. Here is an example of how array data (a coord) can be assigned to a cell. See the output.
scalar data_pointer[];
typedef union{
double* p;
double d;
} datic;
// Our data we wish to store in some cell
= {0.3123, 0.6123, 35};
coord a
int main() {
// This is an important assertion:
assert (sizeof(double*) == sizeof (double));
(N);
init_grid = locate (a.x, a.y, a.z); { // Find cell
Point point // datic ap = {.p = (double*)&a}; // option 1 (not used)
datic ap = {.p = malloc (3*sizeof(double))}; // allocate array
(ap.p, &a, sizeof(coord)); // assign values
memcpy [] = ap.d; // store pointer
data_pointer}
// Search for the data using the `data_pointer` field
foreach() {
if (data_pointer[]) {
datic ap = {.d = data_pointer[]};
* ar = (coord*)ap.p, cc = {x, y, z};
coord ("A position was found:");
puts foreach_dimension(3)
printf ("%g is near %g?\n", ar->x, cc.x);
}
}
// cleanup
foreach() {
if (data_pointer[]) {
free((datic){.d = data_pointer[]}.p); // Not needed with option 1.
[] = 0.;
data_pointer}
}
}