-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathcore.cpp
40 lines (34 loc) · 1.07 KB
/
core.cpp
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
/* Main solver routines for heat equation solver */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "heat.h"
/* Update the temperature values using five-point stencil */
void evolve(field *curr, field *prev, double a, double dt)
{
double dx2, dy2;
int nx, ny;
double *currdata, *prevdata;
currdata = curr->data;
prevdata = prev->data;
nx = curr->nx;
ny = curr->ny;
/* Determine the temperature field at next time step
* As we have fixed boundary conditions, the outermost gridpoints
* are not updated. */
dx2 = prev->dx * prev->dx;
dy2 = prev->dy * prev->dy;
for (int i = 1; i < nx + 1; i++) {
for (int j = 1; j < ny + 1; j++) {
int ind = i * (ny + 2) + j;
int ip = (i + 1) * (ny + 2) + j;
int im = (i - 1) * (ny + 2) + j;
int jp = i * (ny + 2) + j + 1;
int jm = i * (ny + 2) + j - 1;
currdata[ind] = prevdata[ind] + a * dt *
((prevdata[ip] -2.0 * prevdata[ind] + prevdata[im]) / dx2 +
(prevdata[jp] - 2.0 * prevdata[ind] + prevdata[jm]) / dy2);
}
}
}