From 47ea421c29c9274ec4a0c5c85c2dc83a6b48074a Mon Sep 17 00:00:00 2001
From: David Seus <david.seus@ians.uni-stuttgart.de>
Date: Tue, 3 Sep 2019 09:58:00 +0200
Subject: [PATCH] explain implementaiton details of calc_gli_term method and
 recheck.

---
 LDDsimulation/domainPatch.py | 100 +++++++++++++++++++++++++++++++++--
 1 file changed, 97 insertions(+), 3 deletions(-)

diff --git a/LDDsimulation/domainPatch.py b/LDDsimulation/domainPatch.py
index 8eed97a..f2bbad1 100644
--- a/LDDsimulation/domainPatch.py
+++ b/LDDsimulation/domainPatch.py
@@ -293,6 +293,8 @@ class DomainPatch(df.SubDomain):
                         for dof_index, dof_coord in facet_dof_coord_dict.items():
                             dof_coord_tupel = (dof_coord[0], dof_coord[1])
                             save_dict.update({dof_coord_tupel: self.pressure[phase].vector()[dof_index]})
+        else:
+            pass
 
 
     def governing_problem(self, phase: str) -> tp.Dict[str, fl.Form]:
@@ -503,6 +505,7 @@ class DomainPatch(df.SubDomain):
                     corresponding_dof_index = self.interface_corresponding_dof_index[interf_ind][phase]
                     for local_facet_ind, normal in interface.outer_normals[subdomain].items():
                         gli_dofs_along_facet = gli_dof_coordinates[local_facet_ind]
+                        global_facet_ind = local2global_facet[local_facet_ind]
                         for gli_dof_index, gli_dof_coord in gli_dofs_along_facet.items():
                             flux_x_dof_index = corresponding_dof_index[local_facet_ind][gli_dof_index]["flux_x"]
                             flux_y_dof_index = corresponding_dof_index[local_facet_ind][gli_dof_index]["flux_y"]
@@ -516,7 +519,6 @@ class DomainPatch(df.SubDomain):
 
                             # save this newly calculated dof to the interface
                             # dictionary gli_term_prev
