aboutsummaryrefslogtreecommitdiff
path: root/active_correction/scatter/2016.05.02 16-33-15 current delay space/src/inhom.py
blob: 1438846fdc12bc35f2b9123eb2b3b858f44d6c80 (plain)
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
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 21 14:07:53 2014

@author: Dan
"""

from NISE.lib.misc import *

class Inhom():
    # class contains the list of weights and sampling values to use
    #--------------------------Recorded attributes--------------------------
    out_vars = ['inhom_sampling', 'dist_params']
    #--------------------------Methods--------------------------
    def __init__(self, inhom_sampling=None, **dist_params):
        """
        generates the list of sampling points in the distribution and their weights
        inhom dists should be normalized (int(f, dzeta) = 1.)
        """
        # inherit all class attributes unless kwargs has them; then use those 
        # values.  if kwargs is not an Omega attribute, it gets ignored
        for key, value in dist_params.items():
            setattr(self, key, value)
        #print self.__dict__.items()
        # eliminating other quadrature methods; linear works best anyways
        if inhom_sampling == 'linear':
            # currently the only inhomogeneity parameter that can normalize well 
            # in relation to the case of no inhomogeneity
            if isinstance(dist_params.get('num'), int):
                num = dist_params.get('num')
            else:
                try:
                    num = int(num)
                except TypeError:
                    print 'no distribution sampling number specified; using 10 points as default'
                    num = 10
            if 'zeta_bound' in dist_params.keys():
                zeta_bound = dist_params.get('zeta_bound')
            else:
                zeta_bound = 3
            zeta = np.linspace(-zeta_bound, zeta_bound, num=num)
            # need parameter 'sigma'
            sigma = dist_params.get('sigma')
            # scale our sampling intervals according to sigma
            zeta = zeta * sigma
            self.zweight = 1 / (np.sqrt(2*np.pi)*sigma) * np.exp(- 0.5 * ((zeta / sigma)**2))
            self.dzeta = np.abs(zeta[1] - zeta[0])
            self.zeta = zeta
        elif inhom_sampling == 'rect':
            w = dist_params.get('w')
            if isinstance(dist_params.get('num'), int):
                num = dist_params['num']
            else:
                try:
                    num = int(num)
                except TypeError:
                    print 'no distribution sampling number specified; using 10 points as default'
                    num = 10
            self.zeta = np.linspace(-w,w,num=num)
            self.dzeta = np.abs(self.zeta[1] - self.zeta[0])
            self.zweight = np.ones(self.zeta.shape)
        elif inhom_sampling == 'gh':
            import NISE.hamiltonians.params.gauss_hermite as gh
            # gaussian-hermite quadrature
            # see http://en.wikipedia.org/wiki/Gauss%E2%80%93Hermite_quadrature
            # for details
            n = dist_params.get('n')
            try:
                gh.quad[n]
            except KeyError:
                print 'no table for quadrature of number {0} is available'.format(n)
                print 'available quadrature numbers:  {0}'.format(str(gh.quad.keys()))
            sigma = dist_params.get('sigma')
            self.zeta = np.array(gh.quad[n])[0]
            self.zweight = np.array(gh.quad[n])[1]
            self.dzeta = 1.
            # substitution to inhom variables yields the following scaling:
            self.zeta*= np.sqrt(2) * sigma
            self.zweight*= np.pi**-0.5
        else:
            self.zeta = np.array([0])
            self.zweight = [1.0]
            self.dzeta = 1.0
        self.inhom_sampling = inhom_sampling
        self.dist_params = dist_params