gaussgrid.c 6.33 KB
Newer Older
Oliver Heidmann's avatar
Oliver Heidmann committed
1
2
3
4
5
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <config.h>
6
7
8
9
10
#include <math.h>
#include <float.h>
#include <stdio.h>
#include <stdlib.h>

Oliver Heidmann's avatar
Oliver Heidmann committed
11

Uwe Schulzweida's avatar
Uwe Schulzweida committed
12
13
#include "dmemory.h"

14
15
16
#ifndef  M_PI
#define  M_PI        3.14159265358979323846  /* pi */
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
17
18
19
20
21
22

#ifndef  M_SQRT2
#define  M_SQRT2     1.41421356237309504880
#endif


23
static
24
void cpledn(size_t kn, size_t kodd, double *pfn, double pdx, int kflag, 
25
26
27
28
29
30
31
32
            double *pw, double *pdxn, double *pxmod)
{
  double zdlk;
  double zdlldn;
  double zdlx;
  double zdlmod;
  double zdlxn;

33
  size_t ik, jn;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

  /* 1.0 Newton iteration step */

  zdlx = pdx;
  zdlk = 0.0;
  if (kodd == 0) 
    {
      zdlk = 0.5*pfn[0];
    }
  zdlxn  = 0.0;
  zdlldn = 0.0;

  ik = 1;

  if (kflag == 0) 
    {
50
      for(size_t jn = 2-kodd; jn <= kn; jn += 2) 
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
	{
	  /* normalised ordinary Legendre polynomial == \overbar{p_n}^0 */
	  zdlk   = zdlk + pfn[ik]*cos((double)(jn)*zdlx);
	  /* normalised derivative == d/d\theta(\overbar{p_n}^0) */
	  zdlldn = zdlldn - pfn[ik]*(double)(jn)*sin((double)(jn)*zdlx);
	  ik++;
	}
      /* Newton method */
      zdlmod = -(zdlk/zdlldn);
      zdlxn = zdlx + zdlmod;
      *pdxn = zdlxn;
      *pxmod = zdlmod;
    }

  /* 2.0 Compute weights */

  if (kflag == 1) 
    {
      for(jn = 2-kodd; jn <= kn; jn += 2) 
	{
	  /* normalised derivative */
	  zdlldn = zdlldn - pfn[ik]*(double)(jn)*sin((double)(jn)*zdlx);
	  ik++;
	}
      *pw = (double)(2*kn+1)/(zdlldn*zdlldn);
    }

  return;
}

static
82
void gawl(double *pfn, double *pl, double *pw, size_t kn)
83
84
85
86
87
88
89
90
91
92
93
94
95
{
  double pmod = 0;
  int iflag;
  int itemax;
  double zw = 0;
  double zdlx;
  double zdlxn = 0;

  /* 1.0 Initizialization */

  iflag  =  0;
  itemax = 20;

96
  size_t iodd   = (kn % 2);
97
98
99
100
101

  zdlx   =  *pl;

  /* 2.0 Newton iteration */

Thomas Jahns's avatar
Thomas Jahns committed
102
  for (int jter = 1; jter <= itemax+1; jter++)
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    {
      cpledn(kn, iodd, pfn, zdlx, iflag, &zw, &zdlxn, &pmod);
      zdlx = zdlxn;
      if (iflag == 1) break;
      if (fabs(pmod) <= DBL_EPSILON*1000.0) iflag = 1;
    }

  *pl = zdlxn;
  *pw = zw;

  return;
}

static
117
void gauaw(size_t kn, double *__restrict__ pl, double *__restrict__ pw)
118
119
120
121
122
123
124
125
126
127
128
{
  /*
   * 1.0 Initialize Fourier coefficients for ordinary Legendre polynomials
   *
   * Belousov, Swarztrauber, and ECHAM use zfn(0,0) = sqrt(2)
   * IFS normalisation chosen to be 0.5*Integral(Pnm**2) = 1 (zfn(0,0) = 2.0)
   */
  double *zfn, *zfnlat;

  double z, zfnn;

129
130
  zfn    = (double *) malloc((kn+1) * (kn+1) * sizeof(double));
  zfnlat = (double *) malloc((kn/2+1+1)*sizeof(double));
131
132

  zfn[0] = M_SQRT2;
133
  for (size_t jn = 1; jn <= kn; jn++)
134
135
    {
      zfnn = zfn[0];
136
      for (size_t jgl = 1; jgl <= jn; jgl++)
137
138
139
140
141
142
	{
	  zfnn *= sqrt(1.0-0.25/((double)(jgl*jgl))); 
	}

      zfn[jn*(kn+1)+jn] = zfnn;

143
144
      size_t iodd = jn % 2;
      for (size_t jgl = 2; jgl <= jn-iodd; jgl += 2) 
145
146
147
148
149
150
151
152
153
	{
	  zfn[jn*(kn+1)+jn-jgl] = zfn[jn*(kn+1)+jn-jgl+2]
	    *((double)((jgl-1)*(2*jn-jgl+2)))/((double)(jgl*(2*jn-jgl+1)));
	}
    }


  /* 2.0 Gaussian latitudes and weights */

154
155
156
  size_t iodd = kn % 2;
  size_t ik = iodd;
  for (size_t jgl = iodd; jgl <= kn; jgl += 2)
157
158
159
160
161
162
163
164
165
166
    {
      zfnlat[ik] = zfn[kn*(kn+1)+jgl];
      ik++;
    } 

  /*
   * 2.1 Find first approximation of the roots of the
   *     Legendre polynomial of degree kn.
   */

167
  size_t ins2 = kn/2+(kn % 2);
168

169
  for (size_t jgl = 1; jgl <= ins2; jgl++) 
170
171
172
173
174
175
176
    {
      z = ((double)(4*jgl-1))*M_PI/((double)(4*kn+2)); 
      pl[jgl-1] = z+1.0/(tan(z)*((double)(8*kn*kn)));
    }

  /* 2.2 Computes roots and weights for transformed theta */

177
  for (size_t jgl = ins2; jgl >= 1 ; jgl--) 
178
    {
179
      size_t jglm1 = jgl-1;
Thomas Jahns's avatar
Thomas Jahns committed
180
      gawl(zfnlat, &(pl[jglm1]), &(pw[jglm1]), kn);
181
182
183
184
    }

  /* convert to physical latitude */

185
  for (size_t jgl = 0; jgl < ins2; jgl++) 
186
187
188
189
    {
      pl[jgl] = cos(pl[jgl]);
    }

190
  for (size_t jgl = 1; jgl <= kn/2; jgl++) 
191
    {
192
193
      size_t jglm1 = jgl-1;
      size_t isym =  kn-jgl;
194
195
196
197
198
199
200
201
202
203
      pl[isym] =  -pl[jglm1];
      pw[isym] =  pw[jglm1];
    }

  free(zfnlat);
  free(zfn);

  return;
}

