next up previous contents
Next: pppt.c Up: Source Codes Previous: poissonRod.c   Contents

poissonPlate.c

/* * poissonPlate.c * Poisson's equation in 2-D - explicit algorithm * serial implementation * * based on code located at * http://www.physics.buffalo.edu/phy516/homework.html * * refer to http://cs.utm.toronto.edu/ e0gagq6g/492/report.ps * for general comments * * written by Jun Sasaki */

#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h>

#include <unistd.h>

#define INITTMP 10

double L = 1.0; /* linear size of square region */ int N = 16; /* number of interior points per dim */

double **u, **u_new; /* linear arrays to hold solution */ char *filename = "poisson."; /* base for the filename of output */

/* memory related functions. * mallocs and frees the global arrays used*/ void allocate_arrays (void); void free_arrays (void);

void make_source(void);

/* computes one iteration of the explicit heat equation solver */ void explicit (void);

void write_result(char *fileNm);

int main (int argc, char *argv[]) int i,j,k=0, n; int step=0; double change, **swap; char fileNum[5]; char fileTmp[10];

if (argc > 1) sscanf(argv[1], "if (argc > 2) sscanf(argv[2], "

printf("2-D heat equation using explicit algorithm"); printf("============================================="); printf("Number of interior vertices =

allocate_arrays(); make_source();

strcpy(fileTmp, filename);

for(j=0;j<k;j++) explicit();

/* interchange u and u_new */ swap = u; u = u_new; u_new = swap;

if(j sprintf(fileNum, "

strcpy(fileTmp, ""); strcpy(fileTmp, filename); strcat(fileTmp, fileNum);

write_result(fileTmp); step++;

free_arrays();

return 0;

void explicit (void) int i,j; int previ,prevj; int nexti,nextj; double z = 0.22;

u[N/2][N/2]=INITTMP; u_new[N/2][N/2]=INITTMP;

/* explicit update for vertices in my domain */ for (i = 0; i <= N+1; i++) for(j = 0; j <= N+1; j++) if(i==N/2 && j==N/2) /* this would fix the temperature along one line at i==0 */ else if(i==0) previ = N+1; // u[i][j]=INITTMP; else previ = i-1;

if(j==0) prevj = N+1; else prevj = j-1;

nexti = (i+1)nextj = (j+1)

u_new[i][j] = (1 - 4*z) * u[i][j] + z * (u[previ][j] + u[nexti][j] + u[i][prevj] + u[i][nextj]);

/* end j for */ /* end i for */

void make_source(void) int i=0; // u[i][i] = INITTMP;

void allocate_arrays (void) int size, i,j;

size = (N + 2); u = malloc(size * sizeof(void *)); u_new = malloc(size * sizeof(void *));

for(i = 0; i < size; i++) u[i] = (double*) malloc(size * sizeof(double)); u_new[i] = (double*) malloc(size * sizeof(double));

for (i = 0; i < size; i++) for(j=0; j < size; j++) u[i][j] = u_new[i][j] = 0;

void free_arrays (void) int i; int size = (N+2);

for(i = 0; i < size; i++) free(u[i]); free(u_new[i]);

free(u); free(u_new);

void write_result(char *fileNm) int i,j; FILE *fp = fopen(fileNm,"w");

fprintf(fp, "P2"); fprintf(fp, "fprintf(fp, "255");

for(i=0;i<=N+1;i++) for(j=0;j<=N;j++) fprintf(fp, "

fprintf(fp, "

fclose(fp);


next up previous contents
Next: pppt.c Up: Source Codes Previous: poissonRod.c   Contents
J S 2002-08-14