forked from MarcNairn/P3-group
-
Notifications
You must be signed in to change notification settings - Fork 0
/
laplace_algorithm.c++
49 lines (41 loc) · 1.47 KB
/
laplace_algorithm.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
/*This program generates TWO PARALLEL PLATES, EACH AT OPPOSITE POTENTIALS*/
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <fstream>
#define max 40 /* number of grid points */
using namespace std;
int main()
{
double x, p[max][max];
int i, j, iter, y;
ofstream myfile;
myfile.open ("laplace.dat");
for(i=0; i<max; i++) /* declare and clear the array, avoids possible overflow */
{
for (j=0; j<max; j++) p[i][j] = 0;
}
for(i=0; i<max; i++) p[i][0] = 100.0; /* p[i][0] = 100 V */
for(i=0; i<max; i++) p[i][max-1] = 100.0; /* p[i][39] = 100 V, now there's two parallel plates set at 100 and -100V respectively */
for(iter=0; iter<1000; iter++) /* iterations */
{
for(i=1; i<(max-1); i++) /* x-direction */
{
for(j=1; j<(max-1); j++) /* y-direction */
{
p[i][j] = 0.25*(p[i+1][j]+p[i-1][j]+p[i][j+1]+p[i][j-1]);
}
}
}
for (i=0; i<max ; i++) /* write data to then plot in gnuplot, 3D format */
{
for (j=0; j<max; j++)
{
myfile << p[i][j] << endl; /*write data onto file*/
/*Data is stored as 40 2D (x,y) horizontal planes stacked from bottom to top */
}
myfile << "\n"; /* empty line added to make the dataset readable on gnuplot*/
}
cout << "Data stored in laplace.dat"<< endl; /* save data in laplace.dat */
myfile.close();
}