casualguitar

Perse95 Code

Apr 28th, 2022 (edited)
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.39 KB | None | 0 0
  1. import numpy as np
  2. from scipy.integrate import solve_ivp
  3. import matplotlib.pyplot as plt
  4. import math
  5.  
  6.  
  7. def initialize_system(*constants):
  8.     #sets the appropriate initial/boundary conditions
  9.  
  10.     dx, number_of_species , number_of_nodes, A_s, C_pj = constants #unpack constants
  11.     x0_species_eqn = np.zeros((2,number_of_species, number_of_nodes))
  12.     x0_other_eqn = np.zeros(3,number_of_nodes)
  13.  
  14.     #here the initial and boundary conditions are set (flow rate, Tin, T0, etc)
  15.     Tb0 = ...
  16.     M0 = ...
  17.     Tg0 = ...
  18.     mj0 = ...
  19.     y0 = ...
  20.  
  21.     for i in range(number_of_species):
  22.         for j in range(number_of_nodes):
  23.             #For each species i , we set the appropriate initial state for each node j for the dyi/dt Equation
  24.             x0_species_eqn[0][i][j] = ...
  25.             #Similarly for the dM_ij/dt equation
  26.             x0_species_eqn[1][i][j] = ...
  27.             mj = mj(a,b,c) #calculate mj
  28.  
  29.     for j in range(number_of_nodes):
  30.         # Similarly for the dT_g/dt equation
  31.         x0_other_eqn[0][j] = ...
  32.         # Similarly for the dT_b/dt equation
  33.         x0_other_eqn[1][j] = ...
  34.        
  35.         mj = mj(a,b,c) #calculate mj
  36.  
  37.     return np.concatenate([x0_species_eqn.reshape(-1), x0_other_eqn.reshape(-1)], axis=0)
  38.  
  39.  
  40. def df(t,x,*constants):
  41.     #the time derivative of x
  42.     dx, number_of_species, number_of_nodes, A_s, C_pj = constants  # unpack constants
  43.  
  44.     #we operate on the array as before, but first we unpack the initial state
  45.     x_species_eqn = x.reshape(2,number_of_species,number_of_nodes)
  46.     x_other_eqn = x.reshape(3,number_of_nodes)
  47.  
  48.     dxdt_species_eqn = np.zeros_like(x_species_eqn)
  49.     dxdt_other_eqn = np.zeros_like(x_other_eqn)
  50.  
  51.     for i in range(number_of_species):
  52.         for j in range(number_of_nodes):
  53.             # For each species i , we compute dyi/dt Equation for each node j
  54.             dxdt_species_eqn[0][i][j] = ...
  55.             # Similarly for the dM_ij/dt equation
  56.             dxdt_species_eqn[1][i][j] = ...
  57.             # Similarly for the dT_g/dt equation
  58.            
  59.             mj = mj(a,b,c)
  60.  
  61.     for j in range(number_of_nodes):
  62.         dxdt_other_eqn[0][j] = ...
  63.         # Similarly for the dT_b/dt equation
  64.         dxdt_other_eqn[1][j] = ...
  65.        
  66.         mj = mj(a,b,c)
  67.        
  68.  
  69.             #what are these equations called (the relevant ODE for each species and position?)
  70.     return np.concatenate([dxdt_species_eqn.reshape(-1), dxdt_other_eqn.reshape(-1)], axis=0)
  71.  
  72. def mj(a, b, c):
  73.     #this function calculates mj as a function of the relevant paramteters
  74.     #this function will be called inside the initialize_system and df functions where necessary
  75.  
  76. n = 10
  77. A_s = 1
  78. C_pj = 1 #can be an array
  79.  
  80. #create your initial conditions
  81. t0 = 0
  82. tf = 100 #how long you want to integrate for
  83. x0 = initialize_system(n,A_s,C_pj)
  84.  
  85. #solve the system
  86. sol = solve_ivp(df, [t0,tf], x0, dense_output = True) #dense_output gives you an interpolant (3rd/4th order polynomial)
  87.  
  88. #decide at which times you'd like to check your system
  89. t_eval = [t_i, t_i+1,...t_f]
  90.  
  91. x_eval = [sol.sol(t) for t in t_eval]
  92.  
  93.  
  94. x_species_eqn, x_other_eqn = x[:2*number_of_species*number_of_nodes], x[2*number_of_species*number_of_nodes:]
  95.  
  96. x_species_eqn = x_species_eqn.reshape(2, number_of_species, number_of_nodes)
  97. x_other_eqn   = x_other_eqn.reshape(3, number_of_nodes)
  98.  
  99.  
  100. # And then use them like you normally would
  101. ...
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
Advertisement
Add Comment
Please, Sign In to add comment