Conversation
0f7d6b4 to
5691afc
Compare
3ab3666 to
9769e5e
Compare
9769e5e to
56592d6
Compare
|
@speth ... this is ready for a review. A large portion of the line count is due to splitting the header generation from the CLib source generation. There are a couple of loose ends still, but I am trying to keep individual PR's manageable. As an aside: both failing tests appear to be due to a MATLAB regression. |
|
|
||
| /// <summary> | ||
| /// Gets or sets the mass fractions for all the species at once, | ||
| /// in the order in which they appear in this collection, | ||
| /// but does not normalize them. | ||
| /// </summary> | ||
| public void SetUnnormalizedMassFractions(double[] fractions) | ||
| { | ||
| var retval = LibCantera.thermo_setMassFractions(_handle, | ||
| (nuint) fractions.Length, fractions, InteropConsts.False); | ||
|
|
||
| InteropUtil.CheckReturn(retval); | ||
| } |
There was a problem hiding this comment.
Does the "experimental" CLib have a way of setting unnormalized mass/mole fractions?
There was a problem hiding this comment.
It does not have this at the moment. I anticipate that this will come up again in the context of MATLAB ... I am trying to keep some mode switches out of the interface implementation. Also, while I know that it is a needed function, I view it as a relatively odd implementation detail. If we can avoid exposing this in user-facing API's, I'd be in favor of alternatives.
There was a problem hiding this comment.
I think we will need to provide this function. It's essential for users who want to use their own integrators.
56592d6 to
f7141cf
Compare
|
@speth … thank you for the review. I either addressed your suggestions or left notes for follow-up work. |
There was a problem hiding this comment.
@ischoegl Thanks for keeping the .NET interface code generation and hand-built portions updated! I’ve added a few comments below. Addtionally, I would recommend removing the .h part of the generated file names since we are no longer using the CLib header files to inform the C# Code generation. For example Interop.Handles.Reaction.g.cs or Interop.Handles.ctrxn.g.cs instead of Interop.Handles.ctrxn.h.g.cs
| readonly SolutionHandle _sol; | ||
|
|
There was a problem hiding this comment.
I would remove this, since we never access it. (In .NET, the mere ownership of an object will not ensure it is cleaned up. That’s the job of the Garbage Collector, and for non-GC items like objects on the native Cantera side, we need to leverage the dispose pattern for deterministic release of the resources)
| readonly SolutionHandle _sol; |
There was a problem hiding this comment.
I'm a little uncomfortable deleting this object. The way the CLib Solution destructor is implemented, it will clean up associated ThermoPhase objects. I am not sufficiently familiar with the C# garbage collection to be sure that there aren't unintended side effects. Fwiw, here's the generated CLib destructor:
int sol3_del(int handle)
{
// destructor
try {
auto obj = SolutionCabinet::at(handle);
// remove all associated objects in reversed order
if (obj->transport()) {
int index = TransportCabinet::index(*(obj->transport()), handle);
if (index >= 0) {
TransportCabinet::del(index);
}
}
if (obj->kinetics()) {
int index = KineticsCabinet::index(*(obj->kinetics()), handle);
if (index >= 0) {
KineticsCabinet::del(index);
}
}
if (obj->thermo()) {
int index = ThermoPhaseCabinet::index(*(obj->thermo()), handle);
if (index >= 0) {
ThermoPhaseCabinet::del(index);
}
}
SolutionCabinet::del(handle);
return 0;
} catch (...) {
return handleAllExceptions(-1, ERR);
}
}There was a problem hiding this comment.
Keep it for now, see comment below!
| _sol = LibCantera.sol3_newSolution(filename, phaseName ?? "", "none"); | ||
| _handle = LibCantera.sol3_thermo(_sol); |
There was a problem hiding this comment.
Until we add a proper Solution class, we only need to temporarily create the solution object on the native side (it also appears that it would be safe to destroy it once we extract the thermo object because it uses shared pointers). And to ensure the native solution object is released, we should dispose the handle by leveraging the using statement (also guaranteeing its release if an exception is thrown later in the constructor). Also, we need to call sol.EnsureValid() before using the handle to get the proper error message in case of a missing file.
| _sol = LibCantera.sol3_newSolution(filename, phaseName ?? "", "none"); | |
| _handle = LibCantera.sol3_thermo(_sol); | |
| using var sol = LibCantera.sol3_newSolution(filename, phaseName ?? "", "none"); | |
| sol.EnsureValid(); | |
| _handle = LibCantera.sol3_thermo(sol); |
(this assumes we remove the _sol field above)
There was a problem hiding this comment.
See above: I'm not sufficiently familiar - this may introduce side effects that go beyond my wheelhouse.
Edit: As .NET would access the storage in CLib, I'm pretty certain that _sol needs to remain. The ThermoPhase handles will be gone as soon as the Solution object is destroyed.
There was a problem hiding this comment.
@ischoegl I realized the handle handling is messed up in a number of ways. For example, right now when a ThermoPhaseHandle is disposed in .NET it calls thermo3_del(handle). But thermo3_del takes no arguments! I am surprised this doesn’t crash. Same problem for KineticsHandle and TransportHandle.
So I think we should merge this now, and I’ll create a follow-up to fix the these handles. In truth ThermoPhaseHandle, KineticsHandle, and TransportHandle shouldn’t be treated as such because they don’t identify an owned resource which can be closed independently. It will require some thinking on how the handle classes are scaffolded and which ones to scaffold at all, and how the wrapper classes are scaffolded.
4b3a6b0 to
65db458
Compare
|
Hi @burkenyo ... I truly appreciate you looking over the C# code - while I was apparently able to keep things from breaking, I don't really have much prior exposure to C#. Regarding the 32 and 64 bit types, I am planning to dedicate a PR to a migration to Other than that: I didn't touch the error logic (some of that comes out of CLib, some of that is ancient C++ code), and I am fairly certain that the |
65db458 to
55b7d90
Compare
55b7d90 to
2a80fc3
Compare
Regarding the ‘.h’ …. Technically, there aren’t any headers parsed but things are still based on header information (they are not written explicitly but are implicit to the implementation). I certainly can rename though. |
Let's keep the filenames for now so we can merge this PR. :) |
Changes proposed in this pull request
This PR refactors
sourcegenfor clarityHeaderGeneratorfromCLibSourceGenerator(which accounts for a large portion of the diff)clib_experimentalrather than the traditionalclibfor .NETsamples/clib_experimental/demo.cexampleFor the
dotnet buildstep, the current approach uses a Doxygen tree that is saved as an artifact.A secondary refactoring step to make the file structure more Pythonic will be addressed in a follow-up.
If applicable, fill in the issue number this pull request is fixing
Addresses Cantera/enhancements#220
If applicable, provide an example illustrating new features this pull request is introducing
Checklist
scons build&scons test) and unit tests address code coverage