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

Replace convenience classes by C++ Solution object #735

Merged
merged 9 commits into from
Nov 8, 2019

Conversation

ischoegl
Copy link
Member

@ischoegl ischoegl commented Oct 27, 2019

This PR introduces the C++ Solution object (previously only instantiated from the Python front-end) to the C++ test suite and examples.

Changes proposed in this pull request:

  • implement initialization of C++ Solution objects from input files
  • replace convenience wrapper classes (i.e. IdealGasMix, Interface) for test_problems and samples
  • deprecate convenience wrappers (instead of porting them to YAML/AnyMap)

@ischoegl ischoegl force-pushed the deprecate-convenience-classes branch 5 times, most recently from ec82d24 to 9d9f588 Compare October 27, 2019 14:30
@ischoegl ischoegl changed the title Replace convenience classes by C++ Solution Replace convenience classes by C++ Solution object Oct 27, 2019
@ischoegl ischoegl force-pushed the deprecate-convenience-classes branch 5 times, most recently from c519b86 to 5b3c41c Compare October 28, 2019 23:50
@ischoegl ischoegl force-pushed the deprecate-convenience-classes branch from db19dad to 5b3c41c Compare November 1, 2019 17:13
Copy link
Member

@speth speth left a comment

Choose a reason for hiding this comment

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

I'm very much in favor of getting rid of these supposed "convenience" classes, and I think this is a good start to extending the use of the Solution class.

I don't understand the need for the methods getIdealGasPhasePtr and getGasKineticsPtr - I don't think there are any important methods of these classes that aren't available from a pointer to the base class. For surface phases / kinetics, there are a few unique methods like getCoverages, but I'd be inclined to handle those by having a class analogous to Solution which always returns instances of SurfPhase and InterfaceKinetics. Naming this class Interface might be slightly complicated by the existing, now-deprecated Interface class, though.

After seeing the Solution class in action a bit more, I'm wondering if it would make more sense for the thermo() method and friends to return shared_ptr and avoid the need to also having a thermoPtr() (etc.) method. If you then wanted a reference, you could always just write auto& gas = *sol.thermo();.

include/cantera/base/Solution.h Outdated Show resolved Hide resolved
@ischoegl
Copy link
Member Author

ischoegl commented Nov 2, 2019

I don't understand the need for the methods getIdealGasPhasePtr and getGasKineticsPtr

This is true, but for some of the older examples, having to type

std::dynamic_pointer_cast<IdealGasPhase>(sol->thermoPtr())

and

std::dynamic_pointer_cast<GasKinetics>(sol->kineticsPtr())

every time is pretty onerous. As I didn't want to introduce some of the nastier looking C++ 11 features in the simple example, I decided to create the 'convenience' accessors.

The main issue is that a more straight-forward type cast of a pointer to the base class is currently blocked here

Phase(const Phase&) = delete;
Phase& operator=(const Phase&) = delete;

After seeing the Solution class in action a bit more, I'm wondering if it would make more sense for the thermo() method and friends to return shared_ptr and avoid the need to also having a thermoPtr() (etc.) method.

Agreed. From what I've seen, there isn't a way around having shared_ptr's. I also agree that both aren't necessary, although sometimes one or the other is more convenient. I named things such that it is immediately clear what is an object and what is a (shared) pointer.

If you want me to switch to shared_ptr only, now is the time ...

Ingmar Schoegl added 6 commits November 2, 2019 15:09
- implement constructor that loads ThermoPhase, Kinetics, and Transport
from input files (wrapping factory class methods)
- logic for selection of transport manager follows Python object
- add convenience methods to type-cast frequently used classes
- Edge.h, IncompressibleSolid.h and Metal.h
@ischoegl ischoegl force-pushed the deprecate-convenience-classes branch from 5b3c41c to bcde29d Compare November 2, 2019 20:09
@ischoegl
Copy link
Member Author

ischoegl commented Nov 2, 2019

@speth ... alright. I consolidated everything to shared_ptr. Since the C++ Solution class is brand-new, it's probably better to do this now.

@ischoegl ischoegl requested a review from speth November 2, 2019 22:19
@speth
Copy link
Member

speth commented Nov 5, 2019

This is true, but for some of the older examples, having to type

std::dynamic_pointer_cast<IdealGasPhase>(sol->thermoPtr())

[...] every time is pretty onerous.

Under what circumstances is a shared_ptr<IdealGasPhase> needed rather than a shared_ptr<ThermoPhase>? The only case I see is as the argument to the StFlow constructor, and in that case I think the preferred fix might be to change it to accept a ThermoPhase* and then cast that to IdealGasPhase* internally (and throw if that fails).

The main issue is that a more straight-forward type cast of a pointer to the base class is currently blocked here

Phase(const Phase&) = delete;
Phase& operator=(const Phase&) = delete;

Can you give an example of what you think is being blocked? The quoted code should not affect any pointer casting, but just disable copying and assignment.

