Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plug Flow Reactor MATLAB code for changing are and straight area #701

Merged
merged 5 commits into from
Jun 5, 2020

Conversation

mgashwinkumar
Copy link
Contributor

Please fill in the issue number this pull request is fixing:

Fixes #

Changes proposed in this pull request:

@mgashwinkumar mgashwinkumar changed the title Add files via upload Plug Flow Reactor MATLAB code for changing are and straight area Aug 20, 2019
@codecov
Copy link

codecov bot commented Aug 20, 2019

Codecov Report

Merging #701 into master will increase coverage by 0.17%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #701      +/-   ##
==========================================
+ Coverage   71.40%   71.57%   +0.17%     
==========================================
  Files         372      372              
  Lines       43605    44499     +894     
==========================================
+ Hits        31134    31849     +715     
- Misses      12471    12650     +179     
Impacted Files Coverage Δ
include/cantera/zeroD/IdealGasReactor.h 50.00% <0.00%> (-50.00%) ⬇️
include/cantera/zeroD/ConstPressureReactor.h 50.00% <0.00%> (-50.00%) ⬇️
...clude/cantera/zeroD/IdealGasConstPressureReactor.h 50.00% <0.00%> (-50.00%) ⬇️
include/cantera/kinetics/Falloff.h 52.50% <0.00%> (-47.50%) ⬇️
include/cantera/kinetics/Kinetics.h 56.75% <0.00%> (-33.49%) ⬇️
include/cantera/numerics/ctlapack.h 77.04% <0.00%> (-22.96%) ⬇️
include/cantera/thermo/MixtureFugacityTP.h 20.00% <0.00%> (-13.34%) ⬇️
include/cantera/thermo/SingleSpeciesTP.h 54.54% <0.00%> (-12.13%) ⬇️
src/base/units.h 93.04% <0.00%> (-6.96%) ⬇️
include/cantera/thermo/NasaPoly2.h 96.00% <0.00%> (-4.00%) ⬇️
... and 55 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b23f184...e9bd282. Read the comment docs.

@mgashwinkumar
Copy link
Contributor Author

What is the status of this pull request. Is there any comments? Is there anything that I should look into?

@bryanwweber
Copy link
Member

Hi! Sorry that no one has responded to you. We haven't had the time to review this yet. Thanks for your patience!

Copy link
Member

@bryanwweber bryanwweber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! Thank you again for this contribution. I have a few concerns broadly about this example, in addition to the specific comments listed below. Please feel free to ask any questions or push back on any suggestion.

  1. Can you provide a short source/description with the derivation of the equations you're using here, particularly the conservation equations for mass, energy, and species?
  2. You are using the equilibrated gas_conv object passed into all your calls. This results in the composition being essentially fixed and nothing much interesting happening (i.e., this could be solved by frozen composition calculations without an ODE I think). Was this intentional? I tried to change the state to the equilibrated temperature, but the initial composition, but the simulation took a long time and I didn't wait for it to finish.
  3. The files that you're adding here should be placed in the samples/matlab folder, please.

Thanks again for your contribution, and please do let me know any questions you have!

@@ -0,0 +1,75 @@
% This code snippet is a sample to model a converging nozzle as
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation for this example should be in the format similar to flame1.m in the samples/matlab folder. In other words, something like

% GENERAL_PLUG_FLOW_REACTOR - One line description
%
%    Longer description goes here indented by four spaces after the
%    comment character

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!!

in2 = speciesIndex(gas_conv,'N2');
nsp = nSpecies(gas_conv);
x = zeros(nsp,1);
% Change the below values for different Phi values of methane Combsution
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small typo here... "Combsution" -> "combustion"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!!

nsp = nSpecies(gas_conv);
x = zeros(nsp,1);
% Change the below values for different Phi values of methane Combsution
x(ich4,1) =0.2899;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make sure the spacing before and after the = sign is consistent with the rest of the code? Also, can you delete the space at the end of the line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!!


%% Converging Section
% The Dimensions and conditions of the reactor are given below
A_in=0.018; % Inlet Area
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer if the different quantities were labeled by a comment above the variable, rather than on the same line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!!

% k = -1 makes the solver solve for converging area.
% Use k = +1 for diverging area. make sure to change the inlet and exit area for diverging area
% Use k = 0 for constant cross sectional area
k=-1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you automatically set k based on the relative values for A_in and A_out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!!

