Polymorphic injectors, external currents, and diagnostics#377
Polymorphic injectors, external currents, and diagnostics#377JamesMcClung wants to merge 26 commits intopsc-code:mainfrom
Conversation
germasch
left a comment
There was a problem hiding this comment.
Overall, I definitely like the clean up that comes by adding things after the constructor, and specifically having a list of external currents / injectors, etc, that makes it cleaner (including the default case of nothing) and more powerful.
I do have one little issue with storing the raw pointers / leaking the objects (not that the leaking as such matters, but I don't think it's too hard to prevent that).
What I'm somewhat less sure of is whether there's really much gained by having polymorphism here, in particular since essentially all the base type does is specify a given call signature, which could be done via std::function<> directly (if one were to rename the methods back to operator(), which I think I'd still prefer). I don't know, I haven't fully thought this through, in particular questions of ownership / context / copying (or avoiding them), so that's something we can discuss later, after this is merged.
src/include/boundary_injector.hxx
Outdated
| void inject(Mparticles& mprts, MfieldsState& mflds) override | ||
| { | ||
| (*this)(mprts, mflds); | ||
| } | ||
|
|
There was a problem hiding this comment.
I may learn more as I go through more commits, but at this point I'm wondering, why not stick with operator()?
There was a problem hiding this comment.
I would say explicit method names helps with discoverability: one can search for implementations of named methods, but not for operator(). That's also a benefit of using polymorphism rather than templates or outright type erasure (à la std::function).
There was a problem hiding this comment.
Following my own logic, I should get rid of the lambda wrappers and force users to make their own subclasses. That would be better future-proofed against more methods, too (such as requiring subclasses to provide logging-related information).
There was a problem hiding this comment.
I guess I'm not entirely clear on my own thinking here, though I think it's nice isolating users from having to do their own subclasses -- though admittedly lambda functions are likely an even more unfamiliar feature to a casual user...
Anyway, I guess what's there currently (class from lambda) is fine for the time being.
src/include/psc.hxx
Outdated
| std::vector<InjectorBase<Mparticles, MfieldsState>*> injectors; | ||
|
|
There was a problem hiding this comment.
I think I read somewhere that ideally modern C++ shouldn't use explicit pointers at all. The question here is, what guarantees that the objects pointed to don't go away?
There was a problem hiding this comment.
I kinda felt like using fancy pointer wrappers would be more performative than actually beneficial, since the psc integrator may as well be a singleton, and an entire case revolves around calling integrate() once. Thus the leaking :^)
But I'll go back and do the right thing...
There was a problem hiding this comment.
Actually, what would be the best option, if not unique_ptr?
There was a problem hiding this comment.
As I'm working on switching to unique_ptrs, I'm finding it annoying to have to dynamically allocate all the components. I'm leaning back towards keeping raw pointers, since it's nicer to just stack-allocate things.
There was a problem hiding this comment.
I guess I haven't looked at the various places where this is being used, the ones I saw initially had a new anyway. I think transferring ownership is cleaner. I suppose one could have add_injector() or whatever dynamically copy construct the thing, which would hide the new, but at the expense of needing the injectors to be copy-constructible.
The other alternative I can think of is to just keep a reference, ie., punt on the ownership question -- the one thing that's nice about that is that it guarantees non-null, but generally speaking, I think clear ownership rules are preferable. And I can kinda think of test cases where one might want to have to integrators going at the same time for some kind of cross checking, where it'd generally be nice to not assume singleton behavior, though it's kinda far fetched.
There was a problem hiding this comment.
The news were there to avoid having to stack-allocate on a separate line, which references can't do. I realize that isn't a good justification for raw pointers. I'll try to find a better solution that achieves clear ownership without introducing excessive boilerplate.
| psc.injectors.push_back( | ||
| new BoundaryInjector<ParticleGenerator, typename PscConfig::PushParticles>( | ||
| ParticleGenerator(1, 1), grid)); |
There was a problem hiding this comment.
I guess this answers the question about the injectors not going away, essentially because they're being leaked ;) Obviously not a big issue as such, but I think unique_ptr may be the better option here.
Another thing I don't fully like is that the injectors are added after the fact, which kinda means that the integrator isn't fully set up after being constructed. But I think it's acceptable here, in that an empty list of integrators is a valid state.
There was a problem hiding this comment.
And I would have to agree that, as we currently do, passing N objects to the constructor makes that rather ugly...
There was a problem hiding this comment.
The partially-constructed state of the integrator makes me uncomfortable, too... and I was only planning to make it worse over the next few weeks. This seems like a perfect place to use the builder pattern, but it might be a while before that end state is reached.
|
Some other things I'd like to build into these components are:
|
This is the first of (hopefully) several PRs aiming to simplify the process of making a PSC integrator, while also making it more flexible.
As the title says, this PR makes injectors, external currents, and diagnostics inherit from a base class, and the PSC integrator has lists of pointers to each of those base classes.
Benefits of this approach:
CompositeInjectorandDiagnosticsDefaultExternalCurrentsNoneandInjectParticlesNoneDownsides of this approach:
It seems to me that every component of the PSC integrator could be polymorphic instead of templated, and that is one of the long-term goals of this PR chain.