有关调用
大家好,新手请教一个小问题。这个里面calchi调用了L中的*p,但是为什么我在init_p中和在calchi中输出的两个p的文件中这个L->p(p1.dat & p2.dat文件的第二列)变化了呢?我没有变动过p,希望大家能够帮忙。程序代码:
#include <stdio.h> #include <stdlib.h> #include <math.h> /* FMG Solver of the EHL circular contact */ #define pi 3.1415926535897931 typedef struct { double hx; int ii; double *p, *f; double *hfi, *hrhs; double *w; double *K; double *pjac; /*old pressure for use in Jacobi relaxation*/ double rg; double Hm; } Level; typedef struct { int nx0; int maxlevel; double xa,xb; double h0; /*global constant and work unit*/ Level *Lk; } Stack; /********** GLOBAL VARIABLES **********************/ double MMoes, LMoes, H_0, rlambda, alphabar; double p0r, alpha, eta0, zr, maxl; void initialize(Stack *U, int nx0, int maxl, double xa, double xb, double h0) { /* initialize values in datastructure */ double hx; Level *L; int l,ii; U->xa=xa; U->xb=xb; U->maxlevel=maxl; U->h0=h0; U->Lk=(Level *)calloc(maxl+1,sizeof(Level)); hx=(xb-xa)/nx0; ii=nx0; for (l=1; l<=maxl; l++) { L=U->Lk+l; L->hx=hx; L->ii=ii; L->p =(double *)calloc(U->maxlevel+1,sizeof(double)); L->w =(double *)calloc(U->maxlevel+1,sizeof(double)); L->f =(double *)calloc(U->maxlevel+1,sizeof(double)); L->pjac =(double *)calloc(U->maxlevel+1,sizeof(double)); L->hfi =(double *)calloc(U->maxlevel+1,sizeof(double)); L->hrhs=(double *)calloc(U->maxlevel+1,sizeof(double)); L->K =(double *)calloc(U->maxlevel+1,sizeof(double)); printf("\n level: %2d ii=%4d, hx=%f",l,ii,hx); hx*=0.5; ii*=2; } } /********* SINGLE GRID ROUTINES *****/ void init_log(Stack *U, int l) { /* computes the kernel on level l */ int i; Level *L; double xp,xm,x; L=U->Lk+l; for (i=0; i<=L->ii;i++) { xp=(i+0.5)*L->hx; xm=xp-L->hx; L->K[i]=xp*(log(xp)-1)-xm*(log(xm)-1); } FILE *fkk; fkk=fopen("k1.dat","w"); for (i=0; i<=L->ii; i++) { x=U->xa+i*L->hx; fprintf(fkk,"%f %f\n",x,L->K[i]); } fprintf (fkk, "\n"); fclose(fkk); } void init_f(Stack *U, int l) { int i; Level *L; double x; L=U->Lk+l; for (i=0; i<=L->ii; i++) { x=U->xa+i*L->hx; L->hrhs[i]=0.5*x*x; L->f[i]=0.0; } L->rg=-pi/2.0; } void calchi(Stack *U, int lev) { int i,i1,i2; Level *L; L=U->Lk+lev; double helpp,x; double iii; iii=L->ii; double *p,*K; p=L->p; K=L->K; for (i1=0; i1<=iii; i1++) { helpp=0.0; for (i=0; i<=iii; i++) helpp+=L->K[abs(i-i1)]*L->p[i]; L->w[i1]=helpp; } for (i2=0; i2<=iii; i2++) L->hfi[i2]=U->h0+2.0/pi/pi*L->w[i2]+L->hrhs[i2]; FILE *fpp; fpp=fopen("p2.dat","w"); for (i=0; i<=L->ii; i++) { x=U->xa+i*L->hx; fprintf(fpp,"%f %f\n",x,L->p[i]); } fprintf (fpp, "\n"); fclose(fpp); } void init_p(Stack *U, int l) { int i; Level *L; double x; double *p; L =U->Lk+l; p=L->p; for (i=0; i<=L->ii; i++) { x=U->xa+i*L->hx; p[i]=0.0; if (x*x<1.0) p[i]=sqrt(1.0-x*x); } FILE *fp; fp=fopen("p1.dat","w"); for (i=0; i<=L->ii; i++) { x=U->xa+i*L->hx; fprintf(fp,"%f %f\n",x,L->p[i]); } fprintf (fp, "\n"); fclose(fp); } /********** MAIN PROGRAM **********/ void main() { Stack U; initialize(&U,128,1,-4.5,1.5,-0.5); init_log(&U,1); init_f(&U,1); init_p(&U,1); calchi(&U,1); }