I am working on some molecular simulation code, and am hitting a road block on figuring on a good design structure to avoid memory allocation for a container that will store different concrete subtypes of a single abstract supertype. This is related to evaluation of potential energy functions within the code base.
For instance, when evaluating the energy for various molecular interactions, the type structure I have implemented looks something like:
abstract type PairPotential end
struct LJPair <: PairPotential end
struct MiePair <: PairPotential end
And then I would like to store these concrete structs in a single place to be used for potential evaluation later on. My approach would look something like this, where a Dict
with integer keys holds vectors of PairPotential
for given particle types:
struct InteractionInfo
pairs :: Dict{Int, Vector{PairPotential}}
end
However, as is well known, if I were to extract the vector of PairPotentials
for a given key and call some methods on those concrete structs (e.g. LJPair
, MiePair
, etc…), I run into memory allocation issues since the field is a vector of abstract types.
My question is, is there a straightforward design approach in Julia to avoid this type of issue? I would have a difficult time templating the InteractionInfo
struct on the various potentials since a variable number may be present for any given run. From my somewhat limited C++ experience, it seems like a solution to this issue would be to store a vector of pointers to classes that all inherit from a similar base-class. Is this type of setup possible within Julia to avoid the extra memory allocations?
Thanks!