Optimisation of For loop in nested loops in a MPI program - python

I have nested loops in the code attached. I want to optimise the interior most for loop (ks_div one).
from mpi4py import MPI
comm = MPI.COMM_WORLD
proc_id = comm.Get_rank()
n_procs = comm.Get_size()
import time
import numpy as np
from numpy.lib.scimath import sqrt
#import matplotlib.pyplot as plt
#from joblib import Parallel, delayed
delta = 10.0;
lund = 1e04;
alpha = 1e-7;
g = 10.0;
eta = 1.0;
VA = lund*eta/delta;
k0 = sqrt(6)/(1.0*delta);
Omega = VA*k0*50/(1.0*sqrt(2));
ratio = 500000.0;
beta = 1e05;#VA*VA*k0*k0/(g*alpha*ratio*ratio);
t0 = VA*VA/(1.0*g*alpha*beta*eta)
t_min = -8
t_max = 10
division =50
tym =np.logspace(t_min,t_max,division)
t11 = 4*Omega*Omega/(1.0*eta*VA**2*k0**4);
ks_max = 100.0/(1.0*delta);
kz_max = 100.0/(1.0*delta);
ks_min = 1e-10;
kz_min = 1e-10;
kz_div = 1000001
ks_div = 5001
div = ks_div*kz_div;
ks_inc = (ks_max-ks_min)/(1.0*ks_div)#increment in ks
kz_inc = (kz_max-kz_min)/(1.0*kz_div)#increment in kz
start = time.time()
work_size = (kz_div) // n_procs
extra_work = (kz_div) % n_procs
my_work = work_size + (1 if proc_id<extra_work else 0)
d_start = work_size * proc_id + (proc_id if proc_id<extra_work else extra_work)
d_end = d_start + my_work
for s in range(len(tym)):
norm = 0;
normk = 0;
normks = 0;
normkz = 0;
counter = 0;
for l in range(d_start, d_end):
for i in range(int(ks_div)):
ks1 = ks_min+i*ks_inc
kz1 = kz_min+l*kz_inc
ks = ks1 #np.ravel(ks1)
kz = kz1 #np.ravel(kz1) #, kz = np.meshgrid(ks_inc,kz_inc)
k = sqrt(ks**2+kz**2);
omgM = VA*kz;
omge = eta*k**2;
omgA = sqrt(g * alpha*beta)*ks/k;
omgO = Omega*kz/k;
a1 = 1j*1;
a2 = 2*omge;
a3 = -1j*(omgA**2+omge**2+2*omgM**2+4*omgO**2);
a4 = -2*omge*(omgA**2+omgM**2+4*omgO**2);
a5 = 1j*(omgM**4+omgA**2*(omge**2+omgM**2)+4*omge**2*omgO**2);
a6 = omgA**2*omge*omgM**2;
eqn =[a1,a2,a3,a4,a5,a6];
roots = np.roots(eqn);
l4 = roots[0];
l2 = roots[1];
l3 = roots[2];
l1 = roots[3];
l5 = roots[4];
t = tym[s]*t11
theta0hat = delta**3.0/(16.0*np.pi*(2*np.pi)**(1.0/2.0)) * np.exp(- (delta**2)*(ks**2 + kz**2)/8.0)
a11 = (alpha*g*ks/(1.0*k*k))*theta0hat;
a22 = 0;
a33 = -a11*(omgM*omgM+omgA*omgA+4*omgO*omgO);
a44 = a11*omgM*omgM*omge;
d1 = (l1-l2)*(l1-l3)*(l1-l4)*(l1-l5);
d2 = (l2-l1)*(l2-l3)*(l2-l4)*(l2-l5);
d3 = (l3-l1)*(l3-l2)*(l3-l4)*(l3-l5);
d4 = (l4-l1)*(l4-l3)*(l4-l2)*(l4-l5);
d5 = (l5-l1)*(l5-l3)*(l5-l2)*(l5-l4);
n11 = a44
n12 = -1j*a33*(l2+l3+l4+l5)
n13 = -a22*(l4*l5+l3*l4+l3*l5+l2*l3+l2*l4+l2*l5)
n14 = 1j*a11*(l2*l4*l5+l3*l4*l5+l2*l3*l4+l2*l3*l5)
n22 = -1j*a33*(l1+l3+l4+l5)
n23 = -a22*(l4*l5+l3*l4+l3*l5+l1*l3+l1*l4+l1*l5)
n24 = 1j*a11*(l1*l4*l5+l3*l4*l5+l1*l3*l4+l1*l3*l5)
n32 = -1j*a33*(l1+l2+l4+l5)
n33 = -a22*(l4*l5+l2*l4+l2*l5+l1*l2+l1*l4+l1*l5)
n34 = 1j*a11*(l1*l4*l5+l2*l4*l5+l1*l2*l4+l1*l2*l5)
n42 = -1j*a33*(l1+l3+l2+l5)
n43 = -a22*(l3*l5+l2*l3+l2*l5+l1*l2+l1*l3+l1*l5)
n44 = 1j*a11*(l1*l3*l5+l2*l3*l5+l1*l2*l3+l1*l2*l5)
n52 = -1j*a33*(l1+l3+l2+l4)
n53 = -a22*(l3*l4+l2*l3+l2*l4+l1*l2+l1*l3+l1*l4)
n54 = 1j*a11*(l1*l3*l4+l2*l3*l4+l1*l2*l3+l1*l2*l4)
D1 = (n11+n12+n13+n14)/(1.0*d1)
D2 = (n11+n22+n23+n24)/(1.0*d2)
D3 = (n11+n32+n33+n34)/(1.0*d3)
D4 = (n11+n42+n43+n44)/(1.0*d4)
D5 = (n11+n52+n53+n54)/(1.0*d5)
psihat = D5*np.exp(1j*l5*t)+D4*np.exp(1j*l4*t)+D2*np.exp(1j*l2*t)
#print(psihat)
func = psihat*sqrt(ks**2+kz**2)
func_ks = func*ks
func_kz = func*kz
func_k = func*sqrt(kz**2+ks**2)
norm += np.real(sqrt(func*np.conj(func)));
normk += np.real(sqrt(func_k*np.conj(func_k)));
normkz += np.real(sqrt(func_kz*np.conj(func_kz)));
normks += np.real(sqrt(func_ks*np.conj(func_ks)));
norm_reduced = comm.reduce(norm, op=MPI.SUM, root=0)
normk_reduced = comm.reduce(normk, op=MPI.SUM, root=0)
normks_reduced = comm.reduce(normks, op=MPI.SUM, root=0)
normkz_reduced = comm.reduce(normkz, op=MPI.SUM, root=0)
if proc_id == 0:
ksn = normks_reduced/norm_reduced
kzn = normkz_reduced/norm_reduced
kn = normk_reduced/norm_reduced
en = eta*(kn**2)
Mn = VA * kzn
An = sqrt(g* alpha * beta* ksn**2/(1.0*kn**2))
On = Omega*kzn/(1.0*kn)
aa1 = 1j*1;
aa2 = 2*en;
aa3 = -1j*(An**2+en**2+2*Mn**2+4*On**2);
aa4 = -2*en*(An**2+Mn**2+4*On**2);
aa5 = 1j*(Mn**4+An**2*(en**2+Mn**2)+4*en**2*On**2);
aa6 = An**2*en*Mn**2;
eqn1 =[aa1,aa2,aa3,aa4,aa5,aa6];
roots1 = np.roots(eqn1);
L1 = 2*On+1j*en*Mn**2/(4*On**2);
L3 = Mn*Mn/(2.0*On)+1j*en;
L2 = -2*On+1j*en*Mn*Mn/(4*On*On);
L4 = -Mn**2/(2.0*On)+1j*en;
L5 = 1j*An*An*en/(Mn*Mn);
ll4 = roots1[0];
ll2 = roots1[1];
ll3 = roots1[2];
ll1 = roots1[3];
ll5 = roots1[4];
f = open("rot_norm_OMEGA_m.txt","a")
f.write("%.12g %.12g %.12g %.12g %.12g\n"%(tym[s],On,Mn,An,en))
f.close()
p = open("rot_norm_imlambda_approx_m.txt","a")
p.write("%.12g %.12g %.12g %.12g %.12g\n"%(np.imag(L1),np.imag(L2),np.imag(L3),np.imag(L4),np.imag(L5)))
p.close()
s = open("rot_norm_relambda_approx_m.txt","a")
s.write("%.12g %.12g %.12g %.12g %.12g\n"%(np.real(L1),np.real(L2),np.real(L3),np.real(L4),np.real(L5)))
s.close()
q = open("rot_norm_imlambda_num_m.txt","a")
q.write("%.12g %.12g %.12g %.12g %.12g\n"%(np.imag(ll2),np.imag(ll4),np.imag(ll1),np.imag(ll3),np.imag(ll5)))
q.close()
q = open("rot_norm_relambda_num_m.txt","a")
q.write("%.12g %.12g %.12g %.12g %.12g\n"%(np.real(ll2),np.real(ll4),np.real(ll1),np.real(ll3),np.real(ll5)))
q.close()
print ('Completion Time: {:2f}'.format(time.time()-start))
I can't vectorise it since the number of points are quite large. I am using np.roots which also makes it difficult to use a vectorise form. Please have a look and help me out here. I am basically calculating a L2 norm. It is taking a quite lot of time. I had given this code on a machine with 1440 cores and it took about 20 hours to print just one time step.
Thank you.

