-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfdsolv.c
97 lines (85 loc) · 2.5 KB
/
fdsolv.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* this is the initial prototype of a solver for pb-electrostatics
* the eventual idea is to output the interesting parts
* of the solution as well as the field.
*
* i.e. we need maps of the debye charge density
* and of the "dielectric" charge.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "numeric.h"
main()
{
float fd_sweep();
float (*grid)[],(*field)[];
float delta;
int i,j,k;
int nx,ny,nz;
nx = 10; ny = 10; nz = 10;
grid = malloc( nx*ny*nz*sizeof(float));
field = malloc(nx*ny*nz*sizeof(float));
(*grid)[4*nx*ny + 3*nx +3] = 1.;
(*grid)[4*nx*ny + 7*nx +7] = -1.;
for( k=0; k< nz; k++)
{
/*
delta = fd_sweep(grid,field,nx,ny,nz,0.5,0.5,0.5);
*/
/* delta = fd_sweep(grid,field,nx,ny,nz,1.0,1.0,1.0);
*/
delta = fd_sweep(grid,field,nx,ny,nz,2.0,2.0,2.0);
/* delta = fd_sweep(grid,field,nx,ny,nz,4.0,4.0,4.0); */
printf("%f\n",delta);
for( i=0; i< nx; i++)
{
for( j=0; j<ny; j++)
{
printf("%10.5e ",(*field)[4*nx*ny + j*nx + i]);
}
printf("\n");
}
}
}/* end of main */
float fd_sweep(grid,field, nx,ny,nz,dx,dy,dz)
float (*grid)[]; /* the charges*/
float (*field)[]; /* the field */
int nx,ny,nz; /* the numbers of grid points */
float dx,dy,dz; /* the size of the grids in Angstroms */
{
int i,j,k;
float delta,deltamax;
float flux;
float flx,fly,flz,dv;
int izp,iyp,ixp,inxy;
flx = 1./(dx*dx);
fly = 1./(dy*dy);
flz = 1./(dz*dz);
dv = flx+flx +fly+fly + flz+flz;
dv = 1./dv;
inxy = nx*ny;
deltamax = 0.;
for( k=1; k< nz-1; k++)
{
izp = k*inxy;
for( j=1; j< ny-1; j++)
{
iyp = j*nx;
for( i=1; i< nx-1; i++)
{
ixp = i+iyp + izp;
flux = ( -(*field)[ixp-1])*flx;
flux += ( -(*field)[ixp+1])*flx;
flux += ( -(*field)[ixp-nx])*fly;
flux += ( -(*field)[ixp+nx])*fly;
flux += ( -(*field)[ixp-inxy])*flz;
flux += ( -(*field)[ixp+inxy])*flz;
delta = ((*grid)[ixp] - flux)*dv;
if( (*grid)[ixp] > 0.) printf("%f %f %f\n",(*grid)[ixp],flux,delta);
if( fabs(delta) > deltamax) deltamax = fabs(delta);
(*field)[ixp] = delta;
}/* i */
}/* j */
}/* k */
return deltamax;
}/* end of fd_sweep */