204
#if 0
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
static
void gauaw_old(double *pa, double *pw, int nlat)
{
  /*
   * Compute Gaussian latitudes.  On return pa contains the
   * sine of the latitudes starting closest to the north pole and going
   * toward the south
   *
   */

  const int itemax = 20;

  int isym, iter, ins2, jn, j;
  double za, zw, zan;
  double z, zk, zkm1, zkm2, zx, zxn, zldn, zmod;

  /*
   * Perform the Newton loop
   * Find 0 of Legendre polynomial with Newton loop
   */

  ins2 = nlat/2 + nlat%2;

  for ( j = 0; j < ins2; j++ )
    {
      z = (double) (4*(j+1)-1)*M_PI / (double) (4*nlat+2);
      pa[j] = cos(z + 1.0/(tan(z)*(double)(8*nlat*nlat)));
    }

  for ( j = 0; j < ins2; j++ )
    {

      za = pa[j];

      iter = 0;
      do
	{
	  iter++;
	  zk = 0.0;

	  /* Newton iteration step */

	  zkm2 = 1.0;
	  zkm1 = za;
	  zx = za;
	  for ( jn = 2; jn <= nlat; jn++ )
	    {
	      zk = ((double) (2*jn-1)*zx*zkm1-(double)(jn-1)*zkm2) / (double)(jn);
	      zkm2 = zkm1;
	      zkm1 = zk;
	    }
	  zkm1 = zkm2;
	  zldn = ((double) (nlat)*(zkm1-zx*zk)) / (1.-zx*zx);
	  zmod = -zk/zldn;
	  zxn = zx+zmod;
	  zan = zxn;

	  /* computes weight */

	  zkm2 = 1.0;
	  zkm1 = zxn;
	  zx = zxn;
	  for ( jn = 2; jn <= nlat; jn++ )
	    {
	      zk = ((double) (2*jn-1)*zx*zkm1-(double)(jn-1)*zkm2) / (double) (jn);
	      zkm2 = zkm1;
	      zkm1 = zk;
	    }
	  zkm1 = zkm2;
	  zw = (1.0-zx*zx) / ((double) (nlat*nlat)*zkm1*zkm1);
	  za = zan;
	}
      while ( iter <= itemax && fabs(zmod) >= DBL_EPSILON );

      pa[j] = zan;
      pw[j] = 2.0*zw;
    }

#if defined (SX)
#pragma vdir nodep
#endif
  for (j = 0; j < nlat/2; j++)
    {
      isym = nlat-(j+1);
      pa[isym] = -pa[j];
      pw[isym] =  pw[j];
    }

  return;
}
295
#endif
296

297
void gaussaw(double *restrict pa, double *restrict pw, size_t nlat)
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
{
  //gauaw_old(pa, pw, nlat);
  gauaw(nlat, pa, pw);
}

/*
#define NGL  48

int main (int rgc, char *argv[])
{
  int ngl = NGL;
  double plo[NGL], pwo[NGL];
  double pl[NGL], pw[NGL];

  int i;

  gauaw(ngl, pl, pw);
  gauaw_old(plo, pwo, ngl);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
316
317
318
319
320
  for (i = 0; i < ngl; i++)
    {
      pl[i]  = asin(pl[i])/M_PI*180.0;
      plo[i] = asin(plo[i])/M_PI*180.0;
    }
321
322
323

  for (i = 0; i < ngl; i++)
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
324
      fprintf(stderr, "%4d%25.18f%25.18f%25.18f%25.18f\n", i+1, pl[i], pw[i], pl[i]-plo[i], pw[i]-pwo[i]);
325
326
327
328
329
    }

  return 0;
}
*/
330
331
332
333
334
335
336
337
338
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */