-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnls.py
363 lines (289 loc) · 8.63 KB
/
dnls.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# comment# Programa DNLS
# Modulos and import
# Add Scrit
# DNLS.py + Funcion + nameArchivo
import sys
import random as rd
import matplotlib.pyplot as plt
import sys # libreria provee variables y funcionalidades relacionadas con el interprete
import numpy as np
from sympy import *
import cmath
import math
def RK4(h, X_00, F, nameArchivo, sigma):
s1 = 0.9
s2 = 4.0
error = 0.001
delta_t = h
paso = 0.3
X_N = X_00
x_aux = X_N
X_aux = X_N
X_1 = X_N
X_2 = X_N
K_1 = F(X_N)
k_1 = K_1
k_2 = K_1
k_3 = K_1
k_4 = K_1
K_2 = K_1
K_3 = K_1
K_4 = K_1
t = 0.0
arch = open(sigma, "w")
arch.write(str(t))
arch.write(" ")
arch.write(str(0.0))
arch.write("\n")
arch1 = open(nameArchivo, "w")
arch1.write(str(t))
arch1.write(" ")
# for i in range(len(X_N)):
# arch.write(str(1))
# arch.write(" ")
for i in range(len(X_N)):
arch1.write(str(X_N[i]))
arch1.write(" ")
arch1.write("\n")
while t <= 20:
K_1 = F(X_N) * delta_t
K_2 = F(X_N + K_1 / 2.0) * delta_t
K_3 = F(X_N + K_2 / 2.0) * delta_t
K_4 = F(X_N + K_3) * delta_t
X_1 = X_N + (K_1 + 2.0 * K_2 + 2.0 * K_3 + K_4) / 6.0 # siguiente paso no ms
K_1 = F(X_N) * delta_t / 2.0
K_2 = F(X_N + K_1 / 2.0) * delta_t / 2.0
K_3 = F(X_N + K_2 / 2.0) * delta_t / 2.0
K_4 = F(X_N + K_3) * delta_t / 2.0
X_aux = X_N + (K_1 + 2.0 * K_2 + 2.0 * K_3 + K_4) * (1 / 6.0)
K_1 = F(X_aux) * delta_t # aqui modifique den frac de 2 a 1
K_2 = F(X_aux + K_1 / 2.0) * delta_t
K_3 = F(X_aux + K_2 / 2.0) * delta_t
K_4 = F(X_aux + K_3) * delta_t
X_2 = X_aux + (K_1 + 2.0 * K_2 + 2.0 * K_3 + K_4) / 6.0
K_1 = F(X_N) * delta_t
K_2 = F(X_N + 0.5 * K_1) * delta_t
K_3 = F(X_N + 0.5 * K_2) * delta_t
K_4 = F(X_N + K_3) * delta_t
final = X_N + (K_1 + 2.0 * K_2 + 2.0 * K_3 + K_4) / 6.0
X_N = final
t = t + delta_t
if t >= paso:
arch.write(str(t))
arch.write(" ")
arch1.write(str(t))
arch1.write(" ")
sumanum = 0
sumaden = 0
for l in range(len(X_N)):
arch1.write(str(np.absolute(X_N[l] ** 2)))
arch1.write(" ")
num = l * l * (np.absolute(X_N[l]) ** 2)
den = np.absolute(X_N[l]) ** 2
sumanum = sumanum + num
sumaden = sumaden + den
res = sumanum / sumaden
arch.write(str(res))
# arch.write(" ")
arch.write("\n")
arch1.write("\n")
paso = paso + h
else:
continue
# print(t)
return X_N
def potential(x):
a = rd.random()
b = rd.random()
out = a - b
return out
def DLS(x):
v = 1.0 + 0.0j
e = 0
z = complex(0, 1)
aux = np.zeros(101, dtype=complex)
for i in range(len(x)):
if i == 0:
aux[i] = z * (v * (x[i]) + e * x[i])
elif i == (len(x) - 1):
aux[i] = z * (v * (x[i]) + e * x[i])
elif i != (len(x) - 1) and i != 0:
aux[i] = z * (v * (x[i + 1] + x[i - 1]) + e * x[i])
# print(str(aux))
return aux
def DLSdisorder(x):
v = 1.0 + 0.0j
e = 0
z = complex(0, 1)
aux = np.zeros(101, dtype=complex)
for i in range(len(x)):
if i == 0:
aux[i] = z * (v * (x[i]) + e * x[i])
elif i == (len(x) - 1):
aux[i] = z * (v * (x[i]) + e * x[i])
elif i != (len(x) - 1) and i != 0:
aux[i] = z * (v * (x[i + 1] + x[i - 1]) + e * x[i])
# print(str(aux))
return aux
def DNLS(x):
v = 1.0 + 0.0j
e = 0
z = complex(0, 1)
k = [0, 1, 2, 3, 4, 5, 8, 10]
aux = np.zeros(101, dtype=complex)
for i in range(len(x)):
if i == 0:
aux[i] = z * (
v * (x[i]) + e * x[i] + k[3] * x[i] * (np.absolute(x[i]) ** 2)
)
elif i == (len(x) - 1):
aux[i] = z * (
v * (x[i]) + e * x[i] + k[3] * x[i] * (np.absolute(x[i]) ** 2)
)
elif i != (len(x) - 1) and i != 0:
aux[i] = z * (
v * (x[i + 1] + x[i - 1])
+ e * x[i]
+ k[3] * x[i] * (np.absolute(x[i]) ** 2)
)
# print(str(aux))
return aux
def DNLSLambda(x, params, backend=math): # implementar sympy
z = complex(0, 1)
e = params[0]
v = params[1]
k = params[2]
lam = params[3]
aux = np.zeros(101, dtype=complex)
for i in range(len(x)):
if i == 0:
aux[i] = z * (
v * (x[i]) + e * x[i] + k[3] * x[i] * (np.absolute(x[i]) ** 2)
)
elif i == (len(x) - 1):
aux[i] = z * (
v * (x[i]) + e * x[i] + k[3] * x[i] * (np.absolute(x[i]) ** 2)
)
elif i != (len(x) - 1) and i != 0:
aux[i] = z * (
v * (x[i + 1] + x[i - 1])
+ e * x[i]
+ k[3] * x[i] * (np.absolute(x[i]) ** 2)
)
# print(str(aux))
return aux
def DNLSdisorder(x):
v = 1.0 + 0.0j
e = 0
z = complex(0, 1)
k = [0, 1, 2, 3, 4, 5, 8, 10]
lam = 9.3
aux = np.zeros(101, dtype=complex)
for i in range(len(x)):
if i == 0:
aux[i] = z * (
v * (x[i]) + e * x[i] + k[3] * x[i] * (np.absolute(x[i]) ** 2)
)
elif i == (len(x) - 1):
aux[i] = z * (
v * (x[i]) + e * x[i] + k[3] * x[i] * (np.absolute(x[i]) ** 2)
)
elif i != (len(x) - 1) and i != 0:
aux[i] = z * (
v * (x[i + 1] + x[i - 1])
+ e * x[i]
+ k[3] * x[i] * (np.absolute(x[i]) ** 2)
)
# print(str(aux))
return aux
def mDNLS(x):
v = 1.0 + 0.0j
e = 0
z = complex(0, 1)
k = [0, 1, 2, 3, 4, 5, 8, 10]
aux = np.zeros(101, dtype=complex)
for i in range(len(x)):
if i == 0:
aux[i] = z * (
v * (x[i])
+ e * x[i]
+ k[3]
* x[i]
* (2 * np.absolute(x[i]) ** 2 + np.absolute(x[i + 1]) ** 2)
)
elif i == (len(x) - 1):
aux[i] = z * (
v * (x[i])
+ e * x[i]
+ k[3] * x[i] * (2 * np.absolute(x[i]) + np.absolute(x[i - 1]) ** 2)
)
elif i != (len(x) - 1) and i != 0:
aux[i] = z * (
v * (x[i + 1] + x[i - 1])
+ e * x[i]
+ k[3]
* x[i]
* (
np.absolute(x[i + 1]) ** 2
+ 2 * np.absolute(x[i]) ** 2
+ np.absolute(x[i - 1]) ** 2
)
)
# print(str(aux))
return aux
def mDNLSlambda(x, params, backend=math):
z = complex(0, 1)
e = params[0]
v = params[1]
k = params[2]
lam = params[3]
aux = np.zeros(101, dtype=complex)
for i in range(len(x)):
if i == 0:
aux[i] = z * (
-lam * x[i]
+ v * (x[i])
+ e * x[i]
+ k[3]
* x[i]
* (2 * np.absolute(x[i]) ** 2 + np.absolute(x[i + 1]) ** 2)
)
# aux[i]=symbols('aux[i]')
elif i == (len(x) - 1):
aux[i] = z * (
-lam * x[i]
+ v * (x[i])
+ e * x[i]
+ k[3] * x[i] * (2 * np.absolute(x[i]) + np.absolute(x[i - 1]) ** 2)
)
elif i != (len(x) - 1) and i != 0:
aux[i] = z * (
-lam * x[i]
+ v * (x[i + 1] + x[i - 1])
+ e * x[i]
+ k[3]
* x[i]
* (
np.absolute(x[i + 1]) ** 2
+ 2 * np.absolute(x[i]) ** 2
+ np.absolute(x[i - 1]) ** 2
)
)
# print(str(aux))
return aux
def condInitial(values=[1], site=[50], long=101):
x0 = np.zeros(long)
for i in site:
x0[i] = values[site.index(i)]
return x0
# parameters RK4
h = 0.01
t = 0
C0 = condInitial(values=[1, 12], site=[5, 6], long=10)
print(C0)
# p=x0.copy()
# varDNlS= RK4(h ,p, DNLSdisorder, 'DNLSdisorder.txt', 'dls24.txt');
# varmDNlS= RK4(h ,p, mDNLS, 'mDNLS.txt', 'sigmamDNLS.txt');
# var= RK4(h ,p, sys.argv[1], sys.arg[2] , 'sigma.txt');
print("\n *** Programa Finalizado *** \n ")
# puede ser mas adecuado una clase de funciones DNLS