I wrote a neural network and after that, i want to write neural network bigger than it, and i dont want to write too big code
input[0] = (player_x - Game_logic.block_1x)
input[1] = (player_y - Game_logic.block_1y)
input[2] = (player_x - Game_logic.block_2x)
input[3] = (player_y - Game_logic.block_2y)
input[4] = (player_x - Game_logic.block_3x)
input[5] = (player_y - Game_logic.block_3y)
snk[0] = (((input[0] * weights[0])/10) + ((input[1] * weights[1])/10) + ((input[2] * weights[2])/10) + ((input[3] * weights[3])/10) + ((input[4] * weights[4])/10) + ((input[5] * weights[5])/10) +1)
snk[1] = (((input[0] * weights[6])/10) + ((input[1] * weights[7])/10) + ((input[2] * weights[8])/10) + ((input[3] * weights[9])/10) + ((input[4] * weights[10])/10) + ((input[5] * weights[11])/10) +1)
snk[2] = (((input[0] * weights[12])/10) + ((input[1] * weights[13])/10) + ((input[2] * weights[14])/10) + ((input[3] * weights[15])/10) + ((input[4] * weights[16])/10) + ((input[5] * weights[17])/10) +1)
snk[3] = (((input[0] * weights[18])/10) + ((input[1] * weights[19])/10) + ((input[2] * weights[20])/10) + ((input[3] * weights[21])/10) + ((input[4] * weights[22])/10) + ((input[5] * weights[23])/10) +1)
snk[4] = (((input[0] * weights[24])/10) + ((input[1] * weights[25])/10) + ((input[2] * weights[26])/10) + ((input[3] * weights[27])/10) + ((input[4] * weights[28])/10) + ((input[5] * weights[29])/10) +1)
output[0] = ((snake_layer_1[0] * weights[30])/10) + ((snake_layer_1[1] * weights[31])/10) + ((snake_layer_1[2] * weights[32])/10) + ((snake_layer_1[3] * weights[33])/10) + ((snake_layer_1[4] * weights[34])/10)
output[1] = ((snake_layer_1[0] * weights[35])/10) + ((snake_layer_1[1] * weights[36])/10) + ((snake_layer_1[2] * weights[37])/10) + ((snake_layer_1[3] * weights[38])/10) + ((snake_layer_1[4] * weights[39])/10)
output[3] = ((snake_layer_1[0] * weights[40])/10) + ((snake_layer_1[1] * weights[41])/10) + ((snake_layer_1[2] * weights[42])/10) + ((snake_layer_1[3] * weights[43])/10) + ((snake_layer_1[4] * weights[44])/10)
Related
I am trying to print this following pattern , But not able to frame logic
My code :
for i in range(1,row+1):
if i == 1:
print(row * '* ')
elif i<row:
print( '* ' + ((row - 3) * 2) * ' ' + '*')
row = row - 1
else:
print('*')
Expected output :
* * * * * * * *
* *
* *
* *
* *
* *
* *
*
But my code gives me abnormal output :
* * * * * * * *
* *
* *
* *
*
*
*
*
#stacker's answer is nifty but mathematically a little overkill. This should do the trick just as well:
row = 8
print(row * '* ')
for i in range(1,row - 1):
rowlength = (row - i) * 2 - 3
print('*', end='')
print(rowlength * ' ', end='')
print('*')
print('*')
import math
row = 8;
for i in range(1,row+1):
if i == 1:
print(row * '* ')
elif i<(row * row) / (math.pi / math.sqrt(7)):
print( '* ' + ((row - 3) * 2) * ' ' + '*')
row = row - 1
else:
print('*')
Output:
* * * * * * * *
* *
* *
* *
* *
* *
* *
*
row=10
for i in range(1,row):
if i == 1:
print(row * '* ')
elif i < row:
print('* ' + (row-2)*2 * ' ' + '*')
row = row-1
elif i > row-2:
print('* ' + (row - 2) * 2 * ' ' + '*')
row = row - 1
Output:
* * * * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* *
Process finished with exit code 0
Hope this helps
I'm currently having some issue regarding accessing indices in Gekko array. I'm trying to convert following code:
m = GEKKO()
s42 = m.Array(m.Var, 8)
s44 = m.Array(m.Var, 8)
m.Equation(s42[0] == p42_raw[0] / (n34 + n44) * (n34 * (p34_norm[0] + p34_norm[4]) + n44 * (s44[0] + s44[4]) / np.sum(s44)))
m.Equation(s42[1] == p42_raw[1] / (n34 + n44) * (n34 * (p34_norm[0] + p34_norm[4]) + n44 * (s44[0] + s44[4]) / np.sum(s44)))
m.Equation(s42[2] == p42_raw[2] / (n34 + n44) * (n34 * (p34_norm[1] + p34_norm[5]) + n44 * (s44[1] + s44[5]) / np.sum(s44)))
m.Equation(s42[3] == p42_raw[3] / (n34 + n44) * (n34 * (p34_norm[1] + p34_norm[5]) + n44 * (s44[1] + s44[5]) / np.sum(s44)))
m.Equation(s42[4] == p42_raw[4] / (n34 + n44) * (n34 * (p34_norm[2] + p34_norm[6]) + n44 * (s44[2] + s44[6]) / np.sum(s44)))
m.Equation(s42[5] == p42_raw[5] / (n34 + n44) * (n34 * (p34_norm[2] + p34_norm[6]) + n44 * (s44[2] + s44[6]) / np.sum(s44)))
m.Equation(s42[6] == p42_raw[6] / (n34 + n44) * (n34 * (p34_norm[3] + p34_norm[7]) + n44 * (s44[3] + s44[7]) / np.sum(s44)))
m.Equation(s42[7] == p42_raw[7] / (n34 + n44) * (n34 * (p34_norm[3] + p34_norm[7]) + n44 * (s44[3] + s44[7]) / np.sum(s44)))
...
to something shorter like below:
m.Equation([s42[i] for i in range(8)] ==
[p42_raw[i] / (n34 + n44) * (n34 * (p34_norm[np.uint(i / 2)] + p34_norm[np.uint(4 + i / 2)])
+ n44 * (s44[np.uint(i / 2)] + s44[np.uint(4 + i / 2)]) / sum(s44))
for i in range(8)])
but currently getting following errors:
File "/Users/tomino/opt/anaconda3/lib/python3.7/site-packages/gekko/gk_operators.py", line 144, in __len__
return len(self.value)
TypeError: object of type 'int' has no len()
I've been stuck in this problem for a while but cannot find any suitable solution yet. Can anyone please help me resolving this issue?
Isn't this what you're after?
for i in range(8):
h = i // 2
m.Equation(s42[i] == p42_raw[i] / (n34 + n44) * (n34 * (p34_norm[h] + p34_norm[h+4]) + n44 * (s44[h] + s44[h+4]) / np.sum(s44)))
I am trying to solve a PDE using odeint and the method of lines. My code is definitely wrong - and I'm trying to figure out where it is going wrong.
I am calling the ode solver using odeint(odefunc,y0,tspan) where tspan = np.linspace(0.0, 0.5, 5) & y0 = 1.0*np.ones(3).
I tried printing t within odefunc and am confused by the output. Despite the fact that I am solving up to t=0.5, the last t-value to print is 0.015081203121127767. The number of outputs matches tspan, but I cannot see how it could possibly be solving up to t = 0.5 when the last time in the de function is 0.015. What am I missing?
My DE is time dependent - so this is making it very hard to figure out where things are going wrong because I don't seem to be seeing the times where everything fails.
ETA - this is failing, but running this without some of the irrelevant stuff I am getting the warning ODEintWarning: Excess work done on this call (perhaps wrong Dfun type). Run with full_output = 1 to get quantitative information., which I'm assuming is part of the issue - but it doesn't appear to be halting the code.
MWE
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import math
import sys
plt.interactive(False)
sigma = 2320
rho = 1000
gravity = 9.81 # [m/s^2]
g = gravity*3600*3600 # [m/hour^2]
S = 0.01
settlingVelocity = 0.02 # [m/s]
ws = settlingVelocity*3600 # [m/hour]
n = 0.04 # [SI]
J = 400 # [Ws/m]
k = 0.02
Cstar = 0.2 * sigma # [kg/m^3]
W = 2 # [m]
D0 = 1.2
Lw = 20
L = 100
tend = 0.5 # in hours
tspan = np.linspace(0.0, tend, 5)
def d(t): # metres
if t < 50: # hours
return 0.5
else:
return 0.05
def Q(t):
return 3600 * (math.sqrt(S)/n)*((W*d(t))**(5/3))/((2*d(t) + W)**(2/3))
def h(t):
return d(t)/2
def beta(t):
return (sigma - rho) * g * h(t)/sigma
def Omega(t):
return rho * g * S * Q(t) # [W/m]
def PsiTime(t):
return rho * g * Q(t) * (D0 - d(t))/(Lw)
N = 10
X = np.linspace(0, L, N)
delX = L/ (N-1)
def odefunc(y, t):
def zetaEh(t):
return k * (PsiTime(t) + Omega(t)) / (J + beta(t))
def zetaEW(t):
return (2*d(t)/(W + 2*d(t))) * k * Omega(t)/(J + beta(t))
def zetaR(t):
return (W/(W + 2*d(t))) * k*Omega(t)/(beta(t))
def zetaEF(t,i):
return (W/(W + 2*d(t))) * k * Omega(t) / (J + beta(t))
C = y[:N]
M = y[N:]
print("time: ", t)
dCdt = np.zeros(X.shape)
dMdt = np.zeros(X.shape)
dCdt[0] = ( # forward difference for dCdx
-Q(t) / (W*d(t)) * (C[1] - C[0]) / delX
+ (zetaEh(t) / (W * d(t))) * ((Cstar - C[0]) / Cstar)
- (ws * C[0] * (beta(t))) / (d(t) * (J + beta(t)))
)
dMdt[0] = 0
# gully channel
for i in range (1, N-1): # central difference
if M[i] + W *C[i] * ws - zetaR(t) * (Cstar - C[i]) / Cstar < 0:
reMass = M[i] + W * C[i] * ws
dCdt[i] = (
-Q(t) / (W*d(t)) * (C[i+1] - C[i - 1]) / (2*delX)
+ 1 / (W * d(t)) * ((zetaEW(t) + zetaEF(t,i)) * (Cstar - C[i]) / Cstar
+ reMass * (1 - (beta(t))/ (J + beta(t))))
- C[i] * ws/d(t)
)
dMdt[i] = -M[i]
else:
dCdt[i] = (
-Q(t) / (W*d(t)) * (C[i+1] - C[i - 1]) / (2*delX)
+ 1 / (W * d(t)) * (zetaEW(t) + zetaR(t)) * (Cstar - C[i]) / Cstar
- C[i] * ws / d(t)
)
dMdt[i] = W * C[i] * ws - zetaR(t) * (Cstar - C[i]) / Cstar
# Final node - backward difference
if M[N-1] + W * C[N-1] * ws - zetaR(t) * (Cstar - C[N-1]) / Cstar < 0:
reMass = M[N-1] + W * C[N-1] * ws
dCdt[N-1] = (
-Q(t) / (W * d(t)) * (C[N-1] - C[N-2]) / delX
+ 1 / (W * d(t)) * ((zetaEW(t) + zetaEF(t, i)) * (Cstar - C[N-1]) / Cstar
+ reMass * (1 - (beta(t)) / (J + beta(t))))
- C[i] * ws / d(t)
)
dMdt[N-1] = -M[N-1]
else:
dCdt[N-1] = (
-Q(t) / (W * d(t)) * (C[N-2] - C[N - 1]) / delX
+ 1 / (W * d(t)) * (zetaEW(t) + zetaR(t)) * (Cstar - C[N-1]) / Cstar
- C[N-1] * ws / d(t)
)
dMdt[N-1] = W * C[N-1] * ws - zetaR(t) * (Cstar - C[N-1]) / Cstar
dydt = np.ravel([dCdt, dMdt])
return dydt
init_C = 0.0 * np.ones(X.shape)
init_M = 0.0 * np.ones(X.shape)
init= np.ravel([init_C, init_M])
sol = odeint(odefunc, init, tspan)
conc = sol[:, :N]
I have tried to build a code using GEKKO following the answers found in this community, but I was not able to solve my problem. It is a function G(T), which should be solved for every component of a vector T. Is the error in the part of m.Obj?
The error and the code are below:
File "C:/Users/Roberto/PycharmProjects/Testes iniciais/Dry reforming - isothermic.py", line 119, in <module>
m.solve()
File "C:\Users\Roberto\PycharmProjects\Testes iniciais\venv\lib\site-packages\gekko\gekko.py", line 1882, in solve
raise Exception(response)
Exception: #error: Equation Definition
Warning: there is insufficient data in CSV file 1
51.21.105.203_gk_model0.csv
#error: Equation Definition
Equation without an equality (=) or inequality (>,<)
true
Equation without an equality (=) or inequality (>,<)
STOPPING...
true
STOPPING...
Process finished with exit code 1.
I just need to know the origin of the error message.
# Equilibrium calculations - Isothermal conditions
# library with mathematical package
import math
import numpy as np
# ############ Substances ID:
##########################################
# A=methane B=oxygen
# C=hydrogen D=carbon monoxide
# E=carbon dioxide F=water
# G=carbon H=Nitrogen
# I=argon
# ############ Thermodynamic data
##########################################
T0 = 298.15 #[K]
R = 8.134 #[J/mol.K]
p = 1 #[atm]
# A) Standard Enthalpy (hi0) [J/mol] and Entropy (si0) [J/mol] at T0=298.15K
# Source: [Perry's Chemical, Engineers' Handbook seventh edition, Mc Graw-Hill, 1997]
hA0 = -74520; sA0 = 1.8627*10**2
hB0 = 0; sB0 = 2.05149*10**2
hC0 = 0; sC0 = 1.3057*10**2
hD0 = -110530; sD0 = 1.9756*10**2
hE0 = -393510; sE0 = 2.1368*10**2
hF0 = -241814; sF0 = 1.8872*10**2
hG0 = 0; sG0 = 5.734
hH0 = 0; sH0 = 1.91610*10**2
#hI0 = 0; sI0 = 1.5484*10**2
# B) Ideal gas heat capacity polynomial: Coefficients for Cp in [J/mol.K]
# Source: [Chemical thermodynamics for process simulation, Appendix G, pg:711]
C1A = 19.238; C2A = 52.090*10**-3; C3A = 11.966*10**-6; C4A = -11.309*10**-9
C1B = 28.087; C2B = -0.004*10**-3; C3B = 17.447*10**-6; C4B = -10.644*10**-9
C1C = 27.124; C2C = 9.2670*10**-3; C3C = -13.799*10**-6; C4C = 7.64000*10**-9
C1D = 30.848; C2D = -12.84*10**-3; C3D = 27.870*10**-6; C4D = -12.710*10**-9
C1E = 19.780; C2E = 73.390*10**-3; C3E = -55.98*10**-6; C4E = 17.1400*10**-9
C1F = 32.220; C2F = 1.9225*10**-3; C3F = 10.548*10**-6; C4F = -3.5940*10**-9
C1G = -5.416; C2G = 58.981*10**-3; C3G = -43.559*10**-6; C4G = 11.604*10**-9
C1H = 31.128; C2H = -13.556*10**-3; C3H = 26.777*10**-6; C4H = -11.673*10**-9
# ####### Thermodynamic Calculations #####################################
# A) Standard Enthalpy (hi0), Entropy (si0) and Gibbs free molar energy (mi) at T0 [J/mol]
T = np.arange(50, 1000, 50)
for T in T:
hA = hA0 + C1A*(T - T0) + (C2A/2) * (T**2 - T0**2) + (C3A/3) * (T**3 - T0**3) + (C4A/4) * (T**4 - T0**4)
hB = hB0 + C1B*(T - T0) + (C2B/2) * (T**2 - T0**2) + (C3B/3) * (T**3 - T0**3) + (C4B/4) * (T**4 - T0**4)
hC = hC0 + C1C*(T - T0) + (C2C/2) * (T**2 - T0**2) + (C3C/3) * (T**3 - T0**3) + (C4C/4) * (T**4 - T0**4)
hD = hD0 + C1D*(T - T0) + (C2D/2) * (T**2 - T0**2) + (C3D/3) * (T**3 - T0**3) + (C4D/4) * (T**4 - T0**4)
hE = hE0 + C1E*(T - T0) + (C2E/2) * (T**2 - T0**2) + (C3E/3) * (T**3 - T0**3) + (C4E/4) * (T**4 - T0**4)
hF = hF0 + C1F*(T - T0) + (C2F/2) * (T**2 - T0**2) + (C3F/3) * (T**3 - T0**3) + (C4F/4) * (T**4 - T0**4)
hG = hG0 + C1G*(T - T0) + (C2G/2) * (T**2 - T0**2) + (C3G/3) * (T**3 - T0**3) + (C4G/4) * (T**4 - T0**4)
hH = hH0 + C1H*(T - T0) + (C2H/2) * (T**2 - T0**2) + (C3H/3) * (T**3 - T0**3) + (C4H/4) * (T**4 - T0**4)
sA = sA0 + 1/6 * (6 * C2A * T + 3 * C3A * T**2 + 2 * C4A * T**3 - 6 * C2A * T0 - 3 * C3A * T0**2 - 2 * C4A * T0**3 +6 * C1A * (np.log(T) - np.log(T0)))
sB = sB0 + 1/6 * (6 * C2B * T + 3 * C3B * T**2 + 2 * C4B * T**3 - 6 * C2B * T0 - 3 * C3B * T0**2 - 2 * C4B * T0**3 +6 * C1B * (np.log(T) - np.log(T0)))
sC = sC0 + 1/6 * (6 * C2C * T + 3 * C3C * T**2 + 2 * C4C * T**3 - 6 * C2C * T0 - 3 * C3C * T0**2 - 2 * C4C * T0**3 +6 * C1C * (np.log(T) - np.log(T0)))
sD = sD0 + 1/6 * (6 * C2D * T + 3 * C3D * T**2 + 2 * C4D * T**3 - 6 * C2D * T0 - 3 * C3D * T0**2 - 2 * C4D * T0**3 +6 * C1D * (np.log(T) - np.log(T0)))
sE = sE0 + 1/6 * (6 * C2E * T + 3 * C3E * T**2 + 2 * C4E * T**3 - 6 * C2E * T0 - 3 * C3E * T0**2 - 2 * C4E * T0**3 +6 * C1E * (np.log(T) - np.log(T0)))
sF = sF0 + 1/6 * (6 * C2F * T + 3 * C3F * T**2 + 2 * C4F * T**3 - 6 * C2F * T0 - 3 * C3F * T0**2 - 2 * C4F * T0**3 +6 * C1F * (np.log(T) - np.log(T0)))
sG = sG0 + 1/6 * (6 * C2G * T + 3 * C3G * T**2 + 2 * C4G * T**3 - 6 * C2G * T0 - 3 * C3G * T0**2 - 2 * C4G * T0**3 +6 * C1G * (np.log(T) - np.log(T0)))
sH = sH0 + 1/6 * (6 * C2H * T + 3 * C3H * T**2 + 2 * C4H * T**3 - 6 * C2H * T0 - 3 * C3H * T0**2 - 2 * C4H * T0**3 +6 * C1H * (np.log(T) - np.log(T0)))
mA = hA-T*sA
mB = hB-T*sB
mC = hC-T*sC
mD = hD-T*sD
mE = hE-T*sE
mF = hF-T*sF
mG = hG-T*sG
mH = hH-T*sH
# ################################# Gibbs free energy minimization routine ################################################################
# Inform from a to b the respective molar inlet quantity:
a = 1; # CH4
b = 1; # O2
c = 0; # H2
d = 0; # CO
e = 0; # CO2
f = 0; # H2O
g = 0; # C
h = 1-(a+b+c+d+e+f+g); # N2
#i = 0; # Ar
from gekko import GEKKO
m = GEKKO()
# Variables to be minimized:
nA, nB, nC, nD, nE, nF, nG, nH = [m.Var() for i in range(8)]
# Initial values:
nA = 0.2; nB = 0.2; nC = 0.2; nD = 0.1; nE = 0.1; nF = 0.1; nG = 0.05; nH = 2
nt = nA + nB + nC + nD + nE + nF + nG + nH
# Boundary conditions (in this case, boundary conditions are the atomic balances for H, O and C)
m.Equation(nA>=0); m.Equation(nB>=0); m.Equation(nC>=0); m.Equation(nD>=0); m.Equation(nE>=0); m.Equation(nF>=0); m.Equation(nG>=0); m.Equation(nH>=0)
m.Equation(nA+nD+nE+nG==a)
m.Equation(2*nB+nD+2*nE+nF==2*b)
m.Equation(4*nA+2*nC+2*nF==4*a)
m.Equation(nA + nB + nC + nD + nE + nF + nG + nH == nt)
#Gibbs free energy function to be minimized (here for gekko it should be the "Objective")
# Objective:
m.Obj(nA*mA + nB*mB + nC*mC + nD*mD + nE*mE + nF*mF + nG*mG + nH*mH + \
R*T*(nA*np.log(((nA*p)/nt)) + nB*np.log(((nB*p)/nt))+ nC*np.log(((nC*p)/nt)) + \
nD*np.log(((nD*p)/nt)) + nE*np.log(((nE*p)/nt)) + nF*np.log(((nF*p)/nt)) + nH*np.log(((nH*p)/nt))))
# Set global options
m.options.IMODE = 3
# Solve minimization
m.solve()
# Results
print('')
print('Results')
print('nA: ' + str(nA.value))
How do I solve the problem?
Nice application for minimization of Gibbs Free Energy! Some of the things that needed to be fixed:
Use m.log instead of np.log when defining Gekko equations. This allows automatic differentiation to provide exact first and second derivatives to the solver (IPOPT).
Don't override nA-nH with numeric values. Use the nA.value property.
You can add equations such as nA>=0 or just add a lower bound by setting nA.lower=0. Using upper and lower limits on the variables is more efficient than adding inequality constraints.
The indentation doesn't appear to be correct in your posted code. I assumed that everything after the for T in T: statement should be indented as part of that loop. Please check this.
I added an upper bound for your variables of 5.0. Otherwise, the solution is unbounded. You shouldn't typically have a variable at a bound for Gibbs Free Energy minimization problems so please check this as well. I also set the lower bound to 0.01 so that the m.log terms would not be zero and cause an evaluation error.
# Equilibrium calculations - Isothermal conditions
# library with mathematical package
import math
import numpy as np
# ############ Substances ID:
##########################################
# A=methane B=oxygen
# C=hydrogen D=carbon monoxide
# E=carbon dioxide F=water
# G=carbon H=Nitrogen
# I=argon
# ############ Thermodynamic data
##########################################
T0 = 298.15 #[K]
R = 8.134 #[J/mol.K]
p = 1 #[atm]
# A) Standard Enthalpy (hi0) [J/mol] and Entropy (si0) [J/mol] at T0=298.15K
# Source: [Perry's Chemical, Engineers' Handbook seventh edition, Mc Graw-Hill, 1997]
hA0 = -74520; sA0 = 1.8627*10**2
hB0 = 0; sB0 = 2.05149*10**2
hC0 = 0; sC0 = 1.3057*10**2
hD0 = -110530; sD0 = 1.9756*10**2
hE0 = -393510; sE0 = 2.1368*10**2
hF0 = -241814; sF0 = 1.8872*10**2
hG0 = 0; sG0 = 5.734
hH0 = 0; sH0 = 1.91610*10**2
#hI0 = 0; sI0 = 1.5484*10**2
# B) Ideal gas heat capacity polynomial: Coefficients for Cp in [J/mol.K]
# Source: [Chemical thermodynamics for process simulation, Appendix G, pg:711]
C1A = 19.238; C2A = 52.090*10**-3; C3A = 11.966*10**-6; C4A = -11.309*10**-9
C1B = 28.087; C2B = -0.004*10**-3; C3B = 17.447*10**-6; C4B = -10.644*10**-9
C1C = 27.124; C2C = 9.2670*10**-3; C3C = -13.799*10**-6; C4C = 7.64000*10**-9
C1D = 30.848; C2D = -12.84*10**-3; C3D = 27.870*10**-6; C4D = -12.710*10**-9
C1E = 19.780; C2E = 73.390*10**-3; C3E = -55.98*10**-6; C4E = 17.1400*10**-9
C1F = 32.220; C2F = 1.9225*10**-3; C3F = 10.548*10**-6; C4F = -3.5940*10**-9
C1G = -5.416; C2G = 58.981*10**-3; C3G = -43.559*10**-6; C4G = 11.604*10**-9
C1H = 31.128; C2H = -13.556*10**-3; C3H = 26.777*10**-6; C4H = -11.673*10**-9
# ####### Thermodynamic Calculations #####################################
# A) Standard Enthalpy (hi0), Entropy (si0) and Gibbs free molar energy (mi) at T0 [J/mol]
T = np.arange(50, 1000, 50)
for T in T:
hA = hA0 + C1A*(T - T0) + (C2A/2) * (T**2 - T0**2) + (C3A/3) * (T**3 - T0**3) + (C4A/4) * (T**4 - T0**4)
hB = hB0 + C1B*(T - T0) + (C2B/2) * (T**2 - T0**2) + (C3B/3) * (T**3 - T0**3) + (C4B/4) * (T**4 - T0**4)
hC = hC0 + C1C*(T - T0) + (C2C/2) * (T**2 - T0**2) + (C3C/3) * (T**3 - T0**3) + (C4C/4) * (T**4 - T0**4)
hD = hD0 + C1D*(T - T0) + (C2D/2) * (T**2 - T0**2) + (C3D/3) * (T**3 - T0**3) + (C4D/4) * (T**4 - T0**4)
hE = hE0 + C1E*(T - T0) + (C2E/2) * (T**2 - T0**2) + (C3E/3) * (T**3 - T0**3) + (C4E/4) * (T**4 - T0**4)
hF = hF0 + C1F*(T - T0) + (C2F/2) * (T**2 - T0**2) + (C3F/3) * (T**3 - T0**3) + (C4F/4) * (T**4 - T0**4)
hG = hG0 + C1G*(T - T0) + (C2G/2) * (T**2 - T0**2) + (C3G/3) * (T**3 - T0**3) + (C4G/4) * (T**4 - T0**4)
hH = hH0 + C1H*(T - T0) + (C2H/2) * (T**2 - T0**2) + (C3H/3) * (T**3 - T0**3) + (C4H/4) * (T**4 - T0**4)
sA = sA0 + 1/6 * (6 * C2A * T + 3 * C3A * T**2 + 2 * C4A * T**3 - 6 * C2A * T0 - 3 * C3A * T0**2 - 2 * C4A * T0**3 +6 * C1A * (np.log(T) - np.log(T0)))
sB = sB0 + 1/6 * (6 * C2B * T + 3 * C3B * T**2 + 2 * C4B * T**3 - 6 * C2B * T0 - 3 * C3B * T0**2 - 2 * C4B * T0**3 +6 * C1B * (np.log(T) - np.log(T0)))
sC = sC0 + 1/6 * (6 * C2C * T + 3 * C3C * T**2 + 2 * C4C * T**3 - 6 * C2C * T0 - 3 * C3C * T0**2 - 2 * C4C * T0**3 +6 * C1C * (np.log(T) - np.log(T0)))
sD = sD0 + 1/6 * (6 * C2D * T + 3 * C3D * T**2 + 2 * C4D * T**3 - 6 * C2D * T0 - 3 * C3D * T0**2 - 2 * C4D * T0**3 +6 * C1D * (np.log(T) - np.log(T0)))
sE = sE0 + 1/6 * (6 * C2E * T + 3 * C3E * T**2 + 2 * C4E * T**3 - 6 * C2E * T0 - 3 * C3E * T0**2 - 2 * C4E * T0**3 +6 * C1E * (np.log(T) - np.log(T0)))
sF = sF0 + 1/6 * (6 * C2F * T + 3 * C3F * T**2 + 2 * C4F * T**3 - 6 * C2F * T0 - 3 * C3F * T0**2 - 2 * C4F * T0**3 +6 * C1F * (np.log(T) - np.log(T0)))
sG = sG0 + 1/6 * (6 * C2G * T + 3 * C3G * T**2 + 2 * C4G * T**3 - 6 * C2G * T0 - 3 * C3G * T0**2 - 2 * C4G * T0**3 +6 * C1G * (np.log(T) - np.log(T0)))
sH = sH0 + 1/6 * (6 * C2H * T + 3 * C3H * T**2 + 2 * C4H * T**3 - 6 * C2H * T0 - 3 * C3H * T0**2 - 2 * C4H * T0**3 +6 * C1H * (np.log(T) - np.log(T0)))
mA = hA-T*sA
mB = hB-T*sB
mC = hC-T*sC
mD = hD-T*sD
mE = hE-T*sE
mF = hF-T*sF
mG = hG-T*sG
mH = hH-T*sH
# ################################# Gibbs free energy minimization routine ################################################################
# Inform from a to b the respective molar inlet quantity:
a = 1; # CH4
b = 1; # O2
c = 0; # H2
d = 0; # CO
e = 0; # CO2
f = 0; # H2O
g = 0; # C
h = 1-(a+b+c+d+e+f+g); # N2
#i = 0; # Ar
from gekko import GEKKO
m = GEKKO()
# Variables to be minimized:
nA, nB, nC, nD, nE, nF, nG, nH = [m.Var() for i in range(8)]
var = [nA, nB, nC, nD, nE, nF, nG, nH]
# Initial values:
n0 = [0.2,0.2,0.2,0.1,0.1,0.1,0.05,2]
nL = np.ones(len(n0))*0.01
nU = np.ones(len(n0))*5.0
for i,x in enumerate(var):
x.value = n0[i]
x.lower = nL[i]
x.upper = nU[i]
nt = m.Intermediate(nA + nB + nC + nD + nE + nF + nG + nH)
# Boundary conditions (in this case, boundary conditions are the atomic balances for H, O and C)
#m.Equation(nA>=0); m.Equation(nB>=0); m.Equation(nC>=0); m.Equation(nD>=0)
#m.Equation(nE>=0); m.Equation(nF>=0); m.Equation(nG>=0); m.Equation(nH>=0)
m.Equation(nA+nD+nE+nG==a)
m.Equation(2*nB+nD+2*nE+nF==2*b)
m.Equation(4*nA+2*nC+2*nF==4*a)
m.Equation(nA + nB + nC + nD + nE + nF + nG + nH == nt)
#Gibbs free energy function to be minimized (here for gekko it should be the "Objective")
# Objective:
m.Obj(nA*mA + nB*mB + nC*mC + nD*mD + nE*mE + nF*mF + nG*mG + nH*mH + \
R*T*(nA*m.log(((nA*p)/nt)) + nB*m.log(((nB*p)/nt))+ nC*m.log(((nC*p)/nt)) + \
nD*m.log(((nD*p)/nt)) + nE*m.log(((nE*p)/nt)) + nF*m.log(((nF*p)/nt)) + \
nH*m.log(((nH*p)/nt))))
# Set global options
m.options.IMODE = 3
# Solve minimization
m.solve()
# Results
print('')
print('Results')
print('nA: ' + str(nA.value))
my issue is related to Runge-Kutta 4 (RK4) method and the correct iteration steps required for the state vector of an orbiting satellite.
The below code (in Python) describes the motion based on the description as per this link (http://www.navipedia.net/index.php/GLONASS_Satellite_Coordinates_Computation):
if total_step_number != 0:
for i in range(1, total_step_number+1):
#Calculate k1
k1[0] = (-cs.GM_GLONASS * XYZ[0] / radius**3) \
+ ((3/2) * cs.C_20 * cs.GM_GLONASS * cs.SEMI_MAJOR_AXIS_GLONASS**2 * XYZ[0] * (1 - (5*(XYZ[2]**2) / (radius**2))) / radius**5) \
+ XYZDDot[0] + (cs.OMEGAE_DOT**2 * XYZ[0]) + (2 * cs.OMEGAE_DOT * XYZDot[1])
k1[1] = (-cs.GM_GLONASS * XYZ[1] / radius**3) \
+ ((3/2) * cs.C_20 * cs.GM_GLONASS * cs.SEMI_MAJOR_AXIS_GLONASS**2 * XYZ[1] * (1 - (5*(XYZ[2]**2) / (radius**2))) / radius**5) \
+ XYZDDot[1] + (cs.OMEGAE_DOT**2 * XYZ[1]) - (2 * cs.OMEGAE_DOT * XYZDot[0])
k1[2] = (-cs.GM_GLONASS * XYZ[2] / radius**3) \
+ ((3/2) * cs.C_20 * cs.GM_GLONASS * cs.SEMI_MAJOR_AXIS_GLONASS**2 * XYZ[2] * (3 - (5*(XYZ[2]**2) / (radius**2))) / radius**5) \
+ XYZDDot[2]
#Intermediate step to bridge k1 to k2
XYZ2[0] = XYZ[0] + (XYZDot[0] * h / 2) + (k1[0] * h**2 / 8)
XYZDot2[0] = XYZDot[0] + (k1[0] * h / 2)
XYZ2[1] = XYZ[1] + (XYZDot[1] * h / 2) + (k1[1] * h**2 / 8)
XYZDot2[1] = XYZDot[1] + (k1[1] * h / 2)
XYZ2[2] = XYZ[2] + (XYZDot[2] * h / 2) + (k1[2] * h**2 / 8)
XYZDot2[2] = XYZDot[2] + (k1[2] * h / 2)
radius = np.sqrt((XYZ2[0]**2)+(XYZ2[1]**2)+(XYZ2[2]**2))
....
There is more code however I want to limit what I show for now since it's the intermediate steps I'm most interested in resolving. Basically, for those familiar with state vectors and using RK4, you can see that the position and velocity is updated in the intermediate step, but not the acceleration. My question is related to the calculation required in order to update too the acceleration. It would begin:
XYZDDot[0] = ...
XYZDDot[1] = ...
XYZDDot[2] = ...
...but what exactly comes after is not very clear. Any advice welcome.
Below is the full code:
for j in h_step_values:
h = j
if h > 0:
one_way_iteration_steps = one_way_iteration_steps -1
elif h < 0:
one_way_iteration_steps = one_way_iteration_steps +1
XYZ = initial_XYZ
#if total_step_number != 0:
for i in range(0, one_way_iteration_steps):
#Calculate k1
k1[0] = (-cs.GM_GLONASS * XYZ[0] / radius**3) \
+ ((3/2) * cs.C_20 * cs.GM_GLONASS * cs.SEMI_MAJOR_AXIS_GLONASS**2 * XYZ[0] * (1 - (5*(XYZ[2]**2) / (radius**2))) / radius**5) \
+ XYZDDot[0] + (cs.OMEGAE_DOT**2 * XYZ[0]) + (2 * cs.OMEGAE_DOT * XYZDot[1])
k1[1] = (-cs.GM_GLONASS * XYZ[1] / radius**3) \
+ ((3/2) * cs.C_20 * cs.GM_GLONASS * cs.SEMI_MAJOR_AXIS_GLONASS**2 * XYZ[1] * (1 - (5*(XYZ[2]**2) / (radius**2))) / radius**5) \
+ XYZDDot[1] + (cs.OMEGAE_DOT**2 * XYZ[1]) - (2 * cs.OMEGAE_DOT * XYZDot[0])
k1[2] = (-cs.GM_GLONASS * XYZ[2] / radius**3) \
+ ((3/2) * cs.C_20 * cs.GM_GLONASS * cs.SEMI_MAJOR_AXIS_GLONASS**2 * XYZ[2] * (3 - (5*(XYZ[2]**2) / (radius**2))) / radius**5) \
+ XYZDDot[2]
#Intermediate step to bridge k1 to k2
XYZ2[0] = XYZ[0] + (XYZDot[0] * h / 2) + (k1[0] * h**2 / 8)
XYZDot2[0] = XYZDot[0] + (k1[0] * h / 2)
XYZDDot2[0] = XYZDDot[0] + (k1[0] * h / 2)
XYZ2[1] = XYZ[1] + (XYZDot[1] * h / 2) + (k1[1] * h**2 / 8)
XYZDot2[1] = XYZDot[1] + (k1[1] * h / 2)
XYZ2[2] = XYZ[2] + (XYZDot[2] * h / 2) + (k1[2] * h**2 / 8)
XYZDot2[2] = XYZDot[2] + (k1[2] * h / 2)
radius = np.sqrt((XYZ2[0]**2)+(XYZ2[1]**2)+(XYZ2[2]**2))
#Calculate k2
k2[0] = (-cs.GM_GLONASS * XYZ2[0] / radius**3) \
+ ((3/2) * cs.C_20 * cs.GM_GLONASS * cs.SEMI_MAJOR_AXIS_GLONASS**2 * XYZ2[0] * (1 - (5*(XYZ2[2]**2) / (radius**2))) / radius**5) \
+ XYZDDot[0] + (cs.OMEGAE_DOT**2 * XYZ2[0]) + (2 * cs.OMEGAE_DOT * XYZDot2[1])
k2[1] = (-cs.GM_GLONASS * XYZ2[1] / radius**3) \
+ ((3/2) * cs.C_20 * cs.GM_GLONASS * cs.SEMI_MAJOR_AXIS_GLONASS**2 * XYZ2[1] * (1 - (5*(XYZ2[2]**2) / (radius**2))) / radius**5) \
+ XYZDDot[1] + (cs.OMEGAE_DOT**2 * XYZ2[1]) - (2 * cs.OMEGAE_DOT * XYZDot2[0])
k2[2] = (-cs.GM_GLONASS * XYZ2[2] / radius**3) \
+ ((3/2) * cs.C_20 * cs.GM_GLONASS * cs.SEMI_MAJOR_AXIS_GLONASS**2 * XYZ2[2] * (3 - (5*(XYZ2[2]**2) / (radius**2))) / radius**5) \
+ XYZDDot[2]
#Intermediate step to bridge k2 to k3
XYZ2[0] = XYZ[0] + (XYZDot[0] * h / 2) + (k2[0] * h**2 / 8)
XYZDot2[0] = XYZDot[0] + (k2[0] * h / 2)
XYZ2[1] = XYZ[1] + (XYZDot[1] * h / 2) + (k2[1] * h**2 / 8)
XYZDot2[1] = XYZDot[1] + (k2[1] * h / 2)
XYZ2[2] = XYZ[2] + (XYZDot[2] * h / 2) + (k2[2] * h**2 / 8)
XYZDot2[2] = XYZDot[2] + (k2[2] * h / 2)
radius = np.sqrt((XYZ2[0]**2)+(XYZ2[1]**2)+(XYZ2[2]**2))
#Calculate k3
k3[0] = (-cs.GM_GLONASS * XYZ2[0] / radius**3) \
+ ((3/2) * cs.C_20 * cs.GM_GLONASS * cs.SEMI_MAJOR_AXIS_GLONASS**2 * XYZ2[0] * (1 - (5*(XYZ2[2]**2) / (radius**2))) / radius**5) \
+ XYZDDot[0] + (cs.OMEGAE_DOT**2 * XYZ2[0]) + (2 * cs.OMEGAE_DOT * XYZDot2[1])
k3[1] = (-cs.GM_GLONASS * XYZ2[1] / radius**3) \
+ ((3/2) * cs.C_20 * cs.GM_GLONASS * cs.SEMI_MAJOR_AXIS_GLONASS**2 * XYZ2[1] * (1 - (5*(XYZ2[2]**2) / (radius**2))) / radius**5) \
+ XYZDDot[1] + (cs.OMEGAE_DOT**2 * XYZ2[1]) - (2 * cs.OMEGAE_DOT * XYZDot2[0])
k3[2] = (-cs.GM_GLONASS * XYZ2[2] / radius**3) \
+ ((3/2) * cs.C_20 * cs.GM_GLONASS * cs.SEMI_MAJOR_AXIS_GLONASS**2 * XYZ2[2] * (3 - (5*(XYZ2[2]**2) / (radius**2))) / radius**5) \
+ XYZDDot[2]
#Intermediate step to bridge k3 to k4
XYZ2[0] = XYZ[0] + (XYZDot[0] * h) + (k3[0] * h**2 / 2)
XYZDot2[0] = XYZDot[0] + (k3[0] * h)
XYZ2[1] = XYZ[1] + (XYZDot[1] * h) + (k3[1] * h**2 / 2)
XYZDot2[1] = XYZDot[1] + (k3[1] * h)
XYZ2[2] = XYZ[2] + (XYZDot[2] * h) + (k3[2] * h**2 / 2)
XYZDot2[2] = XYZDot[2] + (k3[2] * h)
radius = np.sqrt((XYZ2[0]**2)+(XYZ2[1]**2)+(XYZ2[2]**2))
#Calculate k4
k4[0] = (-cs.GM_GLONASS * XYZ2[0] / radius**3) \
+ ((3/2) * cs.C_20 * cs.GM_GLONASS * cs.SEMI_MAJOR_AXIS_GLONASS**2 * XYZ2[0] * (1 - (5*(XYZ2[2]**2) / (radius**2))) / radius**5) \
+ XYZDDot[0] + (cs.OMEGAE_DOT**2 * XYZ2[0]) + (2 * cs.OMEGAE_DOT * XYZDot2[1])
k4[1] = (-cs.GM_GLONASS * XYZ2[1] / radius**3) \
+ ((3/2) * cs.C_20 * cs.GM_GLONASS * cs.SEMI_MAJOR_AXIS_GLONASS**2 * XYZ2[1] * (1 - (5*(XYZ2[2]**2) / (radius**2))) / radius**5) \
+ XYZDDot[1] + (cs.OMEGAE_DOT**2 * XYZ2[1]) - (2 * cs.OMEGAE_DOT * XYZDot2[0])
k4[2] = (-cs.GM_GLONASS * XYZ2[2] / radius**3) \
+ ((3/2) * cs.C_20 * cs.GM_GLONASS * cs.SEMI_MAJOR_AXIS_GLONASS**2 * XYZ2[2] * (3 - (5*(XYZ2[2]**2) / (radius**2))) / radius**5) \
+ XYZDDot[2]
for p in range(3):
XYZ[p] = XYZ[p] + XYZDot[p] * h + h**2 * ((k1[p] + 2*k2[p] + 2*k3[p] + k4[p]) / 12)
XYZDot[p] = XYZDot[p] + (h * (k1[p] + 2*k2[p] + 2*k3[p] + k4[p]) / 6)
radius = np.sqrt((XYZ[0])**2 + (XYZ[0])**2 + (XYZ[0])**2)
The equation you are solving is of the type
ddot x = a(x)
where a(x) is the acceleration which is computed in your k1 computation. Indeed, the first order system would be
dot v = a(x)
dot x = v
The RK4 implementation thus starts with
k1 = a(x)
l1 = v
k2 = a(x+l1*h/2) = a(x+v*h/2)
l2 = v+k1*h/2
etc. The use of the l1,l2,... seems implicit in the code, inserting these linear combinations directly where they occur.
In short, you are not missing the acceleration computation, it is the main part of the code fragment.
Update: (8/22) To come closer to the intention of the intermediate bridge steps, the abstract code should read ( with (* .. *) denoting comments or unnecessary computations)
k1 = a(x) (* l1 = v *)
x2 = x + v*h/2 (* v2 = v + k1*h/2 *)
k2 = a(x2) (* l2 = v2 *)
x3 (* = x + l2*h/2 *)
= x + v*h/2 + k1*h^2/4 (* v3 = v + k2*h/2 *)
k3 = a(x3) (* l3 = v3 *)
x4 (* = x + l3*h *)
= x + v*h + k2*h^2/2 (* v4 = v + k3*h *)
k4 = a(x4) (* l4 = v4 *)
delta_v = ( k1+2*(k2+k3)+k4 ) * h/6
delta_x (* = ( l1+2*(l2+l3)+l4 ) * h/6 *)
= v*h + (k1+k2+k3) * h^2/6