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

ABC Simulator #13

Merged
merged 25 commits into from
Mar 12, 2021
Merged

ABC Simulator #13

merged 25 commits into from
Mar 12, 2021

Conversation

kspieks
Copy link
Collaborator

@kspieks kspieks commented Apr 18, 2020

This PR creates a template for adding SA adapters. It implements SA with RMG. I welcome all feedback and look forward to your comments :)

@kspieks kspieks requested a review from alongd April 18, 2020 19:14
@kspieks kspieks self-assigned this Apr 18, 2020
@kspieks
Copy link
Collaborator Author

kspieks commented Apr 18, 2020

Hmm the Travis tests failed due to an import error from RMG-Py even though the master branch of RMG-Py passes all tests. I also tried running coverage run -m pytest -ra, which is the same command travis uses, and it passed locally for me giving the following output

coverage run -m pytest -ra
============================================================ test session starts ============================================================
platform darwin -- Python 3.7.6, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: /Users/kevin/Documents/Green_Lab/RMG/T3, inifile: pytest.ini
collected 18 items                                                                                                                          

tests/test_main.py ................                                                                                                   [ 88%]
tests/test_utils.py ..                                                                                                                [100%]

============================================================ 18 passed in 13.31s ============================================================

I must be missing something obvious. Can you help me understand why these imports are failing?

@alongd
Copy link
Member

alongd commented Apr 18, 2020

T3 uses RMG's binaries from conda...
For now, we can import RMG's master for the tests. Let's make a separate PR for that

@kspieks
Copy link
Collaborator Author

kspieks commented Apr 18, 2020

T3 uses RMS's binaries from conda...
For now we can import RMG's master for the tests. Let's make a separate PR for that

Sorry what files must we update in a separate PR? I'm not seeing where the RMS binaries are specified.

@alongd
Copy link
Member

alongd commented Apr 19, 2020

I had a typo, it's RMG, not RMS. I'll make the PR

@kspieks
Copy link
Collaborator Author

kspieks commented Apr 19, 2020

I had a typo, it's RMG, not RMS. I'll make the PR

Okay thanks for making the PR. Admittedly I'm still confused where RMG's conda binaries entered the travis build so I'll learn from your PR

@kspieks
Copy link
Collaborator Author

kspieks commented Apr 22, 2020

@alongd Can you review this PR when you get a chance?

Copy link
Member

@alongd alongd left a comment

Choose a reason for hiding this comment

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

Thanks for this addition, introducing ABC into T3!!
I added some comments, most are minor.

tandem/sa/adapter.py Outdated Show resolved Hide resolved
tandem/sa/factory.py Outdated Show resolved Hide resolved
tandem/sa/factory.py Outdated Show resolved Hide resolved
tandem/sa/factory.py Outdated Show resolved Hide resolved
run_directory: str,
input_file: str,
threshold: Optional[float] = 0.001,
verbose: Optional[bool] = True,
Copy link
Member

Choose a reason for hiding this comment

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

tandem/sa/__init__.py Outdated Show resolved Hide resolved
tandem/sa/rmg_sa.py Outdated Show resolved Hide resolved
tandem/sa/rmg_sa.py Outdated Show resolved Hide resolved
tests/test_rmg_sa.py Outdated Show resolved Hide resolved
tests/test_rmg_sa.py Outdated Show resolved Hide resolved
@alongd
Copy link
Member

alongd commented Apr 22, 2020

Let's asses what parameters RMS/Cantera require, and make sure we have them in out SAAdapter class. I think the most complicated thing is the reactor type: for RMG we simply use the reactor type from the input file, but here we should let the user specify how to simulate and run SA.

@xiaoruiDong
Copy link

xiaoruiDong commented Apr 22, 2020

Thank you for the PR. I totally support the transition to adapters. However, since sensitivity is also a simulation just with more things to calculate, I don't know if you agree that we should have a simulation class (adapter) first and build SA adapter on that.

