From d86f1eaad44db1a5538c5d047a5aa2cc9e7dc919 Mon Sep 17 00:00:00 2001
From: David Seus <david.seus@ians.uni-stuttgart.de>
Date: Fri, 16 Aug 2019 15:09:41 +0200
Subject: [PATCH] fix weird git fuckug

---
 .../TP-TP-2-patch-constant-solution.py        | 285 ++++--------------
 1 file changed, 61 insertions(+), 224 deletions(-)

diff --git a/TP-TP-2-patch-constant-solution/TP-TP-2-patch-constant-solution.py b/TP-TP-2-patch-constant-solution/TP-TP-2-patch-constant-solution.py
index b60c4a7..0dfa344 100755
--- a/TP-TP-2-patch-constant-solution/TP-TP-2-patch-constant-solution.py
+++ b/TP-TP-2-patch-constant-solution/TP-TP-2-patch-constant-solution.py
@@ -7,11 +7,32 @@ import typing as tp
 import domainPatch as dp
 import LDDsimulation as ldd
 import functools as ft
+import helpers as hlp
 #import ufl as ufl
 
 # init sympy session
 sym.init_printing()
 
+solver_tol = 5E-6
+
+############ GRID #######################ü
+mesh_resolution = 20
+timestep_size = 0.01
+number_of_timesteps = 100
+# decide how many timesteps you want analysed. Analysed means, that we write out
+# subsequent errors of the L-iteration within the timestep.
+number_of_timesteps_to_analyse = 10
+starttime = 0
+
+Lw = 1/timestep_size
+Lnw=Lw
+
+l_param_w = 40
+l_param_nw = 40
+
+include_gravity = True
+
+
 ##### Domain and Interface ####
 # global simulation domain domain
 sub_domain0_vertices = [df.Point(-1.0,-1.0), #
@@ -80,15 +101,6 @@ isRichards = {
     }
 
 
-############ GRID #######################ü
-mesh_resolution = 41
-timestep_size = 0.01
-number_of_timesteps = 100
-# decide how many timesteps you want analysed. Analysed means, that we write out
-# subsequent errors of the L-iteration within the timestep.
-number_of_timesteps_to_analyse = 11
-starttime = 0
-
 viscosity = {#
 # subdom_num : viscosity
     1 : {'wetting' :1,
@@ -116,19 +128,19 @@ porosity = {#
 
 L = {#
 # subdom_num : subdomain L for L-scheme
-    1 : {'wetting' :0.25,
-         'nonwetting': 0.25},#
-    2 : {'wetting' :0.25,
-         'nonwetting': 0.25}
+    1 : {'wetting' :Lw,
+         'nonwetting': Lnw},#
+    2 : {'wetting' :Lw,
+         'nonwetting': Lnw}
 }
 
-l_param = 40
+
 lambda_param = {#
 # subdom_num : lambda parameter for the L-scheme
-    1 : {'wetting' :l_param,
-         'nonwetting': l_param},#
-    2 : {'wetting' :l_param,
-         'nonwetting': l_param}
+    1 : {'wetting' :l_param_w,
+         'nonwetting': l_param_nw},#
+    2 : {'wetting' :l_param_w,
+         'nonwetting': l_param_nw}
 }
 
 ## relative permeabilty functions on subdomain 1
@@ -177,7 +189,7 @@ def rel_perm1w_prime(s):
 
 def rel_perm1nw_prime(s):
     # relative permeabilty on subdomain1
-    return 2*(1-s)
+    return -2*(1-s)
 
 # # definition of the derivatives of the relative permeabilities
 # # relative permeabilty functions on subdomain 1
@@ -187,7 +199,7 @@ def rel_perm1nw_prime(s):
 #
 # def rel_perm2nw_prime(s):
 #     # relative permeabilty on subdomain1
-#     return 2*(l_param_w1-s)
+#     return -2*(l_param_w1-s)
 
 _rel_perm1w_prime = ft.partial(rel_perm1w_prime)
 _rel_perm1nw_prime = ft.partial(rel_perm1nw_prime)
@@ -364,211 +376,36 @@ p_e_sym = {
     # 5: {'wetting': 1.0 - (1.0 + t*t)*(1.0 + x*x + y*y)}
 }
 
-# pc_e_sym = {
-#     1: -1*p_e_sym[1]['wetting'],
-#     2: -1*p_e_sym[2]['wetting'],
-#     # 3: -1*p_e_sym[3]['wetting'],
-#     # 4: -1*p_e_sym[4]['wetting'],
-#     # 5: -1*p_e_sym[5]['wetting']
-# }
-
-pc_e_sym = {
-    1: p_e_sym[1]['nonwetting'] - p_e_sym[1]['wetting'],
-    2: p_e_sym[2]['nonwetting'] - p_e_sym[2]['wetting'],
-    # 3: -1*p_e_sym[3]['wetting'],
-    # 4: -1*p_e_sym[4]['wetting'],
-    # 5: -1*p_e_sym[5]['wetting']
-}
-
-
-# #### Manufacture source expressions with sympy
-# ###############################################################################
-# ## subdomain1
-# x, y = sym.symbols('x[0], x[1]') # needed by UFL
-# t = sym.symbols('t', positive=True)
-# #f = -sym.diff(u, x, 2) - sym.diff(u, y, 2)  # -Laplace(u)
-# #f = sym.simplify(f)                         # simplify f
-# p1_w = 1 - (1+t**2)*(1 + x**2 + (y-0.5)**2)
-# p1_nw = t*(1-(y-0.5) - x**2)**2 - sym.sqrt(2+t**2)*(1-(y-0.5))
-#
-# #dtS1_w = sym.diff(S_pc_rel_sym[1](p1_nw - p1_w), t, 1)
-# #dtS1_nw = -sym.diff(S_pc_rel_sym[1](p1_nw - p1_w), t, 1)
-# dtS1_w = porosity[1]*sym.diff(sym.Piecewise((S_pc_rel[1](p1_nw - p1_w), (p1_nw - p1_w) > 0), (1, True) ), t, 1)
-# dtS1_nw = -porosity[1]*sym.diff(sym.Piecewise((S_pc_rel[1](p1_nw - p1_w), (p1_nw - p1_w) > 0), (1, True) ), t, 1)
-# print("dtS1_w = ", dtS1_w, "\n")
-# print("dtS1_nw = ", dtS1_nw, "\n")
-#
-# #dxdxflux1_w = -sym.diff(relative_permeability[1]['wetting'](S_pc_rel_sym[1](p1_nw - p1_w))*sym.diff(p1_w, x, 1), x, 1)
-# #dydyflux1_w = -sym.diff(relative_permeability[1]['wetting'](S_pc_rel_sym[1](p1_nw - p1_w))*sym.diff(p1_w, y, 1), y, 1)
-# dxdxflux1_w = -1/viscosity[1]['wetting']*sym.diff(relative_permeability[1]['wetting'](sym.Piecewise((S_pc_rel[1](p1_nw - p1_w), (p1_nw - p1_w) > 0), (1, True) ))*sym.diff(p1_w, x, 1), x, 1)
-# dydyflux1_w = -1/viscosity[1]['wetting']*sym.diff(relative_permeability[1]['wetting'](sym.Piecewise((S_pc_rel[1](p1_nw - p1_w), (p1_nw - p1_w) > 0), (1, True) ))*sym.diff(p1_w, y, 1), y, 1)
-#
-# rhs1_w = dtS1_w + dxdxflux1_w + dydyflux1_w
-# rhs1_w = sym.printing.ccode(rhs1_w)
-# print("rhs_w = ", rhs1_w, "\n")
-# #rhs_w = sym.expand(rhs_w)
-# #print("rhs_w", rhs_w, "\n")
-# #rhs_w = sym.collect(rhs_w, x)
-# #print("rhs_w", rhs_w, "\n")
-#
-# #dxdxflux1_nw = -sym.diff(relative_permeability[1]['nonwetting'](S_pc_rel_sym[1](p1_nw - p1_w))*sym.diff(p1_nw, x, 1), x, 1)
-# #dydyflux1_nw = -sym.diff(relative_permeability[1]['nonwetting'](S_pc_rel_sym[1](p1_nw - p1_w))*sym.diff(p1_nw, y, 1), y, 1)
-# dxdxflux1_nw = -1/viscosity[1]['nonwetting']*sym.diff(relative_permeability[1]['nonwetting'](1-sym.Piecewise((S_pc_rel[1](p1_nw - p1_w), (p1_nw - p1_w) > 0), (1, True) ))*sym.diff(p1_nw, x, 1), x, 1)
-# dydyflux1_nw = -1/viscosity[1]['nonwetting']*sym.diff(relative_permeability[1]['nonwetting'](1-sym.Piecewise((S_pc_rel[1](p1_nw - p1_w), (p1_nw - p1_w) > 0), (1, True) ))*sym.diff(p1_nw, y, 1), y, 1)
-#
-# rhs1_nw = dtS1_nw + dxdxflux1_nw + dydyflux1_nw
-# rhs1_nw = sym.printing.ccode(rhs1_nw)
-# print("rhs_nw = ", rhs1_nw, "\n")
-#
-# ## subdomain2
-# p2_w = 1 - (1+t**2)*(1 + x**2)
-# p2_nw = t*(1- x**2)**2 - sym.sqrt(2+t**2)*(1-(y-0.5))
-#
-# #dtS2_w = sym.diff(S_pc_rel_sym[2](p2_nw - p2_w), t, 1)
-# #dtS2_nw = -sym.diff(S_pc_rel_sym[2](p2_nw - p2_w), t, 1)
-# dtS2_w = porosity[2]*sym.diff(sym.Piecewise((sym.Piecewise((S_pc_rel[2](p2_nw - p2_w), (p2_nw - p2_w) > 0), (1, True) ), (p2_nw - p2_w) > 0), (1, True) ), t, 1)
-# dtS2_nw = -porosity[2]*sym.diff(sym.Piecewise((S_pc_rel[2](p2_nw - p2_w), (p2_nw - p2_w) > 0), (1, True) ), t, 1)
-# print("dtS2_w = ", dtS2_w, "\n")
-# print("dtS2_nw = ", dtS2_nw, "\n")
-#
-# #dxdxflux2_w = -sym.diff(relative_permeability[2]['wetting'](S_pc_rel_sym[2](p2_nw - p2_w))*sym.diff(p2_w, x, 1), x, 1)
-# #dydyflux2_w = -sym.diff(relative_permeability[2]['wetting'](S_pc_rel_sym[2](p2_nw - p2_w))*sym.diff(p2_w, y, 1), y, 1)
-# dxdxflux2_w = -1/viscosity[2]['wetting']*sym.diff(relative_permeability[2]['wetting'](sym.Piecewise((S_pc_rel[2](p2_nw - p2_w), (p2_nw - p2_w) > 0), (1, True) ))*sym.diff(p2_w, x, 1), x, 1)
-# dydyflux2_w = -1/viscosity[2]['wetting']*sym.diff(relative_permeability[2]['wetting'](sym.Piecewise((S_pc_rel[2](p2_nw - p2_w), (p2_nw - p2_w) > 0), (1, True) ))*sym.diff(p2_w, y, 1), y, 1)
-#
-# rhs2_w = dtS2_w + dxdxflux2_w + dydyflux2_w
-# rhs2_w = sym.printing.ccode(rhs2_w)
-# print("rhs2_w = ", rhs2_w, "\n")
-# #rhs_w = sym.expand(rhs_w)
-# #print("rhs_w", rhs_w, "\n")
-# #rhs_w = sym.collect(rhs_w, x)
-# #print("rhs_w", rhs_w, "\n")
-#
-# #dxdxflux2_nw = -sym.diff(relative_permeability[2]['nonwetting'](S_pc_rel_sym[2](p2_nw - p2_w))*sym.diff(p2_nw, x, 1), x, 1)
-# #dydyflux2_nw = -sym.diff(relative_permeability[2]['nonwetting'](S_pc_rel_sym[2](p2_nw - p2_w))*sym.diff(p2_nw, y, 1), y, 1)
-# dxdxflux2_nw = -1/viscosity[2]['nonwetting']*sym.diff(relative_permeability[2]['nonwetting'](1-sym.Piecewise((S_pc_rel[2](p2_nw - p2_w), (p2_nw - p2_w) > 0), (1, True) ))*sym.diff(p2_nw, x, 1), x, 1)
-# dydyflux2_nw = -1/viscosity[2]['nonwetting']*sym.diff(relative_permeability[2]['nonwetting'](1-sym.Piecewise((S_pc_rel[2](p2_nw - p2_w), (p2_nw - p2_w) > 0), (1, True) ))*sym.diff(p2_nw, y, 1), y, 1)
-#
-# rhs2_nw = dtS2_nw + dxdxflux2_nw + dydyflux2_nw
-# rhs2_nw = sym.printing.ccode(rhs2_nw)
-# print("rhs2_nw = ", rhs2_nw, "\n")
-#
-#
-# ###############################################################################
-#
-# source_expression = {
-#     1: {'wetting': rhs1_w,
-#         'nonwetting': rhs1_nw},
-#     2: {'wetting': rhs2_w,
-#         'nonwetting': rhs2_nw}
-# }
-#
-# p1_w_00 = p1_w.subs(t, 0)
-# p1_nw_00 = p1_nw.subs(t, 0)
-# p2_w_00 = p2_w.subs(t, 0)
-# p2_nw_00 = p2_nw.subs(t, 0)
-# # p1_w_00 = sym.printing.ccode(p1_w_00)
-#
-# initial_condition = {
-#     1: {'wetting': sym.printing.ccode(p1_w_00),
-#         'nonwetting': sym.printing.ccode(p1_nw_00)},#
-#     2: {'wetting': sym.printing.ccode(p2_w_00),
-#         'nonwetting': sym.printing.ccode(p2_nw_00)}
-# }
-#
-# exact_solution = {
-#     1: {'wetting': sym.printing.ccode(p1_w),
-#         'nonwetting': sym.printing.ccode(p1_nw)},#
-#     2: {'wetting': sym.printing.ccode(p2_w),
-#         'nonwetting': sym.printing.ccode(p2_nw)}
-# }
-#
-# # similary to the outer boundary dictionary, if a patch has no outer boundary
-# # None should be written instead of an expression. This is a bit of a brainfuck:
-# # dirichletBC[ind] gives a dictionary of the outer boundaries of subdomain ind.
-# # Since a domain patch can have several disjoint outer boundary parts, the expressions
-# # need to get an enumaration index which starts at 0. So dirichletBC[ind][j] is
-# # the dictionary of outer dirichlet conditions of subdomain ind and boundary part j.
-# # finally, dirichletBC[ind][j]['wetting'] and dirichletBC[ind][j]['nonwetting'] return
-# # the actual expression needed for the dirichlet condition for both phases if present.
-# dirichletBC = {
-# #subdomain index: {outer boudary part index: {phase: expression}}
-#     1: { 0: {'wetting': sym.printing.ccode(p1_w),
-#              'nonwetting': sym.printing.ccode(p1_nw)}},
-#     2: { 0: {'wetting': sym.printing.ccode(p2_w),
-#              'nonwetting': sym.printing.ccode(p2_nw)}}
-# }
-
-# turn above symbolic code into exact solution for dolphin and
-# construct the rhs that matches the above exact solution.
-dtS = dict()
-div_flux = dict()
-source_expression = dict()
-exact_solution = dict()
-initial_condition = dict()
+pc_e_sym = dict()
 for subdomain, isR in isRichards.items():
-    dtS.update({subdomain: dict()})
-    div_flux.update({subdomain: dict()})
-    source_expression.update({subdomain: dict()})
-    exact_solution.update({subdomain: dict()})
-    initial_condition.update({subdomain: dict()})
     if isR:
-        subdomain_has_phases = ["wetting"]
+        pc_e_sym.update({subdomain: -p_e_sym[subdomain]['wetting']})
     else:
-        subdomain_has_phases = ["wetting", "nonwetting"]
-
-    # conditional for S_pc_prime
-    pc = pc_e_sym[subdomain]
-    dtpc = sym.diff(pc, t, 1)
-    dxpc = sym.diff(pc, x, 1)
-    dypc = sym.diff(pc, y, 1)
-    S = sym.Piecewise((S_pc_sym[subdomain](pc), pc > 0), (1, True))
-    dS = sym.Piecewise((S_pc_sym_prime[subdomain](pc), pc > 0), (0, True))
-    for phase in subdomain_has_phases:
-        # Turn above symbolic expression for exact solution into c code
-        exact_solution[subdomain].update(
-            {phase: sym.printing.ccode(p_e_sym[subdomain][phase])}
-            )
-        # save the c code for initial conditions
-        initial_condition[subdomain].update(
-            {phase: sym.printing.ccode(p_e_sym[subdomain][phase].subs(t, 0))}
-            )
-        if phase == "nonwetting":
-            dtS[subdomain].update(
-                {phase: -porosity[subdomain]*dS*dtpc}
-                )
-        else:
-            dtS[subdomain].update(
-                {phase: porosity[subdomain]*dS*dtpc}
-                )
-        pa = p_e_sym[subdomain][phase]
-        dxpa = sym.diff(pa, x, 1)
-        dxdxpa = sym.diff(pa, x, 2)
-        dypa = sym.diff(pa, y, 1)
-        dydypa = sym.diff(pa, y, 2)
-        mu = viscosity[subdomain][phase]
-        ka = relative_permeability[subdomain][phase]
-        dka = ka_prime[subdomain][phase]
-        rho = densities[subdomain][phase]
-        g = gravity_acceleration
-
-        if phase == "nonwetting":
-            # x part of div(flux) for nonwetting
-            dxdxflux = -1/mu*dka(1-S)*dS*dxpc*dxpa + 1/mu*dxdxpa*ka(1-S)
-            # y part of div(flux) for nonwetting
-            dydyflux = -1/mu*dka(1-S)*dS*dypc*(dypa - rho*g) \
-                + 1/mu*dydypa*ka(1-S)
-        else:
-            # x part of div(flux) for wetting
-            dxdxflux = 1/mu*dka(S)*dS*dxpc*dxpa + 1/mu*dxdxpa*ka(S)
-            # y part of div(flux) for wetting
-            dydyflux = 1/mu*dka(S)*dS*dypc*(dypa - rho*g) + 1/mu*dydypa*ka(S)
-        div_flux[subdomain].update({phase: dxdxflux + dydyflux})
-        contructed_rhs = dtS[subdomain][phase] - div_flux[subdomain][phase]
-        source_expression[subdomain].update(
-            {phase: sym.printing.ccode(contructed_rhs)}
-            )
-        # print(f"source_expression[{subdomain}][{phase}] =", source_expression[subdomain][phase])
+        pc_e_sym.update({subdomain: p_e_sym[subdomain]['nonwetting'] - p_e_sym[subdomain]['wetting']})
+
+symbols = {"x": x,
+           "y": y,
+           "t": t}
+# turn above symbolic code into exact solution for dolphin and
+# construct the rhs that matches the above exact solution.
+exact_solution_example = hlp.generate_exact_solution_expressions(
+                        symbols=symbols,
+                        isRichards=isRichards,
+                        symbolic_pressure=p_e_sym,
+                        symbolic_capillary_pressure=pc_e_sym,
+                        saturation_pressure_relationship=S_pc_sym,
+                        saturation_pressure_relationship_prime=S_pc_sym_prime,
+                        viscosity=viscosity,
+                        porosity=porosity,
+                        relative_permeability=relative_permeability,
+                        relative_permeability_prime=ka_prime,
+                        densities=densities,
+                        gravity_acceleration=gravity_acceleration,
+                        include_gravity=include_gravity,
+                        )
+source_expression = exact_solution_example['source']
+exact_solution = exact_solution_example['exact_solution']
+initial_condition = exact_solution_example['initial_condition']
 
 # Dictionary of dirichlet boundary conditions.
 dirichletBC = dict()
@@ -612,7 +449,7 @@ write_to_file = {
 
 
 # initialise LDD simulation class
-simulation = ldd.LDDsimulation(tol = 1E-14, LDDsolver_tol = 1E-6, debug = False)
+simulation = ldd.LDDsimulation(tol = 1E-14, LDDsolver_tol = solver_tol, debug = True)
 simulation.set_parameters(output_dir = "./output/",#
     subdomain_def_points = subdomain_def_points,#
     isRichards = isRichards,#
@@ -635,7 +472,7 @@ simulation.set_parameters(output_dir = "./output/",#
     dirichletBC_expression_strings = dirichletBC,#
     exact_solution = exact_solution,#
     densities=densities,
-    include_gravity=True,
+    include_gravity=include_gravity,
     write2file = write_to_file,#
     )
 
-- 
GitLab