-                            global_facet_ind = local2global_facet[local_facet_ind]
                             dof_coord_tupel = (gli_dof_coord[0], gli_dof_coord[1])
                             interface.gli_term_prev[subdomain][phase][global_facet_ind].update(
                                 {dof_coord_tupel: gl0.vector()[gli_dof_index]}
@@ -535,6 +537,98 @@ class DomainPatch(df.SubDomain):
         the iteration number of the subdomain and return it as a dolfin Function
         used for assembling the governing form in self.govering_problem. Additionally,
         save the newly computed gli_term to the gli dictionries of the interface.
+
+
+        PARAMETERS
+        ------------------------------------
+        interface_index    int       interface index for which to calculate the
+                                     gli term.
+        phase              str       phase to calculate the gli term for.
+        # DEBUG:           bool      toggle the debug output.
+
+        IMPLEMENTATION DETAILS
+        Each interface has several dictionaries to store the gli terms and interface
+        pressures. Let interface be the interface with the index interface_index.
+        For each phase in self.has_phases we have four dictionaries
+
+        interface.gli_term[subdomain][phase],
+        interface.gli_term_prev[subdomain][phase],
+        interface.gli_term[neighbour][phase],
+        interface.gli_term_prev[neighbour][phase],
+
+        storing the gli term of the current iteration as well as the previous one,
+        for both the subdomain and its neighbour that it shares the interface with.
+        The same holds true for the pressure terms.
+        This is needed because the neighbouring subdomain (relative to the subdomain
+        we are currently on) might still need the gli term and the pressures of
+        the previous iteration depending on wether or not it is one iteration ahead or not.
+        So, depending on the current iteration number self.iteration_number (which
+        is updated by the solver method LDDsimulation.LDDsolver) in relation to
+        the iteration number of the neighbouring subdomain, we choose different
+        dictionaries to read the neighbouring previous pressures from.
+        Similarly the dictionaries used to save the newly computed gli terms must
+        be chosen appropriately.
+
+        The paradigm for reading the neighbouring gli_terms is, that the correct
+        terms we need to read are ALWAYS stored in interface.gli_term_prev.
+        The self.calc_gl0_method e.g. stores the gl0 values in the interface.gli_term_prev
+        dictionaries.
+
+        Baring in mind, that for each iteration the solver loops over the subdomains,
+        and the first thing it does is raise the iteration number of the subdomain
+        it tackles, we know that the iteration numbers can only differ by at most 1.
+        We get the follwing two cases:
+
+        a) subdomain_iteration == neighbour_iter_num + 1:
+            subdomain and neighbour started with the same iteration number when the
+            solver entered the new iteration. The solver raised the subodomain iteration
+            number by 1, so our subdomain is now ahead of the neighbouring subdomain.
+            The following steps are carried out.
+            1. step: read previous neighbouring pressure values from interface
+            dictionary. Since in this case the neighbour has not yet iterated beyond
+            the iteration we are currently in,
+
+                pressure_prev = interface.pressure_values[neighbour][phase]
+
+            holds the "previous pressure" we need.
+            2. step: choose the dictionary to save the gli_term in once they are
+            calculated.  Since in this case, the neighbour has not done the next
+            iteration the neighbouring subdomain will need the values stored in
+
+                interface.gli_term_prev[subdomain][phase]
+
+            of the current subdomain. Therefore we cannot overwrite these values
+            and save the newly calculated gli_terms to
+
+                gli_save_dictionary = interface.gli_term[subdomain][phase]
+
+        b) subdomain_iteration == neighbour_iter_num:
+            1. step: read previous neighbouring pressure values from interface
+            dictionary. In this case the neighbour has iterated once beyond the
+            iteration we are currently in, so
+
+                pressure_prev = interface.pressure_values_prev[neighbour][phase]
+
+            holds the "previous pressure", since the LDDsolver always writes
+            newly calculated pressures to the dictionary interface.pressure_values.
+            2. step: choose the dictionary to save the gli_term in once they are
+            calculated.
+            In this case, the neighbour has done the next iteration already
+            will has already used the previous gli term stored in gli_term_prev
+            of the current subdomain. Therefor, we save the the newly calculated
+            gli terms to
+
+                gli_save_dictionary = interface.gli_term_prev[subdomain][phase]
+
+            After calculating gli:
+            Now, since our neighbour previously was in situation a) it saved the
+            gli_term to interface.gli_term[neighbour] and this will be overwritten
+            in the next step, if we don't save it to gli_term_prev of the neighbour.
+            Similarly for the pressure values. So in the present case, we need to
+            save interface.gli_term[neighbour][phase] to
+            interface.gli_term_prev[neighbour][phase]
+            and interface.pressure_values[neighbour][phase] to
+            interface.pressure_values_prev[neighbour][phase]
         """
         # this is the number of the current iteration of the subdomain.
         iteration = self.iteration_number
@@ -560,7 +654,7 @@ class DomainPatch(df.SubDomain):
                 # in this case the neighbour has not yet iterated beyond
                 # the iteration we are currently in, so
                 # interface.pressure_values[neighbour] holds the "previous pressure"
-                # we need to read from the interface to calculate the current gli.
+                # we need to read these values from the interface to calculate the current gli.
                 pressure_prev = interface.pressure_values[neighbour][phase]
 
                 # choose dictionary to save the newly calculated dofs to.
@@ -862,7 +956,7 @@ class DomainPatch(df.SubDomain):
                         )
 
 
-    def _calc_corresponding_dof_indices(self, debug=False):
+    def _calc_corresponding_dof_indices(self, debug=True):
         """ calculate dictionary which for each interface and each phase holds
         for each facet index, the dof indices of the pressures and flux components
         corresponding to a given dof index of the gli function.
-- 
GitLab