@ischoegl ischoegl force-pushed the deprecate-convenience-classes branch from 8043e5b to cfb54d4 Compare November 5, 2019 14:34
@ischoegl
Copy link
Member Author

ischoegl commented Nov 5, 2019

@speth ... yes, there really isn't any special feature in IdealGasPhase where dynamic polymorphism doesn't already do the trick. I.e. I removed all of the accessors (i.e. getIdealGasPhase and similar). In some instances, a dynamic_pointer_cast is still necessary (e.g. for the freeflame sample, and the surfkin test problem; I think it's ok to let users figure those out)

The only case I see is as the argument to the StFlow constructor, and in that case I think the preferred fix might be to change it to accept a ThermoPhase* and then cast that to IdealGasPhase* internally (and throw if that fails).

I haven't dont that yet, but it's straight-forward. Edit: Done. The one case where a fix is less intuitive is the surfkin test problem, where coverages etc. aren't defined in the base class. Not sure whether it's worth creating a virtual method for setCoverages in the base class.

Regarding the blocked type cast, I misspoke. It appears to be difficult to change the type once a variable is passed by reference, but casting of pointers works regardless (I guess my original point is moot). Since much of the passing happens by reference, I was overly cautious.

- allow for 'ThermoPhase`, and cast to 'IdealGasPhase' internally
@ischoegl ischoegl force-pushed the deprecate-convenience-classes branch from e4d2942 to 07e0e2f Compare November 5, 2019 22:23
Copy link
Member

@speth speth left a comment

Choose a reason for hiding this comment

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

Thanks, this is looking pretty good. I mostly have just a few suggestions for simplifications and a couple of minor formatting nitpicks.

samples/cxx/demo.cpp Outdated Show resolved Hide resolved
include/cantera/base/Solution.h Outdated Show resolved Hide resolved
include/cantera/base/Solution.h Outdated Show resolved Hide resolved
include/cantera/zeroD/Reactor.h Outdated Show resolved Hide resolved
src/base/Solution.cpp Outdated Show resolved Hide resolved
src/base/Solution.cpp Outdated Show resolved Hide resolved
src/oneD/StFlow.cpp Outdated Show resolved Hide resolved
test_problems/mixGasTransport/mixGasTransport.cpp Outdated Show resolved Hide resolved
test_problems/multiGasTransport/multiGasTransport.cpp Outdated Show resolved Hide resolved
include/cantera/base/Solution.h Outdated Show resolved Hide resolved
@ischoegl
Copy link
Member Author

ischoegl commented Nov 7, 2019

If we consolidate these methods, then there's never any question about whether to use Solution::create or newSolution, or why there's this constructor that can't be called.

That's a fair point. Will update once our PROCI manuscript is submitted (or I need to take a break) ...

@ischoegl ischoegl force-pushed the deprecate-convenience-classes branch from 8f8f297 to 6693eff Compare November 8, 2019 16:16
@ischoegl
Copy link
Member Author

ischoegl commented Nov 8, 2019

Done, i.e. newSolution(infile, name, transport, adjacent) is now the default way to instantiate.

@codecov
Copy link

codecov bot commented Nov 8, 2019

Codecov Report

Merging #735 into master will increase coverage by 0.01%.
The diff coverage is 94.85%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #735      +/-   ##
==========================================
+ Coverage   70.86%   70.87%   +0.01%     
==========================================
  Files         374      372       -2     
  Lines       43683    43684       +1     
==========================================
+ Hits        30954    30963       +9     
+ Misses      12729    12721       -8
Impacted Files Coverage Δ
include/cantera/transport/TransportBase.h 11.11% <ø> (ø) ⬆️
test_problems/surfSolverTest/surfaceSolver.cpp 76.07% <ø> (ø) ⬆️
include/cantera/kinetics/Kinetics.h 56.75% <ø> (ø) ⬆️
include/cantera/thermo/Phase.h 100% <ø> (ø) ⬆️
test_problems/surfSolverTest/surfaceSolver2.cpp 78.1% <ø> (ø) ⬆️
include/cantera/kinetics/InterfaceKinetics.h 28.57% <ø> (ø) ⬆️
include/cantera/oneD/StFlow.h 96.03% <ø> (ø) ⬆️
include/cantera/thermo/IdealGasPhase.h 96.87% <ø> (ø) ⬆️
include/cantera/thermo/SurfPhase.h 100% <ø> (ø) ⬆️
test_problems/VPsilane_test/silane_equil.cpp 66.66% <ø> (ø) ⬆️
... and 19 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 43d02e9...6693eff. Read the comment docs.

@ischoegl ischoegl requested a review from speth November 8, 2019 16:58
@speth speth merged commit 91c7bf1 into Cantera:master Nov 8, 2019
@ischoegl ischoegl deleted the deprecate-convenience-classes branch December 16, 2019 15:55
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