Related

System of seven ODEs solve using solve_ivp or implement RK4

I'm trying solve a system of coupled ordinary differential equations, formed by 7 ODEs in python, using solve_ivp or either implement a fuction for RK4.
The general physical problem is as follows:
Cooling of photovoltaic modules with heat exchanger coupling to the module. In this way, the module generates electrical energy and thermal energy.
I have a polynomial function, G(t) = 9.8385e-13*t^4 - 1.82918e-8*t^3 + 5.991355e-05*t^2 + 2.312059e-1*t + 25, which works for an approximate range of 0 < t < 9000, which represents solar radiation as a function of time of day.
This function was obtained through a "polyfit" applied to real data (file upload here. Its a CSV - https://files.fm/u/9y4evkf6c).
This function is used as input for the ODEs, which represent an electrical and a thermal system as a function of time.
To solve the electrical model, I created some scripts that solve the diode equation for the photovoltaic module in question, and the output of this script is the photovoltaic power (called in the PPV thermal model) generated as a function of the module temperature and radiation. This script works great and solves part of my problem.
My difficulty lies in solving the equations of the thermal model, which receives as input parameters G(t) and PPV.
The equations result in this system:
System of EDOS
Labels:
Tvidro = Tglass = T1
Tcel = Tpv = T2
Ttedlar = T3
Tabs = Tabsorber = T4
Ttubo = Ttube = T5
Tfsai = Tfluid_out = T6
Tiso = Tinsulation = T7
Using method/function for RK4, the complete code is like this (you can go direct to part "#DEFINE MODEL EQUATIONS - ODES)" :
import numpy as np
import matplotlib.pyplot as plt
import csv
from numpy.polynomial.polynomial import polyval
############################################################
with open('directory of data called teste_dados_radiacao',"r") as i:
rawdata = list(csv.reader(i, delimiter = ";"))
exampledata = np.array(rawdata[1:], dtype=float)
xdata = exampledata[:,0]
ydata = exampledata[:,1]
curve = np.array(np.polyfit(xdata, ydata, 4))
rev_curve = np.array(list(reversed(curve)), dtype=float)
print(rev_curve)
#G_ajustado = polyval(xdata, rev_curve)
""" plt.plot(xdata, ydata, label = "dados experimentais")
plt.plot(xdata, model, label = "model")
plt.legend()
plt.show() """
#############################################################
#CONSTANTS
Tamb = 25 #°C #ambient temperatura
SIGMA = 5.67e-8 #W/m2K4
E_VIDRO = 0.90 #between 0.85 e 0.83 #nasrin2017 0.04
VENTO = 2 #m/s
T_GROUND = Tamb + 2 #°C
T_CEU = 0.00552*Tamb**1.5
Vf = 1 #m/s
Do = 10e-3 #m
Di = 8e-3 #m
NS = 6*10 #number of cells
T_F_ENT = 20 #°C
#INPUTS
Tcel = 25
Tv = 25
Tiso = 30
Av = 1.638*0.982
ALPHA_VIDRO = 0.9
L_VIDRO = 3e-3 #m
RHO_VIDRO = 2500 #kg/m3
M_VIDRO = Av*L_VIDRO*RHO_VIDRO #kg
CP_VIDRO = 500 #j/kgK
K_VIDRO = 2 #W/mK
TAU_VIDRO = 0.95
Pac = 0.85
H_CELL = 0.156 #m
A_CELL = NS*H_CELL**2
ALPHA_CELL = 0.9
L_CEL = 3e-3
RHO_CEL = 2330
M_CEL = A_CELL*L_CEL*RHO_CEL #kg - estimated
CP_CEL = 900 #J/kgK
K_CEL = 140 #W/mK
BETA_T = 0.43/100 # %/°C
N_ELE_REF = 0.1368 #13.68%
N_ELE = N_ELE_REF*(1 - BETA_T*(Tcel - 25)) #273 + 25 - tcel kelvin
A_tedlar = Av
L_TEDLAR = 0.33e-3
RHO_TEDLAR = 1500
M_TEDLAR = Av*L_TEDLAR*RHO_TEDLAR
CP_TEDLAR = 1090 #1090 OU 2090
K_TEDLAR = 0.35
ALPHA_TEDLAR = 0.34 #doc nasa ou zero
#parameters
RHO_ABS = 2700
A_ABS = Av
CP_ABS =900
L_ABS = 3e-3 #mm
M_ABS = A_ABS*RHO_ABS*L_ABS
K_ABS = 300
A_ABS_TUBO = 10*1.60*0.01+0.154*9*0.01
A_ABS_ISO = Av-A_ABS_TUBO
RHO_TUBO = 2700
CP_TUBO = 900
N_TUBOS = 10
L_TUBO = N_TUBOS*1.6
M_TUBO = RHO_TUBO*L_TUBO*(3.1415/4)*(Do**2 - Di**2)
K_TUBO = 300
A_TUBO_F = 0.387 #pi*Di*(L*10 VOLTAS + R(156MM)*9)
A_TUBO_ISO = 0.484 #pi*Do*(L*10 VOLTAS + R(156MM)*9)
A_ISO = Av
RHO_ISO = 50
L_ISO = 40e-3
M_ISO = A_ISO*RHO_ISO*L_ISO
CP_ISO = 670
K_ISO = 0.0375
E_ISO = 0.75 #ESTIMATED
RHO_FLUIDO = 997
M_FLUIDO = L_TUBO*(3.1415/4)*Di**2*RHO_FLUIDO
CP_FLUIDO = 4186 #j/kgK
MI_FLUIDO = 0.890e-3 #Pa*s ou N/m2 * s
K_FLUIDO = 0.607
M_PONTO = 0.05 #kg/s ou 0.5 kg/m3
#DIMENSIONLESS
Pr = CP_FLUIDO*MI_FLUIDO/K_FLUIDO #water 25°C
Re = RHO_FLUIDO*Vf*Di/MI_FLUIDO
if (Re<=2300):
Nuf = 4.364
else:
Nuf = 0.023*(Re**0.8)*(Pr*0.4)*Re
#COEFFICIENTS
h_rad_vidro_ceu = SIGMA*E_VIDRO*(Tv**2 - T_CEU)*(Tv + T_CEU)
h_conv_vidro_amb = 2.8 + 3*VENTO
h_conv_tubo_fluido = 0.5*30#Nuf
h_cond_vidro_cel = 1/((L_VIDRO/K_VIDRO) + (L_CEL/K_CEL))
h_cond_cel_tedlar = 1/((L_TEDLAR/K_TEDLAR) + (L_CEL/K_CEL))
h_cond_tedlar_abs = 1/((L_TEDLAR/K_TEDLAR) + (L_ABS/K_ABS))
h_cond_abs_tubo = 1/((L_TUBO/K_TUBO) + (L_ABS/K_ABS))
h_cond_abs_iso = 1/((L_ISO/K_ISO) + (L_ABS/K_ABS))
h_cond_tubo_iso = 1/((L_ISO/K_ISO) + (L_TUBO/K_TUBO))
h_conv_iso_amb = h_conv_vidro_amb
h_rad_iso_ground = SIGMA*E_ISO*(Tiso**2 - T_GROUND**2)*(Tiso + T_GROUND)
#GROUPS
A1 = (1/(M_VIDRO*CP_VIDRO))*(ALPHA_VIDRO*Av)#*G(t)) G_ajustado = polyval(dt,rev_curve)
A2 = (1/(M_VIDRO*CP_VIDRO))*(Av*(h_rad_vidro_ceu + h_conv_vidro_amb + h_cond_vidro_cel))
A3 = (1/(M_VIDRO*CP_VIDRO))*Av*h_cond_vidro_cel
A4 = (1/(M_VIDRO*CP_VIDRO))*Av*(h_conv_vidro_amb + h_rad_vidro_ceu)
A5 = (1/(M_CEL*CP_CEL))*(Pac*A_CELL*TAU_VIDRO*ALPHA_CELL) #*G(t)
A6 = -1*A5*N_ELE #*G(t)
A7 = (1/(M_CEL*CP_CEL))*A_CELL*h_cond_vidro_cel
A8 = (1/(M_CEL*CP_CEL))*A_CELL*(h_cond_vidro_cel + h_cond_cel_tedlar)
A9 = (1/(M_CEL*CP_CEL))*A_CELL*h_cond_cel_tedlar
A10 = (1/(M_TEDLAR*CP_TEDLAR))*A_tedlar*(1 - Pac)*TAU_VIDRO*ALPHA_TEDLAR#G(t)
A11 = (1/(M_TEDLAR*CP_TEDLAR))*A_tedlar*(h_cond_cel_tedlar + h_cond_tedlar_abs)
A12 = (1/(M_TEDLAR*CP_TEDLAR))*A_tedlar*h_cond_cel_tedlar
A13 = (1/(M_TEDLAR*CP_TEDLAR))*A_tedlar*h_cond_tedlar_abs
A14 = (1/(M_ABS*CP_ABS))*A_ABS*h_cond_tedlar_abs
A15 = (1/(M_ABS*CP_ABS))*(A_ABS*h_cond_tedlar_abs + A_ABS_TUBO*h_cond_abs_tubo + A_ABS_ISO*h_cond_abs_iso)
A16 = (1/(M_ABS*CP_ABS))*A_ABS_TUBO*h_cond_abs_tubo
A17 = (1/(M_ABS*CP_ABS))*A_ABS_ISO*h_cond_abs_iso
A18 = (1/(M_TUBO*CP_TUBO))*A_ABS_TUBO*h_cond_abs_tubo
A19 = (1/(M_TUBO*CP_TUBO))*(A_ABS_TUBO*h_cond_abs_tubo + A_TUBO_F*h_conv_tubo_fluido + A_TUBO_ISO*h_cond_tubo_iso)
A20 = (1/(M_TUBO*CP_TUBO))*A_TUBO_F*h_conv_tubo_fluido*0.5
A21 = (1/(M_TUBO*CP_TUBO))*A_TUBO_ISO*h_cond_tubo_iso
A22 = (1/(M_FLUIDO*CP_FLUIDO))*A_TUBO_F*h_conv_tubo_fluido
A23 = (1/(M_FLUIDO*CP_FLUIDO))*(A_TUBO_F*h_conv_tubo_fluido*0.5 + M_PONTO*CP_FLUIDO)
A24 = (1/(M_FLUIDO*CP_FLUIDO))*(T_F_ENT*(M_PONTO*CP_FLUIDO - h_conv_tubo_fluido*A_TUBO_F*0.5))
A25 = (1/(M_ISO*CP_ISO))*A_ABS_ISO*h_cond_abs_iso
A26 = (1/(M_ISO*CP_ISO))*(A_ABS_ISO*h_cond_abs_iso + A_TUBO_ISO*h_cond_tubo_iso + A_ISO*h_conv_iso_amb + A_ISO*h_rad_iso_ground)
A27 = (1/(M_ISO*CP_ISO))*A_TUBO_ISO*h_cond_tubo_iso
A28 = (1/(M_ISO*CP_ISO))*A_ISO*(h_conv_iso_amb*Tamb + h_rad_iso_ground*T_GROUND)
#DEFINE MODEL EQUATIONS - ODES - (GLASS, PV CELL, TEDLAR, ABSORBER, TUBE, FLUID, INSULATION) # dT1dt = A1*G_ajustado - A2*x[0] + A3*x[1] + A4 # dT2dt = A5*G_ajustado - A6*G_ajustado + A7*x[0] - A8*x[1] + A9*x[2]# dT3dt = A10*G_ajustado - A11*x[2] + A12*x[1] +A13*x[3]
def SysEdo(x, k):#tv-x[0] tcel-x[1] ttedlar-x[2] tabs-x[3] ttubo-x[4] tiso-x[5] tfs-x[6]
dT1dt = A1*polyval(k,rev_curve) - A2*x[0] + A3*x[1] + A4
dT2dt = A5*polyval(k,rev_curve) - A6*polyval(k,rev_curve) + A7*x[0] - A8*x[1] + A9*x[2]
dT3dt = A10*polyval(k,rev_curve) - A11*x[2] + A12*x[1] +A13*x[3]
dT4dt = A14*x[2] - A15*x[3] + A16*x[4] + A17*x[5]
dT5dt = A18*x[3] - A19*x[4] + A20*x[6] + A20*T_F_ENT + A21*x[5]
dT6dt = A22*x[4] - A23*x[6] + A24
dT7dt = A25*x[3] - A26*x[5] + A27*x[4] + A28
Tdot = np.array([dT1dt, dT2dt, dT3dt, dT4dt, dT5dt, dT6dt, dT7dt])
return Tdot
#RungeKutta4
def RK4(f, x0, t0, tf, dt):
t = np.arange(t0, tf, dt) #time vector
nt = t.size #lenght of time vector
nx = x0.size #length of state variables?
x = np.zeros((nx,nt)) #initialize 2D vector
x[:,0] = x0 #initial conditions
#RK4 constants
for k in range(nt-1):
k1 = dt*f(t[k], x[:,k],k)
k2 = dt*f(t[k] + dt/2, x[:,k] + k1/2, k)
k3 = dt*f(t[k] + dt/2, x[:,k] + k2/2, k)
k4 = dt*f(t[k] + dt, x[:,k] + k3, k)
dx = (k1 + 2*k2 + 2*k2 + k4)/6
x[:,k+1] = x[:,k] + dx
return x,t
#Define problems
f = lambda t, x, k : SysEdo(x, k)
#initial state - t0 is initial time - tf is final time - dt is time step
x0 = np.array([30, 30, 30, 30, 30, 30, 30])
t0 = 0
tf = 1000
dt = 1
#EDO SOLVE
x, t = RK4(f, x0, t0, tf, dt)
plt.figure()
plt.plot(t, x[0], '-', label='Tvidro')
"""
plt.plot(t, x[1], '-', label='Tpv')
plt.plot(t, x[2], '-', label='Ttedlar')
plt.plot(t, x[3], '-', label='Tabs')
plt.plot(t, x[4], '-', label='Tiso')
plt.plot(t, x[5], '-', label='Ttubo')
plt.plot(t, x[6], '-', label='Tfsai')"""
plt.title('Gráfico')
plt.legend(['Tvidro', 'Tpv', 'Ttedlar', 'Tabs', 'Tiso', 'Ttubo', 'Tfsai'], shadow=False)
plt.xlabel('t (s)')
plt.ylabel('Temperatura (°C)')
plt.xlim(0,20)
plt.ylim(0,150)
plt.grid('on')
plt.show()
Thank you in advance, I am also open to completely start the implementation from scratch if there is a better way to do this with python or matlab.
You can just replace
x, t = RK4(f, x0, t0, tf, dt)
with
t = arange(t0,tf+0.5*dt,dt)
res = solve_ivp(f,(t0,tf),x0,t_eval=t,args=(k,), method="DOP853", atol=1e-6,rtol=1e-8)
x = res.y[0]
Adapt the last 3 parameters to your liking.

MPC with python and Error ValueError: `f0` passed has more than 1 dimension

I wrote a MPC with Python and it worked before. After a long time I want to use it again but I got this Error
f0 passed has more than 1 dimension.
But I didn't change anything on my code. It is some kind of strange.
Here is my code:
import numpy as np
import numpy.linalg as npl
import matplotlib.pyplot as plt
from scipy.optimize import minimize
def mpcAugment(Am, Bm, Cm ):
"Function for Augmented Model"
nx, nu = Bm.shape
ny = Cm.shape[0]
A = np.zeros((nx+ny,nx+ny))
A[0:nx,0:nx] = Am
A[nx:nx+ny,0:nx] = Cm#Am
A[nx:nx+ny,nx:nx+ny] = np.eye(ny)
B = np.zeros((nx+ny,nu))
B[0:nx,:nu] = Bm
B[nx:nx+ny,:nu] = Cm#Bm
C = np.zeros((ny,nx+ny))
C[:ny,nx:nx+ny] = np.eye(ny)
return A, B, C
'Define Parameters'
k = 0.4
AICB = 153.8
mcp = 8.8e4
vamb1 = 30
vamb2 = 45
a = -k*AICB/mcp
b = -1/mcp
Ts = 20
VICBref = -5.0
Am = np.array([[1+Ts*a]])
Bm = np.array([[Ts*b]])
Gm = np.array([[-Ts*a]])
Cm = np.array([[1]])
A, B, C = mpcAugment(Am,Bm,Cm)
A, G, C = mpcAugment(Am,Gm,Cm)
nx, nu = B.shape
ny = C.shape[0]
nd = G.shape[1]
Np = 20
Nu = 5
F = np.zeros((Np*ny,nx))
PHI = np.zeros((Np*ny,Nu*nu))
PHIw = np.zeros((Np*ny,Np*nd))
for i in range(0,Np):
Ai = npl.matrix_power(A, i+1)
F[i*ny:(i+1)*ny,:] = C#Ai
for j in range(0, Nu):
if j <= i:
Aij = np.linalg.matrix_power(A, i-j)
PHI[i*ny:(i+1)*ny, j*nu:(j+1)*nu] = C#Aij#B
for j in range(0, Np):
if j <= i:
Aij = np.linalg.matrix_power(A, i-j)
PHIw[i*ny:(i+1)*ny, j*nd:(j+1)*nd] = C#Aij#G
umax = 3100
umin = 0
Q = np.eye(Np*ny)
R = 1e-2*np.eye(Nu*nu)
Rs = VICBref*np.ones((Np*ny,1))
Ainq = np.zeros((2*Nu*nu,Nu*nu))
binq = np.zeros((2*Nu*nu,1))
cinq = np.zeros((2*Nu*nu,1))
for i in range(0,Nu):
binq[i*nu:(i+1)*nu] = umax
binq[(i+Nu)*nu:(Nu+i+1)*nu] = 1
cinq[i*nu:(i+1)*nu] = 1
cinq[(i+Nu)*nu:(Nu+i+1)*nu] = -1
for j in range(0,i+1):
Ainq[i*nu:(i+1)*nu,j*nu:(j+1)*nu] = np.eye(nu)
Ainq[(i+Nu)*nu:(Nu+i+1)*nu,j*nu:(j+1)*nu] = np.eye(nu)
u0 = 0
def objective(du):
dU = np.array(du).reshape((len(du),1))
Y = F#x + PHI#dU + PHIw#w
return np.transpose((Rs-Y))#(Rs-Y)+np.transpose(dU)#R#(dU)
def constraint1(du):
dU = np.array(du).reshape((len(du),1))
return (binq - Ainq#dU - cinq*u0)[0]
#print(objective([1,1,1]))
ulim = (umin, umax)
bnds = np.kron(np.ones((Nu,1)),ulim)
#print(bnds)
Um = np.ones((nu*Nu,1))
Tsim = 5e4
time = np.arange(0,Tsim,Ts)
Nt = len(time)
xm = np.zeros((Nt,1))
um = np.zeros((Nt,nu))
ym = np.zeros((Nt,ny))
xm[0] = 0
ym[0] = Cm.dot(xm[0])
w = np.zeros((Np*nd,1))
print('Am = ',Am)
print('Bm = ',Bm)
print('Cm = ',Cm)
x = np.zeros((nx,1))
x[1] = xm[0]
vamb = vamb1
Vamb = np.zeros((Nt,1))
Ns = int(np.floor(Nt/2))
Vamb[0:Ns] = vamb1*np.ones((Ns,1))
Vamb[Ns:Nt] = vamb2*np.ones((Nt-Ns,1))
Vref = VICBref*np.ones((Nt,1))
con = {'type':'ineq','fun':constraint1}
for i in range(0,Nt-1):
sol = minimize(objective, Um, method = 'SLSQP',constraints = con)
if sol.success == False:
print('Error Cant solve problem')
exit()
Um = sol.x
um[i+1] = um[i] + Um[0]
u0 = um[i+1]
xm[i+1] = Am.dot(xm[i])+Bm.dot(um[i+1])+Gm.dot(Vamb[i])
ym[i+1] = Cm.dot(xm[i+1])
for j in range(0,Np):
if i+j < Nt:
Rs[j] = Vref[i+j]
w[j] = Vamb[i+j]-Vamb[i+j-1]
else:
Rs[j] = Vref[Nt-1]
w[j] = 0
x[0] = xm[i+1] - xm[i]
x[1] = xm[i+1]
print('Q = ',um[i+1],' , VICB = ',xm[i+1], ' vamb = ', Vamb[i])
hour = 60*60
plt.figure()
plt.subplot(2,1,1)
plt.plot(time/hour,ym)
plt.plot(time/hour,Vref,'--')
plt.xlabel('time(hours)')
plt.xlim([0, Tsim/hour])
plt.subplot(2,1,2)
plt.plot(time/hour,um)
plt.xlim([0, Tsim/hour])
plt.show()
It about a controller, which control the temperature of a cool box.
Is that possible that anything changed in main simply code?
I think the problem is now in minimizations part.
I reinstalled all of my libraries and it worked

Scipy.fmin_bfgs passing to many arguments to function

I am trying to program a neural network and was trying to minimize the cost function using scipy.optimize_bfgs() and after attempting to use this I get the error that "TypeError: cost() takes 3 positional arguments but 4 were given". Where are these four arguments coming from and how can I rectify this?
The cost function is defined by:
def cost(param,X,y):
Theta1 = np.reshape(param[0:106950:1],(75,1426))
Theta2 = np.reshape(param[106950:112650:1],(75,76))
Theta3 = np.reshape(param[112650::1],(1,76))
m = len(X)
J = 0
a1 = X
z2 = np.dot(a1,np.transpose(Theta1))
a2 = sigmoid(z2)
a2 = np.concatenate((np.ones((len(a2),1)),a2),axis=1)
z3 = np.dot(a2,Theta2.T)
a3 = sigmoid(z3)
a3 = np.concatenate((np.ones((len(a3),1)),a3),axis=1)
z4 = np.dot(a3,Theta3.T)
a4 = sigmoid(z4)
h = a4
##Calculate cost
J = np.sum(np.sum(np.multiply(-y,np.log(h)) - np.multiply((1-y),np.log(1-h))))/(2*m)
theta1_reg[:,0] = 0
theta2_reg[:,0] = 0
theta3_reg[:,0] = 0
Reg = (lamb/(2*m))*(np.sum(np.sum(np.square(theta1_reg)))+np.sum(np.sum(np.sqaure(theta2_reg)))+np.sum(np.sum(np.square(theta3_reg))))
J = J + Reg
return J
The gradient is then calculated with:
def grad(param,X,y):
Theta1 = np.reshape(param[0:106950:1],(75,1426))
Theta2 = np.reshape(param[106950:112650:1],(75,76))
Theta3 = np.reshape(param[112650::1],(1,76))
Theta1_grad = np.zeros(Theta1.shape)
Theta2_grad = np.zeros(Theta2.shape)
Theta3_grad = np.zeros(Theta3.shape)
m = len(X)
##Forward propogation
a1 = X
z2 = np.dot(a1,np.transpose(Theta1))
a2 = sigmoid(z2)
a2 = np.concatenate((np.ones((len(a2),1)),a2),axis=1)
z3 = np.dot(a2,Theta2.T)
a3 = sigmoid(z3)
a3 = np.concatenate((np.ones((len(a3),1)),a3),axis=1)
z4 = np.dot(a3,Theta3.T)
a4 = sigmoid(z4)
h = a4
##Backward propogation
d4 = a4 - y
d3 = np.multiply(np.dot(d4,Theta3[:,1:]),sigmoidGradient(z3))
d2 = np.multiply(np.dot(d3,Theta2[:,1:]),sigmoidGradient(z2)) ## or sigmoid(z2) .* ( 1 - sigmoid(z2))
D1 = np.dot(d2.T,a1)
D2 = np.dot(d3.T,a2)
D3 = np.dot(d4.T,a3)
##Unregularized gradients
Theta1_grad = (1/m)*D1
Theta2_grad = (1/m)*D2
Theta3_grad = (1/m)*D3
##Regularize gradients
theta1_reg = Theta1
theta2_reg = Theta2
theta3_reg = Theta3
theta1_reg[:,0] = 0
theta2_reg[:,0] = 0
theta3_reg[:,0] = 0
theta1_reg = (lamb/m)*theta1_reg
theta2_reg = (lamb/m)*theta2_reg
theta3_reg = (lamb/m)*theta3_reg
Theta1_grad = Theta1_grad + theta1_reg
Theta2_grad = Theta2_grad + theta2_reg
Theta3_grad = Theta3_grad + theta3_reg
##Concatenate gradients
grad = np.concatenate((Theta1_grad,Theta2_grad,Theta3_grad),axis=None)
return grad
Other functions defined are
def sigmoid(z):
sig = 1 / (1 + np.exp(z))
return sig
def randInitializeWeights(l_in, l_out):
epsilon = 0.12;
W = np.random.rand(l_out, 1+l_in)*2*epsilon - epsilon;
return W
def sigmoidGradient(z):
g = np.multiply(sigmoid(z),(1-sigmoid(z)))
return g
As an example:
import numpy as np
import scipy.optimize
X = np.random.rand(479,1426)
y1 = np.zeros((frames,1))
y2 = np.ones((framesp,1))
y = np.concatenate((y1,y2),axis=0)
init_param = np.random.rand(112726,)
lamb = 0.5
scipy.optimize.fmin_bfgs(cost,fprime=grad,x0=init_param,args=(param,X,y))
Then the error appears.
Thanks for any help
The arguments passed into the cost functions are the parameters, followed by the extra arguments. The parameters are chosen by the minimization function, the extra arguments are passed through.
When calling fmin_bfgs, only pass the extra arguments as args, not the actual parameters to optimize:
scipy.optimize.fmin_bfgs(..., args=(X,y))

python, matplotlib.animation: My 'planets' aren't rotating?

My planets are not rotating in my plot and im not entirely sure why?
As a side note, is there way to scale the planets radius properly with respect to the sun. And could the initial positions(x,y) be the aphelion distance from the sun? But no worries, no need to answer, just looking form some insight. Thank you.
from pylab import*
from matplotlib.animation import *
earth_radius = 6.3781e6#meters earth radius
suns_radius = 696e6#meters suns radius
mercury_radius = 2439.5e3# meters mercury radius
venus_radius = 6052e3 #meters venus radius
SunEarth_dist = 152e9 #distance from sun to earth approx. 1Au = 152e6km
SunMercury_dist = 69.8e9 #meters
SunVenus_dist = 108.9e9#meters
sun_mass = 1.988e30 #m1 : kg
mercury_mass = .330e24#m2: kg
venus_mass = 4.87e24 #m3 : kg
earth_mass = 5.97e24#m4 : kg
#radius of the planets scaled from the sun
r1 = suns_radius
r2 = mercury_radius
r3 = venus_radius
r4 = earth_radius
n = 10000#number of steps
dt = 10000#step size
G = 6.67384*10**(-11)#gravitational constant
def planets():
tmax = dt* n
t = 0
x = 0
#inital position of the planets
x1 = 3844e8*0.8*0
y1 = 3844e8*0.8*0
x2 = 3844e8*0.8
y2 = -3844e8*0.8*0
x3 = -3844e8*0.8
y3 = 3844e8*0.8*0
x4 = -3844e8*0.8*0
y4 = -3844e8*0.8
#intial velocity of each planet
Velocity_xS = 0
Velocity_yS = 0
Velocity_xM = 800
Velocity_yM= 1700
Velocity_xV = 0
Velocity_yV = -1500
Velocity_xE = 2000
Velocity_yE = 0
#distance between the planets
d12 = sqrt((x1-x2)**2+(y1-y2)**2)
d23 = sqrt((x2-x3)**2+(y2-y3)**2)
d13 = sqrt((x1-x3)**2+(y1-y3)**2)
d14 = sqrt((x1-x4)**2+(y1-y4)**2)
d24 = sqrt((x2-x4)**2+(y2-y4)**2)
d34 = sqrt((x3-x4)**2+(y3-y4)**2)
while t < tmax:
Fg12 = (G*sun_mass*mercury_mass)/d12**2
Fgx12 = -Fg12*((x1-x2))/d12
Fgy12 = -Fg12*((y1-y2))/d12
Fgx21 = -Fg12*((x2-x1))/d12
Fgy21 = -Fg12*((y2-y1))/d12
Fg13 = (G*sun_mass*venus_mass)/d13**2
Fgx13 = -Fg13*((x1-x3))/d13
Fgy13 = -Fg13*((y1-y3))/d13
Fgx31 = -Fg13*((x3-x1))/d13
Fgy31 = -Fg13*((y3-y1))/d13
Fg23 = (G*venus_mass*mercury_mass)/d23**2
Fgx23 = -Fg23*((x2-x3))/d23
Fgy23 = -Fg23*((y2-y3))/d23
Fgx32 = -Fg23*((x3-x2))/d23
Fgy32 = -Fg23*((y3-y2))/d23
Fg14 = (G*sun_mass*earth_mass)/d14**2
Fgx14 = -Fg14*((x1-x4))/d14
Fgy14 = -Fg14*((y1-y4))/d14
Fgx41 = -Fg14*((x4-x1))/d14
Fgy41 = -Fg14*((y4-y1))/d14
Fg24 = (G*sun_mass*earth_mass)/d24**2
Fgx24 = -Fg24*((x2-x4))/d24
Fgy24 = -Fg24*((y2-y4))/d24
Fgx42 = -Fg24*((x4-x2))/d24
Fgy42 = -Fg24*((x4-x2))/d24
Fg34 = (G*sun_mass*earth_mass)/d34**2
Fgx34 = -Fg34*((x3-x4))/d34
Fgy34 = -Fg34*((y3-y4))/d34
Fgx43 = -Fg34*((x4-x3))/d34
Fgy43 = -Fg34*((y4-y3))/d34
Acceleration_xS = Fgx12/sun_mass + Fgx13/sun_mass + Fgx14/sun_mass
Acceleration_yS = Fgy12/sun_mass + Fgy13/sun_mass + Fgy14/sun_mass
Acceleration_xM = Fgx21/mercury_mass + Fgx23/mercury_mass + Fgx24/mercury_mass
Acceleration_yM = Fgy21/mercury_mass + Fgy23/mercury_mass + Fgy24/mercury_mass
Acceleration_xV = Fgx32/venus_mass + Fgx31/venus_mass + Fgx34/venus_mass
Acceleration_yV = Fgy32/venus_mass + Fgy31/venus_mass + Fgy34/venus_mass
Acceleration_xE = Fgx41/earth_mass + Fgx42/earth_mass+ Fgx43/earth_mass
Acceleration_yE = Fgy41/earth_mass + Fgy42/earth_mass + Fgx43/earth_mass
Velocity_xS = Velocity_xS +Acceleration_xS*dt
Velocity_yS = Velocity_yS +Acceleration_yS*dt
Velocity_xM = Velocity_xM +Acceleration_xM*dt
Velocity_yM = Velocity_yM +Acceleration_yM*dt
Velocity_xV = Velocity_xV +Acceleration_xV*dt
Velocity_yV = Velocity_yV +Acceleration_yV*dt
Velocity_xE = Velocity_xE +Acceleration_xE*dt
Velocity_yE = Velocity_yE +Acceleration_yE*dt
#update the position of the planets
x1 = x1 + Velocity_xS*dt
y1 = y1 + Velocity_yS*dt
x2 = x2 + Velocity_xM*dt
y2 = y2 + Velocity_yM*dt
x3 = x3 + Velocity_xV*dt
y3 = y3 + Velocity_yV*dt
x4 = x4 + Velocity_xE*dt
y4 = y4 + Velocity_yE*dt
Sun.center = x1,y1
Mercury.center = x2,y2
Venus.center = x3,y3
Earth.center = x4,y4
d12 = sqrt((x1-x2)**2+(y1-y2)**2)
d23 = sqrt((x2-x3)**2+(y2-y3)**2)
d13 = sqrt((x1-x3)**2+(y1-y3)**2)
d14 = sqrt((x1-x4)**2+(y1-y4)**2)
d24 = sqrt((x2-x4)**2+(y2-y4)**2)
d34 = sqrt((x3-x4)**2+(y3-y4)**2)
t = t+dt
return x, t
def initial_points(planets):
x, t = planets[0], planets[1]
line.set_data(t, x)
ctr = Sun.center
ax.set_xlim(ctr[0]-5e12, ctr[0]+5e12)
ax.set_ylim(ctr[1]-5e12, ctr[1]+5e12)
return line
fig = plt.figure()
ax = plt.axes(xlim=(-5e12, 5e12), ylim=(-5e12, 5e12))
ax.set_aspect("equal")
line, = ax.plot([], [], '', ms=10)
Sun = Circle((0,0), r1, fc='yellow')
ax.add_artist(Sun)
Mercury = Circle((0 ,0), r2, fc='brown')
ax.add_artist(Mercury)
Venus = Circle(( 0,0), r3, fc='green')
ax.add_artist(Venus)
Earth = Circle((0,0), r4, fc='red')
ax.add_artist(Earth)
ani = FuncAnimation(fig, initial_points, planets, blit=False,\
interval=10, repeat=True)
plt.show()

RAM full in numpy sagemath

I wrote the next code. In 1-2 hours of execution time the RAM of my laptop (8gb) is filled and the sistem crash:
from scipy.stats import uniform
import numpy as np
cant_de_cadenas =[700,800,900]
cantidad_de_cadenas=np.array([])
for kkkkk in cant_de_cadenas:
cantidad_de_cadenas=np.append(cantidad_de_cadenas,kkkkk)
cantidad_de_cadenas=np.transpose(cantidad_de_cadenas)
b=10
h=b
Longitud=1
numero_experimentos=100
densidad_de_cadenas =cantidad_de_cadenas/(b**2)
prob_perc=np.array([])
tiempos=np.array([])
S_int=np.array([])
S_medio=np.array([])
desviacion_standard=np.array([])
desviacion_standard_nuevo=np.array([])
anisotropia_macroscopica_porcentual=np.array([])
componente_y=np.array([])
componente_x=np.array([])
import time
for N in cant_de_cadenas:
empieza=time.clock()
PERCOLACION=np.array([])
size_medio_intuitivo = np.array([])
size_medio_nuevo = np.array([])
std_dev_size_medio_intuitivo = np.array([])
std_dev_size_medio_nuevo = np.array([])
comp_y = np.array([])
comp_x = np.array([])
for u in xrange(numero_experimentos):
perco = False
array_x1=uniform.rvs(loc=-b/2, scale=b, size=N)
array_y1=uniform.rvs(loc=-h/2, scale=h, size=N)
array_angle=uniform.rvs(loc=-0.5*(np.pi), scale=np.pi, size=N)
array_pendiente_x=1./np.tan(array_angle)
random=uniform.rvs(loc=-1, scale=2, size=N)
lambda_sign=np.zeros([N])
for t in xrange(N):
if random[t]<0:
lambda_sign[t]=-1
else:
lambda_sign[t]=1
array_lambdas=(lambda_sign*Longitud)/np.sqrt(1+array_pendiente_x**2)
array_x2= array_x1 + array_lambdas*array_pendiente_x
array_y2= array_y1 + array_lambdas*1
array_x1 = np.append(array_x1, [-b/2, b/2, -b/2, -b/2])
array_y1 = np.append(array_y1, [-h/2, -h/2, -h/2, h/2])
array_x2 = np.append(array_x2, [-b/2, b/2, b/2, b/2])
array_y2 = np.append(array_y2, [h/2, h/2, -h/2, h/2])
M = np.zeros([N+4,N+4])
for j in xrange(N+4):
if j>0:
x_A1B1 = array_x2[j]-array_x1[j]
y_A1B1 = array_y2[j]-array_y1[j]
x_A1A2 = array_x1[0:j]-array_x1[j]
y_A1A2 = array_y1[0:j]-array_y1[j]
x_A2A1 = -1*x_A1A2
y_A2A1 = -1*y_A1A2
x_A2B2 = array_x2[0:j]-array_x1[0:j]
y_A2B2 = array_y2[0:j]-array_y1[0:j]
x_A1B2 = array_x2[0:j]-array_x1[j]
y_A1B2 = array_y2[0:j]-array_y1[j]
x_A2B1 = array_x2[j]-array_x1[0:j]
y_A2B1 = array_y2[j]-array_y1[0:j]
p1 = x_A1B1*y_A1A2 - y_A1B1*x_A1A2
p2 = x_A1B1*y_A1B2 - y_A1B1*x_A1B2
p3 = x_A2B2*y_A2B1 - y_A2B2*x_A2B1
p4 = x_A2B2*y_A2A1 - y_A2B2*x_A2A1
condicion_1=p1*p2
condicion_2=p3*p4
for k in xrange (j):
if condicion_1[k]<=0 and condicion_2[k]<=0:
M[j,k]=1
del condicion_1
del condicion_2
if j+1<N+4:
x_A1B1 = array_x2[j]-array_x1[j]
y_A1B1 = array_y2[j]-array_y1[j]
x_A1A2 = array_x1[j+1:]-array_x1[j]
y_A1A2 = array_y1[j+1:]-array_y1[j]
x_A2A1 = -1*x_A1A2
y_A2A1 = -1*y_A1A2
x_A2B2 = array_x2[j+1:]-array_x1[j+1:]
y_A2B2 = array_y2[j+1:]-array_y1[j+1:]
x_A1B2 = array_x2[j+1:]-array_x1[j]
y_A1B2 = array_y2[j+1:]-array_y1[j]
x_A2B1 = array_x2[j]-array_x1[j+1:]
y_A2B1 = array_y2[j]-array_y1[j+1:]
p1 = x_A1B1*y_A1A2 - y_A1B1*x_A1A2
p2 = x_A1B1*y_A1B2 - y_A1B1*x_A1B2
p3 = x_A2B2*y_A2B1 - y_A2B2*x_A2B1
p4 = x_A2B2*y_A2A1 - y_A2B2*x_A2A1
condicion_1=p1*p2
condicion_2=p3*p4
for k in xrange ((N+4)-j-1):
if condicion_1[k]<=0 and condicion_2[k]<=0:
M[j,k+j+1]=1
del condicion_1
del condicion_2
M[N,N+2]=0
M[N,N+3]=0
M[N+1,N+2]=0
M[N+1,N+3]=0
M[N+2,N]=0
M[N+2,N+1]=0
M[N+3,N]=0
M[N+3,N+1]=0
CD=np.array([])
POPOPO=[]
for g in xrange(N):
lala=0
r=False
while lala<=len(POPOPO)-1:
esta= g in POPOPO[lala]
if esta is True:
lala=len(POPOPO)
r=True
else:
lala=lala+1
if r is False:
L=np.array([g])
for s in xrange(N):
if M[g,s] != 0:
L=np.append(L,s)
x=0
while x<= N:
for l in xrange(N):
z= l in L
d=L[x]
if z is False and M[d,l] != 0:
L=np.append(L,l)
if x+1<len(L):
x+=1
else:
x=N+1.
q= len (L)
CD=np.append(CD, q)
POPOPO.append(L)
M_horizontal=M.copy()
M_horizontal[:,N+2] = np.zeros(N+4)
M_horizontal[:,N+3] = np.zeros(N+4)
M_horizontal[N+2] = np.zeros(N+4)
M_horizontal[N+3] = np.zeros(N+4)
L=np.array([N])
for s in xrange(N+4):
if M_horizontal[N,s] != 0:
L=np.append(L,s)
x=0
while x<= N+4:
for l in xrange(N+4):
z= l in L
d=L[x]
if z is False and M_horizontal[d,l] != 0:
L=np.append(L,l)
if x+1<len(L):
x+=1
else:
x=(N+4)+1.
LV1_in_L = N in L
LV2_in_L= (N+1) in L
if LV1_in_L is True and LV2_in_L is True:
perc_horiz=True
else:
perc_horiz=False
M_vertical=M.copy()
M_vertical[:,N] = np.zeros(N+4)
M_vertical[:,N+1] = np.zeros(N+4)
M_vertical[N] = np.zeros(N+4)
M_vertical[N+1] = np.zeros(N+4)
L=np.array([N+2])
for s in xrange(N+4):
if M_vertical[N+2,s] != 0:
L=np.append(L,s)
x=0
while x<= N+4:
for l in xrange(N+4):
z= l in L
d=L[x]
if z is False and M_vertical[d,l] != 0:
L=np.append(L,l)
if x+1<len(L):
x+=1
else:
x=(N+4)+1.
LH1_in_L = (N+2) in L
LH2_in_L= (N+3) in L
if LH1_in_L is True and LH2_in_L is True:
perc_ver = True
else:
perc_ver = False
if perc_ver is True or perc_horiz is True:
PERCOLACION=np.append(PERCOLACION,1)
perco=True
D = np.array([])
W = np.array([])
for c in xrange (int(min(CD)), int(max(CD)+1),1):
D=np.append(D,c)
frec = sum (CD == c)
W = np.append(W,frec)
if perco is True:
posicion=np.argmax(D)
D=np.delete(D,posicion)
W=np.delete(W,posicion)
if len(D) == 0 and len(W)==0:
S_medio_intuitivo_exp_u=0
S_medio_nuevo_exp_u = 0
std_dev_exp_u = 0
std_dev_nuevo_exp_u = 0
else:
S_medio_intuitivo_exp_u = np.average (D,weights=W)
peso_nuevo=D*W
S_medio_nuevo_exp_u = np.average (D,weights=peso_nuevo)
tipos=sum(W)
X=W*((D-S_medio_intuitivo_exp_u)**2)
S=sum(X)
std_dev_exp_u = np.sqrt(S/(tipos-1.))
tipos_nuevo=sum(peso_nuevo)
X_nuevo=peso_nuevo*((D-S_medio_nuevo_exp_u)**2)
S_nuevo=sum(X_nuevo)
std_dev_nuevo_exp_u = np.sqrt(S_nuevo/(tipos_nuevo-1.))
componente_longitudinal=Longitud*np.abs(np.cos(array_angle))
comp_y=np.append(comp_y, sum(componente_longitudinal)/N)
componente_transversal=Longitud*np.abs(np.sin(array_angle))
comp_x=np.append(comp_x, sum(componente_transversal)/N)
std_dev_size_medio_intuitivo=np.append(std_dev_size_medio_intuitivo, std_dev_exp_u)
std_dev_size_medio_nuevo=np.append(std_dev_size_medio_nuevo, std_dev_nuevo_exp_u)
size_medio_intuitivo=np.append(size_medio_intuitivo, S_medio_intuitivo_exp_u)
size_medio_nuevo=np.append(size_medio_nuevo, S_medio_nuevo_exp_u)
percolation_probability=sum(PERCOLACION)/numero_experimentos
prob_perc=np.append(prob_perc, percolation_probability)
S_int = np.append (S_int, sum(size_medio_intuitivo)/numero_experimentos)
S_medio=np.append (S_medio, sum(size_medio_nuevo)/numero_experimentos)
desviacion_standard = np.append (desviacion_standard, sum(std_dev_size_medio_intuitivo)/numero_experimentos)
desviacion_standard_nuevo=np.append (desviacion_standard_nuevo, sum(std_dev_size_medio_nuevo)/numero_experimentos)
tiempos=np.append(tiempos, time.clock()-empieza)
componente_y=np.append(componente_y, sum(comp_y)/numero_experimentos)
componente_x=np.append(componente_x, sum(comp_x)/numero_experimentos)
anisotropia_macroscopica_porcentual=100*(1-(componente_y/componente_x))
I tryed with gc and gc.collect() and 'del'command for deleting arrays after his use and nothing work!
What am I doing wrong? Why the memory becomes full while running (starts with 10% of RAM used and in 1-2hour is totally full used)?
Lets take an array M with size 300MB. When this is overwritten (for example, in each iteration), have we 300MB occupated in RAM memory or just 300MB? In the case that we have just 300MB, there should be no problem, so why have I this RAM issue? In the case of the RAM is acumulated, how can I do for free RAM memory occupated for the 'old' array?
Please help me, I'm totally stuck!
Thanks a lot!

Categories