Below is a link a cantera sensitivity input example:
https://projects.skill-lync.com/projects/Sensitivity-Analysis-of-GRI30-Mechanism-using-Cantera-36940
To me, the simulation input is just like a subset to the sensitivity input.

Besides, I saw there is an argument (input_file), do we want to pursue a workflow of writing the input file first and then apply it to this SA factory, or it is just the input_file of RMG we used to generate the model?

If the first case, the observable list can be removed (included in the input file). I think it introduces more flexibility. For example, I use range reactor in the model generation, while using a range of different conditions to do sensitivities. Besides, adiabatic conditions are also something I considered for sensitivity. Besides, in combustion community, sensitivity analysis on ignition delay is another thing wanted. This is probably also something we want to add into consideration. So, since we want to build input_files, we need to have a template write. (Jinja2 is something we want to pursue, I guess. Its inheritance function is useful).

If the second case, I feel that we will be limited, but probably let me know your reason first.

@alongd
Copy link
Member

alongd commented Apr 22, 2020

good point @xiaoruiDong, let's make a SimulateAdapter instead, we could have a bool flag for requesting SA in addition.

Regarding the other points, we're just starting to discuss how to use Cantera and RMS, this part isn't ripe yet. Yes, it's possible to ask the user to supply an additional script for the simulation, specifying the reactors.

@kspieks
Copy link
Collaborator Author

kspieks commented Apr 22, 2020

Thank you both for this valuable feedback! I agree that the first case you describe offers better generalizability. I'll start working on a new branch called abc simulate that tries to incorporate these ideas: creates a simulate class that uses an input file separate from the RMG input.py and ARC arc.yml files passed to T3

I have some clarifying questions for you both:

  1. So the big idea is that once T3 has completed model generation, we allow the user to simulate the model with either RMG, RMS, or Cantera? As an additional feature, the user can also specify which software to use when performing SA?

  2. What motivates performing SA for different T, P, or in a different reactor type than what was used to generate the model? I didn't know this was a common need, which is why I hadn't planned for it when writing the SA adapter for this PR.

@kspieks
Copy link
Collaborator Author

kspieks commented Apr 22, 2020

I was in the process of addressing Alon's comments so I just pushed the changes for all resolved conversations

@alongd
Copy link
Member

alongd commented Apr 22, 2020

I think we don't necessarily need a new branch.
In the grand scheme of things, T3 will also be able to generate a report with fluxes, profiles, etc.:
image

Since RMG has limited reactor types (which is OK for model generation), the process being modeled might be different and require a more complicated reactor model.

@kspieks
Copy link
Collaborator Author

kspieks commented Apr 22, 2020

I think we don't necessarily need a new branch.
In the grand scheme of things, T3 will also be able to generate a report with fluxes, profiles, etc.:
image

Since RMG has limited reactor types (which is OK for model generation), the process being modeled might be different and require a more complicated reactor model.

I see. I was unaware that developing a model in reactor A and then simulating in reactor B gave good results. Good to know!

Okay I can keep this branch. I was considering creating a new Simulate class that has a method for performing SA. But I guess is it better organization to have both Simulate and SA be separate classes?

@alongd
Copy link
Member

alongd commented Apr 22, 2020

I agree with the first approach. We could have one (ABC) class, say SimulateAdaptor, that has many methods such as SA, UA, species profiles, fluxes... and at the core of all these features (I think) is simulating the reactor(s).

@kspieks
Copy link
Collaborator Author

kspieks commented Apr 22, 2020

I agree with the first approach. We could have one (ABC) class, say SimulateAdaptor, that has many methods such as SA, UA, species profiles, fluxes... and at the core of all these features (I think) is simulating the reactor(s).

I see. Thanks for clarifying. So would simulate just be an additional method that SA, UA, etc would call?

