-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ipynb_tmp.py
176 lines (145 loc) · 5.53 KB
/
test.ipynb_tmp.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import numpy as np
from sympy import sqrt, sin, symbols, Function
from IPython.display import display
# F = ma = sum of:
# -friction [gamma]
# noise [xi]
# noise intensity [D]
# potential(x) [U]
# oscillating force amplitude [a]
# oscillating force(omega, t)
# constant force [f]
class Sde():
@staticmethod
def get_default_options():
return {
'simulation':{
'spp':100, # steps per period
'periods':2,#000, # number of periods in the simulation
'paths':2,#56, # number of paths to sample
'samples':100, # sample the position every N steps
'transients_number':200, # number of periods to ignore
'transients_fraction':0.1, # fraction of periods to ignore
'transients_type':'fraction', # periods to ignore because of transients (fraction, number)
'rng_seed':None,
'precision':'single', # precision of the floating-point numbers (single, double)
'rng_generator':'kiss32',
'deterministic':False, # do not generate any noises
},
'output':{
'mode':'summary', # output mode (summary, path)
'format':'text', # output file format (text, npy)
'destination':'./sde_out'
},
'gpu':{
'cuda':True,
},
'debug':{
'enabled':True,
}
}
def __init__(self, variables, parameters, functions, equation, options = get_default_options.__func__()):
self.variables = variables
self.parameters = parameters
self.functions = functions
self.equation = equation
self.options = options
self.name_to_sympy = {}
for p in self.parameters:
self.name_to_sympy[p.name] = p
for v in self.variables:
self.name_to_sympy[v.name] = v
self._init_cuda()
def _init_cuda(self):
if self.options['gpu']['cuda']:
import pycuda.driver as cuda
cuda.init()
def _validate(self):
for symbol in self.equation.free_symbols:
if symbol not in self.variables and symbol not in self.parameters:
raise ValueError('Value(s) for \'%s\' not provided.' % symbol.name)
# dt
self.name_to_sympy['dt'] = symbols('dt')
self.parameters[self.name_to_sympy['dt']] = 2*np.pi/self.parameters[self.name_to_sympy['omega']]
def _simulate(self):
if self.options['gpu']['cuda']:
self._simulate_gpu()
else:
self._simulate_cpu()
def _simulate_cpu(self):
current_values = self.variables.copy()
for path in range(self.options['simulation']['paths']):
dt = self.parameters[self.name_to_sympy['dt']]
for period in range(self.options['simulation']['periods']):
for step in range(self.options['simulation']['spp']):
result = self.equation.evalf(subs = current_values)
current_values[self.name_to_sympy['t']] += dt
current_values[self.name_to_sympy['v']] += result * dt
current_values[self.name_to_sympy['x']] += current_values[self.name_to_sympy['v']] * dt
def _simulate_gpu(self):
return
def start(self):
self._validate()
self._simulate()
# variables with initial values
var_x, var_v, var_t = symbols('x v t')
variables = {
var_x:0,
var_v:1,
var_t:0,
}
# parameters with values
par_gamma, par_amplitude, par_potential_aplitude, par_omega, par_const_force, par_noise_intensity \
= symbols('gamma a Delta_U omega f D')
parameters = {
par_gamma:1,
par_amplitude:1,
par_potential_aplitude:1,
par_omega:1,
# par_const_force:np.linspace(0, 1, 10),
par_const_force:1,
par_noise_intensity:1,
}
# functions
def oscillation():
return sin
def potential():
return sin
f_potential = Function('U')
f_oscillation = oscillation
f_noise = Function('xi')
functions = {f_potential, f_oscillation, f_noise}
# equation
equation = -par_gamma*var_v + par_potential_aplitude * f_potential(var_x) + par_amplitude * f_oscillation()(par_omega*var_t) \
+ par_const_force + sqrt(2*par_noise_intensity) * f_noise(var_t)
equation2 = -par_gamma*var_v + par_potential_aplitude * sin(var_x) + par_amplitude * sin(par_omega*var_t) \
+ par_const_force + sqrt(2*par_noise_intensity)
# runtime options
options = Sde.get_default_options()
options['gpu']['cuda'] = False
# SDE
SDE = Sde(variables, parameters, functions, equation2, options)
SDE.start()
from sympy import init_printing
init_printing()
display(equation)
display(equation.free_symbols)
print(f_potential)
print(variables)
display(equation.evalf())
display(equation.evalf(subs={par_const_force:1}))
print('\n--------------------\n')
equation2 = -par_gamma*var_v # + par_potential_aplitude * f_potential(var_x) + par_amplitude * f_oscillation(par_omega, var_t) + par_const_force + sqrt(2*par_noise_intensity) * f_noise(var_t)
display(equation2)
print(equation2.free_symbols)
display(equation2.evalf())
display(equation2.evalf(subs={'gamma':3}))
from sympy import Symbol, solve
aa = Symbol("a")
bb = Symbol("b")
cc = Symbol("c")
exp = (aa+bb)*40-(cc-aa)/0.5
print(exp)
print(exp.free_symbols)
print(solve(exp))
print(exp.evalf(subs={aa:0.2, 'b':0.05, 'c':0.7}))