uw-comphys / openccm

OpenCCM is a CFD-based compartment modelling software package. It is primarily intended for convection dominated reactive flows which feature a weak or one-way coupling between the reactive species and the carrier fluid, i.e. the reaction does not substantially influence the fluid flow over the course of the simulation.
https://uw-comphys.github.io/openccm/
GNU Lesser General Public License v2.1
0 stars 3 forks source link

Speed Up Compartmentalization and Immutable CMesh #29

Closed Alex-Vasile closed 5 months ago

Alex-Vasile commented 5 months ago

This PR does two related things:

  1. It uses the elements_not_in_a_compartment set inside _calculate_compartments to avoid having to change the element connectivity inside the CMesh object, allowing it to be immutable and less error prone.
  2. It Further speeds up _calculate_compartments by changing seeds from being a List (poorly chosen on my part) to the keys in an OrderedDict.

Some more explanation on point 2. The seed elements are initially specified by the user, but the collection gets expanded upon by the _calculate_compartments as each compartment is grown. It involved tens of thousands of evaluations of if x not in seeds on large meshes, which is O(N) in time where N is the number of seed elements. N can easily get into the tens of thousands.

A replacement to List needed: 1) constant time lookup, 2) maintain insertion order, and 3) a way to pop items from the front of the insertion order. Alternative options could have been Set, Dict, or Deque but none had all three.

Instead the keys of an OrderedDict are used, which satisfies all 3 criteria. The value used for each key is None in order to keep the total size to a minimum.