-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrid.c
71 lines (56 loc) · 1.4 KB
/
grid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdlib.h>
#include "grid.h"
#include "error.h"
double* uniform_partition(double lb, double ub, int N)
{
int i;
double h;
double* partition;
// Compute the step size
h = (ub - lb)/N;
// Allocate space for the parition
partition = calloc(N+1, sizeof(double));
for(i = 1, partition[0] = lb; i < N; i++)
{
partition[i] = partition[i - 1] + h;
}
partition[N] = ub;
return partition;
}
grid* build_cartesian_grid(double lb_x, double ub_x, double lb_y, double ub_y, int N)
{
grid* cartesian_grid;
if(ub_x - lb_x != ub_y - lb_y)
{
error("The domain is not square.");
}
cartesian_grid = calloc(1, sizeof(grid));
cartesian_grid->N = N;
cartesian_grid->lb_x = lb_x;
cartesian_grid->ub_x = ub_x;
cartesian_grid->lb_y = lb_y;
cartesian_grid->ub_y = ub_y;
cartesian_grid->h = (ub_x - lb_x)/N;
cartesian_grid->grid_nodes_x = uniform_partition(lb_x, ub_x, N); // Partition in x direction
cartesian_grid->grid_nodes_y = uniform_partition(lb_y, ub_y, N); // Partition in y direction
return cartesian_grid;
}
int cleanup_grid(grid* cartesian_grid)
{
#define CGX (cartesian_grid->grid_nodes_x)
#define CGY (cartesian_grid->grid_nodes_y)
if(cartesian_grid != NULL)
{
if(CGX != NULL)
{
free(CGX);
}
if(CGY != NULL)
{
free(CGY);
}
free(cartesian_grid);
}
#undef CG
return 0;
}