File: gengauss.py

package info (click to toggle)
sasmodels 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,888 kB
  • sloc: python: 25,392; ansic: 7,377; makefile: 149; sh: 61
file content (47 lines) | stat: -rwxr-xr-x 1,251 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
"""
Generate the Gauss-Legendre integration points and save them as a C file.
"""
from __future__ import division, print_function

import numpy as np
from numpy.polynomial.legendre import leggauss

def gengauss(n, path):
    """
    Save the Gauss-Legendre integration points for length *n* into file *path*.
    """
    z, w = leggauss(n)

    # Make sure array size is a multiple of 4
    if n%4:
        array_size = n + (4 - n%4)
        z, w = [np.hstack((v, [0.]*(4-n%4))) for v in (z, w)]
    else:
        array_size = n

    with open(path, "w") as fid:
        fid.write("""\
// Generated by sasmodels.gengauss.gengauss(%d)

#ifdef GAUSS_N
# undef GAUSS_N
# undef GAUSS_Z
# undef GAUSS_W
#endif
#define GAUSS_N %d
#define GAUSS_Z Gauss%dZ
#define GAUSS_W Gauss%dWt

"""%(n, n, n, n))

        if array_size != n:
            fid.write("// Note: using array size %d so that it is a multiple of 4\n\n"%array_size)

        fid.write("constant double Gauss%dWt[%d]={\n"%(n, array_size))
        fid.write(",\n".join("\t% .15e"%v for v in w))
        fid.write("\n};\n")

        fid.write("constant double Gauss%dZ[%d]={\n"%(n, array_size))
        fid.write(",\n".join("\t% .15e"%v for v in z))
        fid.write("\n};")