Uploaded by juan.alonsotjr

applied-numerical-methods-in-chemical-engineering-20201128

advertisement
Applied Numerical Methods in Chemical Engineering
Using Python
Zulfan Adi Putra, PDEng, PEng
Internal
Outline
q Resources and Introduction to Python
q Python Basics: Conditionals, Functions, Loops, Arrays, Generate Plots, Solve Equations
q Linear Algebra
q Ordinary Differential Equations
q Partial Differential Equations
q Data Regression and Curve Fitting
q Machine Learning
q Optimization
Internal
Python Resources
Internal
Installation through Anaconda
• https://www.anaconda.com/products/individual
Internal
Python Important Libraries
• Numpy (www.numpy.org) – array operation library
• Scipy (www.scipy.org) – scientific algorithm library that uses numpy
• Matplotlib (www.matplotlib.org) – provides the pyplot and pylab
plotting libraries
• Pandas (http://pandas.pydata.org/) – easy to use data structures and
data analysis tools
• Scikit-learn (https://scikit-learn.org/) – machine learning in python
• Xgboost (https://xgboost.readthedocs.io/en/latest/index.html) –
eXtreme Gradient Boosting algorithm, winning algorithm for Kagel
data science competitions in recent years
• Sympy (www.sympy.org) – symbolic mathematics library (optional)
Internal
1st Aid
Internal
Youtube Intro for Beginners in Python
• https://www.youtube.com/watch?v=rfscVS0vtbw&t=4669s
Internal
Other Helps
1
2
3
Internal
Python Basics
Internal
Spyder
Working
directory
Working folder
address
Tab file
Created variables.
Run (F5), then check your
variables here
Console
Internal
All files
available in
the working
folder
Python Programming Basics
• https://apmonitor.com/che263/index.php/Main/PythonBasics
Internal
Some Basics
(not to memorize, but you’ll see them when you make any error)
• Case-sensitive
• Indentation-sensitive
• Numbering starts from 0
• Code and run line by line for easier debugging. Use “print()” to check
• Use comment “#” often
•…
Internal
Some Basics: Data plotting using numpy and matplotlib
Internal
Another Example
Internal
Some Basics: simple calculation
Internal
Some Basics: For loop
Internal
Some Basics: While loop
• “while” loop
Internal
Some Basics: Range()
• “range()” function
Internal
Some Basics: Define function
Internal
Some Basics: Function + IF-Else
sqrt() is a built-in
function within “math”
package
Internal
Some Basics: Function + If-Else + While
Internal
Linear Algebra
Internal
https://docs.scipy.org/doc/scipy/reference/tutorial/linalg.html
Linear Algebra
x + 3y + 5z = 10
2x + 5y + z = 8
2x + 3y + 8z = 3
Internal
1
2

 2
3
5
3
5   x  10 
1   y    8 
8   z   3 
 x  1
 y   2
  
 z   2
3
5
3
5
1 
8 
1
10 
8 
 
 3 
Linear Algebra with Gekko
3x + 2y = 1
x + 2y = 0
Internal
Solving Ordinary Differential
Equations (ODE) with Python
Internal
Runge Kutta method
• https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods
Internal
Pressure Profile In A Vessel
Internal
Zombie Population
Internal
https://scipycookbook.readthedocs.io/items/Zombie
_Apocalypse_ODEINT.html
Zombie Population: Vary initial conditions
Internal
Results
Internal
SEIR model = ODE + Curve Fitting for Parameter Optimization
Internal
Define necessary parameters
Internal
Define more parameters
Internal
Define functions
Internal
Data fitting with data to obtain model parameters
Results:
Internal
Using obtained model parameters to predict the trend
Internal
Extending the prediction
Internal
Further predictions
Internal
Solving Partial Differential
Equations (PDE) with Python
Internal
Partial Differential Equation
North
West
East
South
coeff = 2*(h2k2+1)
h2k2 = (h/k)2
h and k are steps in the integration
Centered Finite Different Approximation
 coeff  ci , j  ci 1, j  ci 1, j  h 2k 2ci , j 1  ci , j 1   0
 coeff  node  East  West  h 2k 2North  South   0
 ci , j 
c 
 i 1, j 
1 1 h 2k 2 h 2k 2 ci 1, j   0
c 
 i , j 1 
ci , j 1 
Linear Algebra:
Ax = b
 coeff
Node = j*(M+1) + i, with M = 15, N = 15
node = 255
node = 240
i=0
j=N
i=M
j=N
i=0
j=0
i=M
j=0
node = 16
node = 1
node = 2
node = 3
node = 15
The Code
node definition for:
ci,j --> current node
ci+1, j --> east node
ci-1, j --> west node
ci,j+1 --> north node
ci,j-1 --> south node
C = 0 or matrix b = 0 in all
sides, except at i = 0 (west)
 coeff  ci , j  ci 1, j  ci 1, j  h 2k 2ci , j 1  ci , j 1   0
0
 coeff  node  East  West  h 2k 2 North  South   0
Internal
Linear Regression with Python
Internal
Linear Regression with Polyfit and Linregress
Internal
Regressions with Gekko
Internal
Machine Learning with Python
Internal
Black Box Machine Learning
Independent
variables, e.g.
P, T, F
Internal
ML
Dependent
variables, e.g.
Machine Learning using NN (DME case)
Internal
Internal
Print and plot results
Internal
Machine Learning Using XGBoost (DME case)
Internal
Internal
Internal
Optimization with Python
Internal
Optimization and Root Finding
General Formulation for Optimization:
min
f x, y, z 
subject
to :
hi  x , y , z   0
g j x, y, z   0
x R
y   0 ,1 
z  I
Internal
Optimization and Root Finding
Internal
https://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html
Optimization with Gekko https://gekko.readthedocs.io/en/latest/quick_start.html
Internal
Linear Programming with linprog
Internal
https://realpython.com/linear-programming-python/
Internal
Linear Programming
“linprog” package ONLY receives general optimization formulation
Later, change x1 bound to below:
Internal
Project Scheduling with MIP
Internal
Dummy example
Internal
Model
Internal
Coding the model in Python (1)
Data input
Variables declaration
Internal
Coding the model in Python (2)
General model
Internal
Coding the model in Python (3)
Users’ manual constraint
Internal
Coding the model in Python (4)
solving and printing results
write the model for checking
IMPORTANT!
This code can NOT be
included prior to the solve
code above
Internal
MIP result
the model built is
saved here
the model
built
results printed
here
Project Scheduling Result
Case 1: No MAX total cost constraint
Case 2: MAX total cost until time 2 (3 period) is 15
Case 2a: MAX total cost until time 2 (3 period) is 15 AND forcing Project 1 to start earlier
Internal
Non Linear Optimization (unconstrained)
Internal
Non Linear Optimization (Constrained)
Internal
Run and print the result
Internal
Stochastic Optimization (1)
Internal
Stochastic Optimization (2)
Internal
available solvers are:
1. shgo,
2. dual_annealing,
3. differential_evolution,
4. basinhopping
Monte Carlo Simulation
Internal
Monte Carlo Simulation with Machine Learning model
(checking inherent randomness in machine learning)
Internal
Monte Carlo simulation loop
Internal
Internal
Download