%---density, temperature and mass fractions variations along a plug flow---
%-------------------------reactor------------------------------------------
%--------------------------------------------------------------------------
F(1)=((1-R/cp_mass(gas))*((rho*vx)^2)*(1/A)*(dAdx) + rho*R*sum(MW.*w.*(h-MW_mix*cp_mass(gas)*T./MW))/(vx*cp_mass(gas)) ) / (P*(1+vx^2/(cp_mass(gas)*T)) - rho*vx^2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to add a variable for the value of cp_mass to avoid having to calculate it so many times in these two lines.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should also split this calculation over several lines by defining several variables to hold different pieces of the equation. That will make it easier for people to see how each term is calculated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is easier for me like this to debug. If everything else is approved, I can change this as the last step before you upload it to the main repository

if k~=0
set(gas,'Temperature',T,'Density',rho,'MassFractions',Y); % the gas is set to the corresponding properties during each iteration of the ode loop
else
set(gas,'Temperature',T,'Pressure',P,'MassFractions',Y); % the gas is set to the corresponding properties during each iteration of the ode loop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use pressure to set the state when k = 0? Density should be equally as valid, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I changed it

@@ -0,0 +1,43 @@
function [F] = PFR_solver_CANTERA(x,initial,gas,mdot,A_in,dAdx,k)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of naming this function PFR_solver_CANTERA, can you just name it PFR_solver? I think you should also be able to include it in the PFR_setup.m file, at the end of that file. I'm not that familiar with how MATLAB functions are defined though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes.


if k==1
A = A_in-k*dAdx*x;
elseif k==-1|k==0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My linter suggests using || for this comparison. However, if k = 1, doesn't this result in the area decreasing anyways? I think you have a logic error here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I fixed it

F(2) = (vx*vx/(rho*cp_mass(gas)))*F(1) + vx*vx*(1/A)*(dAdx)/cp_mass(gas) - (1/(vx*rho*cp_mass(gas)))*sum(h.*w.*MW);

for i = 3:nsp+2
F(i) = w(i-2)*MW(i-2)/(rho*vx);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a for-loop, can you do this calculation all at once, and then add the calculated values into the F array?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@bryanwweber
Copy link
Member

@mgashwinkumar Hi 👋 Do you have any questions about the comments that I left here? How to update the code, or anything like that?

@mgashwinkumar
Copy link
Contributor Author

mgashwinkumar commented Dec 3, 2019 via email

@bryanwweber
Copy link
Member

@mgashwinkumar Hello again! Just wondering when/if you'll have a chance to update this code? Thanks!

@mgashwinkumar
Copy link
Contributor Author

@bryanwweber , I will update the code and try to address your comments in two-three weeks.

@bryanwweber
Copy link
Member

@mgashwinkumar thanks! Let me know if you have any questions, I'm happy to help

@mgashwinkumar
Copy link
Contributor Author

@mgashwinkumar thanks! Let me know if you have any questions, I'm happy to help

@bryanwweber , Is there a way to retrieve the version that I uploaded earlier for reference?

@bryanwweber
Copy link
Member

@mgashwinkumar You can go to https://github.com/Cantera/cantera/pull/701/files

and then click the 3 dots, and then "View File" which will let you see the original version.
image

@mgashwinkumar
Copy link
Contributor Author

@bryanwweber , I replied to each of your comment individually. I have uploaded the derivations in a pdf along with this comment. I have also uploaded the new files into samples/matlab of my individual repository. Please let me know if you have any other question or concern.
PFR Derivation.pdf

@bryanwweber
Copy link
Member

@mgashwinkumar Thank you! Unfortunately, I can't see any of the changes here. Did you do them on your local computer? If so, you'll need to git commit them, and then git push them so they're on GitHub. Let me know if you need any help with that!

@mgashwinkumar
Copy link
Contributor Author

mgashwinkumar commented Jun 2, 2020 via email

@bryanwweber
Copy link
Member

@mgashwinkumar No problem! Send me an email to the email on my profile and we can set up a meeting. Tomorrow won't work for me, but Wednesday or Thursday will be a little better.

@bryanwweber
Copy link
Member

@mgashwinkumar Wednesday at 10 would work for me. Can you send a separate Zoom invite by email? I don't think it's a good idea to publish meeting codes publicly like this.

@bryanwweber
Copy link
Member

@mgashwinkumar I made a few more fixes to the files here, let me know if anything seems wrong! https://github.com/Cantera/cantera/pull/701/files

@mgashwinkumar
Copy link
Contributor Author

@bryanwweber , I would like to download the latest version and run it once on my computer to check if everything is fine. Should I download the file from the main cantera repository or the forked copy of mine?

@bryanwweber
Copy link
Member

@mgashwinkumar the forked copy, all the changes got made to those copies

@decaluwe
Copy link
Member

decaluwe commented Jun 3, 2020

Hi, @mgashwinkumar -- thanks so much for submitting this PR - I think it will be well-received and useful for many!

I made a few small changes, mostly to documentation and variable names in a couple places, but otherwise this should be good to go!

@mgashwinkumar
Copy link
Contributor Author

@bryanwweber , @decaluwe , Thank you very much for your inputs, I have a few comments/questions mentioned below:

  1. I tried running the code, my matlab was having trouble with '.yam1'. When I changed it to '.cti', it ran smoothly. I am not sure why this is happening and if this will affect someone who is trying to use this in matlab. If that isn't a problem, I'm fine with this version. What is the next step?
  2. I just added contact info at the end of description file if that's fine.
  3. Just curious, if this is the final version, does it show up on the Cantera Matlab examples webpage ?

@mgashwinkumar
Copy link
Contributor Author

@bryanwweber , Any updates about the final version and the comment I posted yesterday?

mgashwinkumar and others added 5 commits June 4, 2020 21:12
The reactor can have varying area, and therefore, pressure variation
throughout the domain. The example is solved by discretizing the domain
into chunks and solving them as a sequence of 0-D reactors.
Renaming solution vector references.

Renaming 'init' variable, external and internal to the residual function
for greater clarity.  External to the ode call, they are renamed
'inlet_soln', since they correspond to the current volume's inlet state.
Within the ode call, they are renamed 'soln_vector' since they represent
solution at the current location x.
The call to meanMolecularWeight was previously located before the
Cantera gas object was set to the current state. This commit fixes that.
@bryanwweber
Copy link
Member

@mgashwinkumar No problem adding the contact info. The YAML file is something new in Cantera 2.5.0, so I wouldn't expect it to work in your environment unless you compiled Cantera yourself fairly recently. Using the CTI version is fine for your local testing, but since this will go out with Cantera 2.5.0, we'd like it to use the new format. Once we release v2.5.0 (sometime this summer), your example will show up on the website! Thanks again for the contribution, I'm just going to wait for the tests to pass before merging it into master 😃

@mgashwinkumar
Copy link
Contributor Author

@bryanwweber , Thank you very much for your help. I am happy that I contributed to the combustion community. Please let me know when you publish it online.

@bryanwweber bryanwweber merged commit 57b4641 into Cantera:master Jun 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants