Newer
Older
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
/* Utility functions for heat equation solver
* NOTE: This file does not need to be edited! */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "heat.h"
/* Utility routine for allocating a two dimensional array */
double **malloc_2d(int nx, int ny)
{
double **array;
int i;
array = (double **) malloc(nx * sizeof(double *));
array[0] = (double *) malloc(nx * ny * sizeof(double));
for (i = 1; i < nx; i++) {
array[i] = array[0] + i * ny;
}
return array;
}
/* Utility routine for deallocating a two dimensional array */
void free_2d(double **array)
{
free(array[0]);
free(array);
}
/* Copy data on temperature1 into temperature2 */
void copy_field(field *temperature1, field *temperature2)
{
assert(temperature1->nx == temperature2->nx);
assert(temperature1->ny == temperature2->ny);
memcpy(temperature2->data[0], temperature1->data[0],
(temperature1->nx + 2) * (temperature1->ny + 2) * sizeof(double));
}
/* Swap the data of fields temperature1 and temperature2 */
void swap_fields(field *temperature1, field *temperature2)
{
double **tmp;
tmp = temperature1->data;
temperature1->data = temperature2->data;
temperature2->data = tmp;
}