As another question, how much flexibility are we looking for? Would SA, UA, etc actually be abstract methods in an ABC SimulateAdapter? Say we have 3 adapters: RMG, RMS, Cantera. Each has methods for simualte, SA, UA, etc. Normally, we would use the factory to create one instance of the needed adapter. I'm thinking it would be cumbersome for a user to simulate with Cantera and do SA with RMS with this set up. But perhaps this isn't necessary?

@codecov-io
Copy link

codecov-io commented Apr 22, 2020

Codecov Report

Merging #13 into master will increase coverage by 5.28%.
The diff coverage is 61.05%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #13      +/-   ##
==========================================
+ Coverage   44.18%   49.46%   +5.28%     
==========================================
  Files           4        8       +4     
  Lines         817      849      +32     
  Branches      238      238              
==========================================
+ Hits          361      420      +59     
+ Misses        400      363      -37     
- Partials       56       66      +10     
Impacted Files Coverage Δ
tandem/main.py 50.23% <33.33%> (+3.80%) ⬆️
tandem/sa/factory.py 50.00% <50.00%> (ø)
tandem/sa/rmg_sa.py 61.64% <61.64%> (ø)
tandem/sa/adapter.py 75.00% <75.00%> (ø)
tandem/sa/__init__.py 100.00% <100.00%> (ø)
tandem/logger.py 25.96% <0.00%> (+5.76%) ⬆️

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 1b9fd37...6588b14. Read the comment docs.

@kspieks kspieks added the WIP This is currently work-in-progress label May 5, 2020
@kspieks kspieks changed the title Abc sa [WIP]: Abc sa May 5, 2020
@kspieks
Copy link
Collaborator Author

kspieks commented May 26, 2020

@alongd Can you take a look at this draft of the RMGSimulator when you get a chance?

Copy link
Member

@alongd alongd left a comment

Choose a reason for hiding this comment

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

Looking good! I added a minor comment, let's work on the input file PR (I'll push soon) so we'll have the correct inputs for these adapters

sa_rtol (Optional[float]): The relative tolerance used when running SA.
verbose (Optional[bool]): Whether or not to log to file.

======================= ====== ====================================================
Copy link
Member

Choose a reason for hiding this comment

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

we don't use this style for attributes in T3 (we do in RMG), you can have an Attributes: section in a similar format to Args:

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for reviewing! I just pushed the updated format for documenting the attributes. I think the pyrms simulator adapter is ready as well, just needs SA to be working in rms. I'm happy to help with the T3 input file in the meantime :)


Returns:
A SA dictionary, whose structure is given below.
sa_dict = { 'thermo' :
Copy link
Member

Choose a reason for hiding this comment

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

excellent, thanks!

kspieks added 24 commits March 9, 2021 13:30
Run SA using the simulate adapter rather than the previous `run_sa()` method. 

Remove code from `run_sa()` that is now part of the `get_sa_coefficients()` method in the RMG adapter. 

Remove the corresponding imports in main.py that have now been moved to the RMG Simulator Adapter.
Update environment.yml to include pyrms and Matt's channel

Add scripts to organize the installation of ARC, Julia 1.4, RMS, and pycall.

Note: the pyrms conda package is currently for v0.2.1. SA is functional only on v0.3.0 or later and we must use RMS v0.3.2 to fix the constantPDomain error
@alongd
Copy link
Member

alongd commented Mar 12, 2021

@kspieks, everything looks good to me. Are we good to merge this in?

@kspieks
Copy link
Collaborator Author

kspieks commented Mar 12, 2021

@alongd Yes I believe so. Thanks again for all of your helpful feedback in developing this PR. I'm happy to look at PR #69 after this is merged in as well as discuss other next steps

Copy link
Member

@alongd alongd left a comment

Choose a reason for hiding this comment

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

@kspieks, Thank you. This is a well deserved merge: a seminal PR which reflects a significant improvement in T3's abilities and flexibility. Thanks for the professional work! 🥇

@alongd alongd merged commit 2fa9199 into master Mar 12, 2021
@alongd alongd deleted the abc_sa branch March 12, 2021 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants