Coupled Ion Exchange - Electrodialysis model using partial differential equations
Show older comments
Dear All,
I am trying to model a coupled Ion Exchange - Electrodialysis model. This is similar to the adsorption model. The extra term in this model is that of transport of ions due to the electric field. The first snapshot I have attached shows the original model. The second snapshot shows the rearranged form of the model to match the adsorption model (without axial dispersion). In this second snapshot you can see the extra term containing 'phi', the potential gradient.
In the adsorption model, the 2 independent variables are time and one direction. The 2 dependent variables are bulk concentration and solid phase concentration. In this model, the 3 independent variables are time and 2 directions. 1 direction (x) is the direction the ions move under the influence of the electric field. The second direction (z) is the direction that the bulk fluid moves in. The 3 dependent variables are the bulk concentration, solid phase concentration and the electric potential.
The sample code for the adsorption model is given below (Taken from https://uk.mathworks.com/matlabcentral/answers/385756-adsorption-modelling-solving-pde-axial-dispersion-model. Coded by Ashley Gratton, Torsten). Please let me know how I can change this for my model which has 3 independent and 3 dependent variables.
Thank you in advance.
function main
% Initialisation
close all
clear all
clc
%System Set-up %
%Define Variables
D = 3*10^-8; % Axial Dispersion coefficient
v = 1*10^-3; % Superficial velocity
epsilon = 0.4; % Voidage fraction
k = 3*10^-5; % Mass Transfer Coefficient
b = 2.5*10^-5; % Langmuir parameter
qs = 2*10^4; % Saturation capacity
cFeed = 10; % Feed concentration
L = 1; % Column length
t0 = 0; % Initial Time
tf = 1000; % Final time
dt = 0.5; % Time step
z = [0:0.01:L]; % Mesh generation
t = [t0:dt:tf];% Time vector
n = numel(z); % Size of mesh grid
%Initial Conditions / Vector Creation
c0 = zeros(n,1);
c0(1) = cFeed;
q0 = zeros(n,1); % t = 0, q = 0 for all z, this makes sense to me
y0 = [c0 ; q0]; % Appends conditions together
%ODE15S Solver
[T, Y] = ode15s(@(t,y) MyFun(t,y,z,n),t,y0);
function DyDt=MyFun(~, y, z, n)
% Defining Constants
D = 3*10^-8; % Axial Dispersion coefficient
v = 1*10^-3; % Superficial velocity
epsilon = 0.4; % Voidage fraction
k = 3*10^-5; % Mass Transfer Coefficient
b = 2.5*10^-5; % Langmuir parameter
qs = 20000; % Saturation capacity
% Variables being allocated zero vectors
c = zeros(n,1);
q = zeros(n,1);
DcDt = zeros(n,1);
DqDt = zeros(n,1);
DyDt = zeros(2*n,1);
zhalf = zeros(n-1,1);
DcDz = zeros(n,1);
D2cDz2 = zeros(n,1);
c = y(1:n);
q = y(n+1:2*n);
% Interior mesh points
zhalf(1:n-1)=(z(1:n-1)+z(2:n))/2;
for i=2:n-1
DcDz(i) = ((z(i)-z(i-1))/(z(i+1)-z(i))*(c(i+1)-c(i))+(z(i+1)-z(i))/(z(i)-z(i-1))*(c(i)-c(i-1)))/(z(i+1)-z(i-1));
D2cDz2(i) = (zhalf(i)*(c(i+1)-c(i))/(z(i+1)-z(i))-zhalf(i-1)*(c(i)-c(i-1))/(z(i)-z(i-1)))/(zhalf(i)-zhalf(i-1));
end
% Calculate DcDz and D2cDz2 at z=L for boundary condition dc/dz = 0
DcDz(n) = 0;
D2cDz2(n) = -1.0/(z(n)-zhalf(n-1))*(c(n)-c(n-1))/(z(n)-z(n-1));
% Set time derivatives at z=0
% DcDt = 0 since c=cFeed remains constant over time and is already set as initial condition
% Standard setting for q
DqDt(1) = k*( ((qs*b*c(1))/(1 + b*c(1))) - q(1) );
DcDt(1) = 0.0;
% Set time derivatives in remaining points
for i=2:n
%Equation: dq/dt = k(q*-q) where q* = qs * (b*c)/(1+b*c)
DqDt(i) = k*( ((qs*b*c(i))/(1 + b*c(i))) - q(i) );
%Equation: dc/dt = D * d2c/dz2 - v*dc/dz - ((1-e)/(e))*dq/dt
DcDt(i) = D*D2cDz2(i) - v*DcDz(i) - ((1-epsilon)/(epsilon))*DqDt(i);
end
% Concatenate vector of time derivatives
DyDt = [DcDt;DqDt];
end
34 Comments
Zain Shabeeb
on 9 Jul 2019
Edited: Zain Shabeeb
on 9 Jul 2019
Torsten
on 10 Jul 2019
When looking at your model, the following questions arose:
c_i^b only depends on z ?
c_i^hat depends on z and x ?
d_phi_dx is only a single number (thus not dependent on x or z) ?
Boundary conditions for c_i^b at z = 0 ?
Boundary conditions for c_i^hat at x = 0 ?
How large is "n" in d_phi_dx ?
Zain Shabeeb
on 10 Jul 2019
Torsten
on 10 Jul 2019
ci^hat (solid phase concentration) changes only with x direction (please let me know if your understanding of the model suggests otherwise).
Since c_i^hat depends on c_i^b, c_i^hat must depend on z and x.
I was given this relationship of dphidx from literature. I understand that it is dependent on the x direction.
In your notation, I don't see a dependence on x for dphidx. It is a single number since all the space-dependent contributions are integrated out.
Zain Shabeeb
on 10 Jul 2019
Torsten
on 10 Jul 2019
How can I proceed with modelling this system?
First by clarifying the questions - maybe by contacting the authors.
Zain Shabeeb
on 10 Jul 2019
Your model is too much different from the one you want to take as reference. If I were you, I'd start programming your model from scratch.
If z_i (i=1,...,Z) is a discretization for the interval [0;1] and x_j (j=1,...,X) is a disretization for the interval [0;d], you will have to solve for c_k^b at z=z_i (i=1,...,Z) and c_k^hat at z=z_i and x=x_j(i=1,...,Z;j=1,...,X).
Discretize dc_k^b/dz at z=z_i and dc_k^hat/dx with a first-order upwind scheme.
The integral in the computation of dphidx as a single value can be evaluated using the trapezoidal rule, e.g.
This gives Z + X*Z ordinary differential equations that can be solved using ODE15S.
Good luck.
Zain Shabeeb
on 10 Jul 2019
Zain Shabeeb
on 10 Jul 2019
Zain Shabeeb
on 10 Jul 2019
Torsten
on 10 Jul 2019
Should read
c_k^b(i-1)
and
c_k^hat(i,j-1)
thus the solution variables at z_(i-1) resp. (z_i,x_(j-1)).
Zain Shabeeb
on 10 Jul 2019
Edited: Zain Shabeeb
on 10 Jul 2019
Zain Shabeeb
on 10 Jul 2019
Torsten
on 10 Jul 2019
With open questions I mean especially the dphidx-term. I doubt that it is constant for all positions.
Zain Shabeeb
on 10 Jul 2019
Torsten
on 11 Jul 2019
Count the number of variables in the call list to EDIfun2 and in the receive list. They do not coincide.
Zain Shabeeb
on 11 Jul 2019
Zain Shabeeb
on 11 Jul 2019
I don't understand why you can't identify such obvious programming errors on your own.
In EDIfun2, you set
q = y(n+1:n+n*m);
and then use q in
DqDx(i,j) = (q(i,j)-q(i,j-1))/(x(j)-x(j-1));
Don't you see the problem ?
Please don't immediately post every single error - start thinking and use MATLAB's debugger.
Zain Shabeeb
on 11 Jul 2019
Zain Shabeeb
on 11 Jul 2019
q=0 at t=0, and you divide by q when calculating DphiDx.
Again: Print Dydt in EDIfun2 until it contains reasonable values and use the debugger.
Note that according to your model equations, DphiDx does not depend on x and z . In your code from above, it does because you replace the integrand by its value in (z(i),x(j)).
Zain Shabeeb
on 11 Jul 2019
Why should DphiDx be a matrix of 0's ? If you start with q=1, it is a single number different from 0. And if it is a single number that does not depend on z and x, it will affect DJdx.
Note further that you use DqDt(i,j) in your loop before you define it.
Zain Shabeeb
on 11 Jul 2019
Torsten
on 11 Jul 2019
I defined DqDt and DcDt in the function file at the top. Here is the section of code which shows where i defined it:
This is only an initialization of the arrays.
What I mean is
for i = 2:n
for j = 2:m
DcDt(i) = -v*DcDz(i) + (((1-epsilon)/epsilon)*(DqDt(i,j) + DJDx(i,j)));
DqDt(i,j) = a*D*(q(i,j)-(cstar(i,j))) - DJDx(i,j);
end
end
You use DqDt(i,j) in the calculation of DcDt(i) before you define it as
DqDt(i,j) = a*D*(q(i,j)-(cstar(i,j))) - DJDx(i,j);
Zain Shabeeb
on 11 Jul 2019
Torsten
on 11 Jul 2019
If I have q = 0 then I cant divide it in the DphiDx equation. What would you suggest for this problem?
I don't know about the physical background of your problem.
So I can't tell what makes sense here as initial condition for q.
Zain Shabeeb
on 11 Jul 2019
Zain Shabeeb
on 13 Jul 2019
Answers (1)
hbulusoy
on 17 Sep 2019
0 votes
Did you have any luck solving your problem? I'm very interested in this code. I have been trying to figure out to code this model in Python for a while but I am also not getting very good results.
1 Comment
Zain Shabeeb
on 17 Sep 2019
Categories
Find more on Structural Mechanics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
