diff --git a/TP-R-two-patch-test-case-constant-solution/TP-R-2-patch-test-constant-solution.py b/TP-R-two-patch-test-case-constant-solution/TP-R-2-patch-test-constant-solution.py new file mode 100755 index 0000000000000000000000000000000000000000..aab3e1754a7343eba060aea676e2e9c0e0272f6e --- /dev/null +++ b/TP-R-two-patch-test-case-constant-solution/TP-R-2-patch-test-constant-solution.py @@ -0,0 +1,431 @@ +#!/usr/bin/python3 +import dolfin as df +import mshr +import numpy as np +import sympy as sym +import typing as tp +import domainPatch as dp +import LDDsimulation as ldd +import functools as ft +#import ufl as ufl + +# init sympy session +sym.init_printing() + +##### Domain and Interface #### +# global simulation domain domain +sub_domain0_vertices = [df.Point(-1.0, -1.0), + df.Point(1.0, -1.0), + df.Point(1.0, 1.0), + df.Point(-1.0, 1.0)] +# interface between subdomain1 and subdomain2 +interface12_vertices = [df.Point(-1.0, 0.0), + df.Point(1.0, 0.0) ] +# subdomain1. +sub_domain1_vertices = [interface12_vertices[0], + interface12_vertices[1], + sub_domain0_vertices[2], + sub_domain0_vertices[3]] + +# vertex coordinates of the outer boundaries. If it can not be specified as a +# polygon, use an entry per boundary polygon. This information is used for defining +# the Dirichlet boundary conditions. If a domain is completely internal, the +# dictionary entry should be 0: None +subdomain1_outer_boundary_verts = { + 0: [interface12_vertices[1], # + sub_domain0_vertices[2], + sub_domain0_vertices[3], # + interface12_vertices[0]] +} +# subdomain2 +sub_domain2_vertices = [sub_domain0_vertices[0], + sub_domain0_vertices[1], + interface12_vertices[1], + interface12_vertices[0] ] + +subdomain2_outer_boundary_verts = { + 0: [interface12_vertices[0], # + sub_domain0_vertices[0], + sub_domain0_vertices[1], + interface12_vertices[1]] +} +# subdomain2_outer_boundary_verts = { +# 0: [interface12_vertices[0], df.Point(0.0,0.0)],# +# 1: [df.Point(0.0,0.0), df.Point(1.0,0.0)], # +# 2: [df.Point(1.0,0.0), interface12_vertices[1]] +# } +# subdomain2_outer_boundary_verts = { +# 0: None +# } + +# list of subdomains given by the boundary polygon vertices. +# Subdomains are given as a list of dolfin points forming +# a closed polygon, such that mshr.Polygon(subdomain_def_points[i]) can be used +# to create the subdomain. subdomain_def_points[0] contains the +# vertices of the global simulation domain and subdomain_def_points[i] contains the +# vertices of the subdomain i. +subdomain_def_points = [sub_domain0_vertices,# + sub_domain1_vertices,# + sub_domain2_vertices] +# in the below list, index 0 corresponds to the 12 interface which has index 1 +interface_def_points = [interface12_vertices] + +# if a subdomain has no outer boundary write None instead, i.e. +# i: None +# if i is the index of the inner subdomain. +outer_boundary_def_points = { + # subdomain number + 1 : subdomain1_outer_boundary_verts, + 2 : subdomain2_outer_boundary_verts +} + +# adjacent_subdomains[i] contains the indices of the subdomains sharing the +# interface i (i.e. given by interface_def_points[i]). +adjacent_subdomains = [[1,2]] +isRichards = { + 1: True, # + 2: False + } + + +############ GRID #######################ü +mesh_resolution = 20 +timestep_size = 0.001 +number_of_timesteps = 50 +# 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}, + #'nonwetting': 1}, # + 2 : {'wetting' :1, + 'nonwetting': 1} +} + +porosity = {# +# subdom_num : porosity + 1 : 1,#0.22,# + 2 : 1#0.022 +} + +# Dict of the form: { subdom_num : density } +densities = { + 1: {'wetting': 1}, + 2: {'wetting': 1, + 'nonwetting': 1}, +} + +gravity_acceleration = 9.81 + +L = {# +# subdom_num : subdomain L for L-scheme + 1 : {'wetting' :0.25}, + # 'nonwetting': 0.25},# + 2 : {'wetting' :0.25, + 'nonwetting': 0.25} +} + +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} +} + +## relative permeabilty functions on subdomain 1 +def rel_perm1w(s): + # relative permeabilty wetting on subdomain1 + return s**2 + +# def rel_perm1nw(s): +# # relative permeabilty nonwetting on subdomain1 +# return (1-s)**2 + +_rel_perm1w = ft.partial(rel_perm1w) +# _rel_perm1nw = ft.partial(rel_perm1nw) +subdomain1_rel_perm = { + 'wetting': _rel_perm1w,# + # 'nonwetting': _rel_perm1nw +} +## relative permeabilty functions on subdomain 2 +def rel_perm2w(s): + # relative permeabilty wetting on subdomain2 + return s**2 +def rel_perm2nw(s): + # relative permeabilty nonwetting on subdosym.cos(0.8*t - (0.8*x + 1/7*y))main2 + return (1-s)**2 + +_rel_perm2w = ft.partial(rel_perm2w) +_rel_perm2nw = ft.partial(rel_perm2nw) + +subdomain2_rel_perm = { + 'wetting': _rel_perm2w,# + 'nonwetting': _rel_perm2nw +} + +## dictionary of relative permeabilties on all domains. +relative_permeability = {# + 1: subdomain1_rel_perm, + 2: subdomain2_rel_perm +} + + +# definition of the derivatives of the relative permeabilities +# relative permeabilty functions on subdomain 1 +def rel_perm1w_prime(s): + # relative permeabilty on subdomain1 + return 2*s + +# def rel_perm1nw_prime(s): +# # relative permeabilty on subdomain1 +# return 2*(1-s) + +# definition of the derivatives of the relative permeabilities +# relative permeabilty functions on subdomain 1 +def rel_perm2w_prime(s): + # relative permeabilty on subdomain1 + return 2*s + +def rel_perm2nw_prime(s): + # relative permeabilty on subdomain1 + return 2*(1-s) + +_rel_perm1w_prime = ft.partial(rel_perm1w_prime) +# _rel_perm1nw_prime = ft.partial(rel_perm1nw_prime) +_rel_perm2w_prime = ft.partial(rel_perm2w_prime) +_rel_perm2nw_prime = ft.partial(rel_perm2nw_prime) + +subdomain1_rel_perm_prime = { + 'wetting': _rel_perm1w_prime + # 'nonwetting': _rel_perm1nw_prime +} + + +subdomain2_rel_perm_prime = { + 'wetting': _rel_perm2w_prime, + 'nonwetting': _rel_perm2nw_prime +} + +# dictionary of relative permeabilties on all domains. +ka_prime = { + 1: subdomain1_rel_perm_prime, + 2: subdomain2_rel_perm_prime, +} + + +def saturation1(pc, subdomain_index): + # inverse capillary pressure-saturation-relationship + return df.conditional(pc > 0, 1/((1 + pc)**(1/(subdomain_index + 1))), 1) + +def saturation2(pc, n_index, alpha): + # inverse capillary pressure-saturation-relationship + return df.conditional(pc > 0, 1/((1 + (alpha*pc)**n_index)**((n_index - 1)/n_index)), 1) + +# S-pc-relation ship. We use the van Genuchten approach, i.e. pc = 1/alpha*(S^{-1/m} -1)^1/n, where +# we set alpha = 0, assume m = 1-1/n (see Helmig) and assume that residual saturation is Sw +def saturation1_sym(pc, subdomain_index): + # inverse capillary pressure-saturation-relationship + return 1/((1 + pc)**(1/(subdomain_index + 1))) + + +def saturation2_sym(pc, n_index, alpha): + # inverse capillary pressure-saturation-relationship + #df.conditional(pc > 0, + return 1/((1 + (alpha*pc)**n_index)**((n_index - 1)/n_index)) + + +# derivative of S-pc relationship with respect to pc. This is needed for the +# construction of a analytic solution. +def saturation1_sym_prime(pc, subdomain_index): + # inverse capillary pressure-saturation-relationship + return -(1/(subdomain_index + 1))*(1 + pc)**((-subdomain_index - 2)/(subdomain_index + 1)) + + +def saturation2_sym_prime(pc, n_index, alpha): + # inverse capillary pressure-saturation-relationship + return -(alpha*(n_index - 1)*(alpha*pc)**(n_index - 1)) / ( (1 + (alpha*pc)**n_index)**((2*n_index - 1)/n_index) ) + +# note that the conditional definition of S-pc in the nonsymbolic part will be +# incorporated in the construction of the exact solution below. +S_pc_sym = { + 1: ft.partial(saturation1_sym, subdomain_index = 1), + 2: ft.partial(saturation1_sym, subdomain_index = 1), #ft.partial(saturation2_sym, n_index=3, alpha=0.001), +} + +S_pc_sym_prime = { + 1: ft.partial(saturation1_sym_prime, subdomain_index = 1), + 2: ft.partial(saturation1_sym_prime, subdomain_index = 1), #ft.partial(saturation2_sym_prime, n_index=3, alpha=0.001), +} + +sat_pressure_relationship = { + 1: ft.partial(saturation1, subdomain_index = 1),#, + 2: ft.partial(saturation1, subdomain_index = 1),#, ft.partial(saturation2, n_index=3, alpha=0.001), +} + + +############################################# +# Manufacture source expressions with sympy # +############################################# +x, y = sym.symbols('x[0], x[1]') # needed by UFL +t = sym.symbols('t', positive=True) + +p_e_sym = { + 1: {'wetting': -3.0 + 0*t}, + 2: {'wetting': -3.0 + 0*t, + 'nonwetting': 0*t}, +} #-(y-0.5)*(y-0.5)*(sym.sin(-2*t+2*x)*sym.sin(1/2*y-1.2*t)) - t*t*x*(0.5-y)*y*(1-x) + + +pc_e_sym = { + 1: -1*p_e_sym[1]['wetting'], + 2: p_e_sym[2]['nonwetting'] - p_e_sym[2]['wetting'] +} + +# 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() +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"] + 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]) + +# Dictionary of dirichlet boundary conditions. +dirichletBC = dict() +# similarly 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. + +# subdomain index: {outer boudary part index: {phase: expression}} +for subdomain in isRichards.keys(): + # if subdomain has no outer boundary, outer_boundary_def_points[subdomain] is None + if outer_boundary_def_points[subdomain] is None: + dirichletBC.update({subdomain: None}) + else: + dirichletBC.update({subdomain: dict()}) + # set the dirichlet conditions to be the same code as exact solution on + # the subdomain. + for outer_boundary_ind in outer_boundary_def_points[subdomain].keys(): + dirichletBC[subdomain].update( + {outer_boundary_ind: exact_solution[subdomain]} + ) + + +# def saturation(pressure, subdomain_index): +# # inverse capillary pressure-saturation-relationship +# return df.conditional(pressure < 0, 1/((1 - pressure)**(1/(subdomain_index + 1))), 1) +# +# sa + +write_to_file = { + 'meshes_and_markers': True, + 'L_iterations': True +} + + +# initialise LDD simulation class +simulation = ldd.LDDsimulation(tol = 1E-14, LDDsolver_tol = 5E-4, debug = True) +simulation.set_parameters(output_dir = "./output/",# + subdomain_def_points = subdomain_def_points,# + isRichards = isRichards,# + interface_def_points = interface_def_points,# + outer_boundary_def_points = outer_boundary_def_points,# + adjacent_subdomains = adjacent_subdomains,# + mesh_resolution = mesh_resolution,# + viscosity = viscosity,# + porosity = porosity,# + L = L,# + lambda_param = lambda_param,# + relative_permeability = relative_permeability,# + saturation = sat_pressure_relationship,# + starttime = starttime,# + number_of_timesteps = number_of_timesteps, + number_of_timesteps_to_analyse = number_of_timesteps_to_analyse, + timestep_size = timestep_size,# + sources = source_expression,# + initial_conditions = initial_condition,# + dirichletBC_expression_strings = dirichletBC,# + exact_solution = exact_solution,# + densities=densities, + include_gravity=True, + write2file = write_to_file,# + ) + +simulation.initialise() +# simulation.write_exact_solution_to_xdmf() +simulation.run() diff --git a/TP-R-two-patch-test-case/TP-R-2-patch-test.py b/TP-R-two-patch-test-case/TP-R-2-patch-test.py new file mode 100755 index 0000000000000000000000000000000000000000..459f2ddfe7e66dc2b5610d9491aab66ce8bc8ea1 --- /dev/null +++ b/TP-R-two-patch-test-case/TP-R-2-patch-test.py @@ -0,0 +1,463 @@ +#!/usr/bin/python3 +import dolfin as df +import mshr +import numpy as np +import sympy as sym +import typing as tp +import domainPatch as dp +import LDDsimulation as ldd +import functools as ft +#import ufl as ufl + +# init sympy session +sym.init_printing() + +##### Domain and Interface #### +# global simulation domain domain +sub_domain0_vertices = [df.Point(-1.0, -1.0), + df.Point(1.0, -1.0), + df.Point(1.0, 1.0), + df.Point(-1.0, 1.0)] +# interface between subdomain1 and subdomain2 +interface12_vertices = [df.Point(-1.0, 0.0), + df.Point(1.0, 0.0) ] +# subdomain1. +sub_domain1_vertices = [interface12_vertices[0], + interface12_vertices[1], + sub_domain0_vertices[2], + sub_domain0_vertices[3]] + +# vertex coordinates of the outer boundaries. If it can not be specified as a +# polygon, use an entry per boundary polygon. This information is used for defining +# the Dirichlet boundary conditions. If a domain is completely internal, the +# dictionary entry should be 0: None +subdomain1_outer_boundary_verts = { + 0: [interface12_vertices[1], # + sub_domain0_vertices[2], + sub_domain0_vertices[3], # + interface12_vertices[0]] +} +# subdomain2 +sub_domain2_vertices = [sub_domain0_vertices[0], + sub_domain0_vertices[1], + interface12_vertices[1], + interface12_vertices[0] ] + +subdomain2_outer_boundary_verts = { + 0: [interface12_vertices[0], # + sub_domain0_vertices[0], + sub_domain0_vertices[1], + interface12_vertices[1]] +} + +# list of subdomains given by the boundary polygon vertices. +# Subdomains are given as a list of dolfin points forming +# a closed polygon, such that mshr.Polygon(subdomain_def_points[i]) can be used +# to create the subdomain. subdomain_def_points[0] contains the +# vertices of the global simulation domain and subdomain_def_points[i] contains the +# vertices of the subdomain i. +subdomain_def_points = [sub_domain0_vertices,# + sub_domain1_vertices,# + sub_domain2_vertices] +# in the below list, index 0 corresponds to the 12 interface which has index 1 +interface_def_points = [interface12_vertices] + +# if a subdomain has no outer boundary write None instead, i.e. +# i: None +# if i is the index of the inner subdomain. +outer_boundary_def_points = { + # subdomain number + 1 : subdomain1_outer_boundary_verts, + 2 : subdomain2_outer_boundary_verts +} + +# adjacent_subdomains[i] contains the indices of the subdomains sharing the +# interface i (i.e. given by interface_def_points[i]). +adjacent_subdomains = [[1,2]] +isRichards = { + 1: True, # + 2: False + } + + +############ GRID #######################ü +mesh_resolution = 50 +timestep_size = 0.01 +number_of_timesteps = 160 +# 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}, + #'nonwetting': 1}, # + 2 : {'wetting' :1, + 'nonwetting': 1} +} + +porosity = {# +# subdom_num : porosity + 1 : 1,#0.22,# + 2 : 1#0.022 +} + +# Dict of the form: { subdom_num : density } +densities = { + 1: {'wetting': 1}, + 2: {'wetting': 1, + 'nonwetting': 1}, +} + +gravity_acceleration = 9.81 + +L = {# +# subdom_num : subdomain L for L-scheme + 1 : {'wetting' :0.25}, + # 'nonwetting': 0.25},# + 2 : {'wetting' :0.25, + 'nonwetting': 0.25} +} + +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} +} + +## relative permeabilty functions on subdomain 1 +def rel_perm1w(s): + # relative permeabilty wetting on subdomain1 + return s**2 + +# def rel_perm1nw(s): +# # relative permeabilty nonwetting on subdomain1 +# return (1-s)**2 + +_rel_perm1w = ft.partial(rel_perm1w) +# _rel_perm1nw = ft.partial(rel_perm1nw) +subdomain1_rel_perm = { + 'wetting': _rel_perm1w,# + # 'nonwetting': _rel_perm1nw +} +## relative permeabilty functions on subdomain 2 +def rel_perm2w(s): + # relative permeabilty wetting on subdomain2 + return s**3 +def rel_perm2nw(s): + # relative permeabilty nonwetting on subdosym.cos(0.8*t - (0.8*x + 1/7*y))main2 + return (1-s)**3 + +_rel_perm2w = ft.partial(rel_perm2w) +_rel_perm2nw = ft.partial(rel_perm2nw) + +subdomain2_rel_perm = { + 'wetting': _rel_perm2w,# + 'nonwetting': _rel_perm2nw +} + +## dictionary of relative permeabilties on all domains. +relative_permeability = {# + 1: subdomain1_rel_perm, + 2: subdomain2_rel_perm +} + + +# definition of the derivatives of the relative permeabilities +# relative permeabilty functions on subdomain 1 +def rel_perm1w_prime(s): + # relative permeabilty on subdomain1 + return 2*s + +# def rel_perm1nw_prime(s): +# # relative permeabilty on subdomain1 +# return 2*(1-s) + +# definition of the derivatives of the relative permeabilities +# relative permeabilty functions on subdomain 1 +def rel_perm2w_prime(s): + # relative permeabilty on subdomain1 + return 3*s**2 + +def rel_perm2nw_prime(s): + # relative permeabilty on subdomain1 + return 3*(1-s)**2 + +_rel_perm1w_prime = ft.partial(rel_perm1w_prime) +# _rel_perm1nw_prime = ft.partial(rel_perm1nw_prime) +_rel_perm2w_prime = ft.partial(rel_perm2w_prime) +_rel_perm2nw_prime = ft.partial(rel_perm2nw_prime) + +subdomain1_rel_perm_prime = { + 'wetting': _rel_perm1w_prime + # 'nonwetting': _rel_perm1nw_prime +} + + +subdomain2_rel_perm_prime = { + 'wetting': _rel_perm2w_prime, + 'nonwetting': _rel_perm2nw_prime +} + +# dictionary of relative permeabilties on all domains. +ka_prime = { + 1: subdomain1_rel_perm_prime, + 2: subdomain2_rel_perm_prime, +} + + +# def saturation1(pc, subdomain_index): +# # inverse capillary pressure-saturation-relationship +# return df.conditional(pc > 0, 1/((1 + pc)**(1/(subdomain_index + 1))), 1) +# +# def saturation2(pc, n_index, alpha): +# # inverse capillary pressure-saturation-relationship +# return df.conditional(pc > 0, 1/((1 + (alpha*pc)**n_index)**((n_index - 1)/n_index)), 1) +# +# # S-pc-relation ship. We use the van Genuchten approach, i.e. pc = 1/alpha*(S^{-1/m} -1)^1/n, where +# # we set alpha = 0, assume m = 1-1/n (see Helmig) and assume that residual saturation is Sw +# def saturation1_sym(pc, subdomain_index): +# # inverse capillary pressure-saturation-relationship +# return 1/((1 + pc)**(1/(subdomain_index + 1))) +# +# +# def saturation2_sym(pc, n_index, alpha): +# # inverse capillary pressure-saturation-relationship +# #df.conditional(pc > 0, +# return 1/((1 + (alpha*pc)**n_index)**((n_index - 1)/n_index)) +# +# +# # derivative of S-pc relationship with respect to pc. This is needed for the +# # construction of a analytic solution. +# def saturation1_sym_prime(pc, subdomain_index): +# # inverse capillary pressure-saturation-relationship +# return -(1/(subdomain_index + 1))*(1 + pc)**((-subdomain_index - 2)/(subdomain_index + 1)) +# +# +# def saturation2_sym_prime(pc, n_index, alpha): +# # inverse capillary pressure-saturation-relationship +# return -(alpha*(n_index - 1)*(alpha*pc)**(n_index - 1)) / ( (1 + (alpha*pc)**n_index)**((2*n_index - 1)/n_index) ) +# +# # note that the conditional definition of S-pc in the nonsymbolic part will be +# # incorporated in the construction of the exact solution below. +# S_pc_sym = { +# 1: ft.partial(saturation1_sym, subdomain_index = 1), +# 2: ft.partial(saturation2_sym, n_index=3, alpha=0.001), +# } +# +# S_pc_sym_prime = { +# 1: ft.partial(saturation1_sym_prime, subdomain_index = 1), +# 2: ft.partial(saturation2_sym_prime, n_index=3, alpha=0.001), +# } +# +# sat_pressure_relationship = { +# 1: ft.partial(saturation1, subdomain_index = 1),#, +# 2: ft.partial(saturation2, n_index=3, alpha=0.001), +# } + +def saturation(pc, index): + # inverse capillary pressure-saturation-relationship + return df.conditional(pc > 0, 1/((1 + pc)**(1/(index + 1))), 1) + + +def saturation_sym(pc, index): + # inverse capillary pressure-saturation-relationship + return 1/((1 + pc)**(1/(index + 1))) + + +# derivative of S-pc relationship with respect to pc. This is needed for the +# construction of a analytic solution. +def saturation_sym_prime(pc, index): + # inverse capillary pressure-saturation-relationship + return -1/((index+1)*(1 + pc)**((index+2)/(index+1))) + + +# note that the conditional definition of S-pc in the nonsymbolic part will be +# incorporated in the construction of the exact solution below. +S_pc_sym = { + 1: ft.partial(saturation_sym, index=1), + 2: ft.partial(saturation_sym, index=2), + # 3: ft.partial(saturation_sym, index=2), + # 4: ft.partial(saturation_sym, index=1) +} + +S_pc_sym_prime = { + 1: ft.partial(saturation_sym_prime, index=1), + 2: ft.partial(saturation_sym_prime, index=2), + # 3: ft.partial(saturation_sym_prime, index=2), + # 4: ft.partial(saturation_sym_prime, index=1) +} + +sat_pressure_relationship = { + 1: ft.partial(saturation, index=1), + 2: ft.partial(saturation, index=2), + # 3: ft.partial(saturation, index=2), + # 4: ft.partial(saturation, index=1) +} + + +############################################# +# Manufacture source expressions with sympy # +############################################# +x, y = sym.symbols('x[0], x[1]') # needed by UFL +t = sym.symbols('t', positive=True) + +p_e_sym = { + 1: {'wetting': 1.0 - (1.0 + t*t)*(1.0 + x*x + y*y)}, + 2: {'wetting': 1.0 - (1.0 + t*t)*(1.0 + x*x), + 'nonwetting': (-t*(1-y - x**2)**2 - sym.sqrt(2+t**2))*y}, +} #-y*y*(sym.sin(-2*t+2*x)*sym.sin(1/2*y-1.2*t)) - t*t*x*(0.5-y)*y*(1-x) + + +pc_e_sym = { + 1: -1*p_e_sym[1]['wetting'], + 2: p_e_sym[2]['nonwetting'] - p_e_sym[2]['wetting'] +} + +# 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() +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"] + 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]) + +# Dictionary of dirichlet boundary conditions. +dirichletBC = dict() +# similarly 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. + +# subdomain index: {outer boudary part index: {phase: expression}} +for subdomain in isRichards.keys(): + # if subdomain has no outer boundary, outer_boundary_def_points[subdomain] is None + if outer_boundary_def_points[subdomain] is None: + dirichletBC.update({subdomain: None}) + else: + dirichletBC.update({subdomain: dict()}) + # set the dirichlet conditions to be the same code as exact solution on + # the subdomain. + for outer_boundary_ind in outer_boundary_def_points[subdomain].keys(): + dirichletBC[subdomain].update( + {outer_boundary_ind: exact_solution[subdomain]} + ) + + +# def saturation(pressure, subdomain_index): +# # inverse capillary pressure-saturation-relationship +# return df.conditional(pressure < 0, 1/((1 - pressure)**(1/(subdomain_index + 1))), 1) +# +# sa + +write_to_file = { + 'meshes_and_markers': True, + 'L_iterations': True +} + + +# initialise LDD simulation class +simulation = ldd.LDDsimulation(tol = 1E-14, LDDsolver_tol = 1E-7, debug = False) +simulation.set_parameters(output_dir = "./output/",# + subdomain_def_points = subdomain_def_points,# + isRichards = isRichards,# + interface_def_points = interface_def_points,# + outer_boundary_def_points = outer_boundary_def_points,# + adjacent_subdomains = adjacent_subdomains,# + mesh_resolution = mesh_resolution,# + viscosity = viscosity,# + porosity = porosity,# + L = L,# + lambda_param = lambda_param,# + relative_permeability = relative_permeability,# + saturation = sat_pressure_relationship,# + starttime = starttime,# + number_of_timesteps = number_of_timesteps, + number_of_timesteps_to_analyse = number_of_timesteps_to_analyse, + timestep_size = timestep_size,# + sources = source_expression,# + initial_conditions = initial_condition,# + dirichletBC_expression_strings = dirichletBC,# + exact_solution = exact_solution,# + densities=densities, + include_gravity=True, + write2file = write_to_file,# + ) + +simulation.initialise() +# simulation.write_exact_solution_to_xdmf() +simulation.run() diff --git a/TP-TP-2-patch-pure-dd/TP-TP-2-patch-pure-dd.py b/TP-TP-2-patch-pure-dd/TP-TP-2-patch-pure-dd.py new file mode 100755 index 0000000000000000000000000000000000000000..cea1cdac7aded8b6638f50998f322188bbaa99d8 --- /dev/null +++ b/TP-TP-2-patch-pure-dd/TP-TP-2-patch-pure-dd.py @@ -0,0 +1,480 @@ +#!/usr/bin/python3 +import dolfin as df +import mshr +import numpy as np +import sympy as sym +import typing as tp +import domainPatch as dp +import LDDsimulation as ldd +import functools as ft +#import ufl as ufl + +# init sympy session +sym.init_printing() + +##### Domain and Interface #### +# global simulation domain domain +sub_domain0_vertices = [df.Point(-1.0,-1.0), # + df.Point(1.0,-1.0),# + df.Point(1.0,1.0),# + df.Point(-1.0,1.0)] +# interface between subdomain1 and subdomain2 +interface12_vertices = [df.Point(-1.0, 0.0), + df.Point(1.0, 0.0) ] +# subdomain1. +sub_domain1_vertices = [interface12_vertices[0], + interface12_vertices[1], + sub_domain0_vertices[2], + sub_domain0_vertices[3] ] + +# vertex coordinates of the outer boundaries. If it can not be specified as a +# polygon, use an entry per boundary polygon. This information is used for defining +# the Dirichlet boundary conditions. If a domain is completely internal, the +# dictionary entry should be 0: None +subdomain1_outer_boundary_verts = { + 0: [interface12_vertices[1], + sub_domain0_vertices[2], + sub_domain0_vertices[3], # + interface12_vertices[0]] +} +# subdomain2 +sub_domain2_vertices = [sub_domain0_vertices[0], + sub_domain0_vertices[1], + interface12_vertices[1], + interface12_vertices[0] ] + +subdomain2_outer_boundary_verts = { + 0: [interface12_vertices[0], # + sub_domain0_vertices[0], + sub_domain0_vertices[1], + interface12_vertices[1]] +} +# subdomain2_outer_boundary_verts = { +# 0: [interface12_vertices[0], df.Point(0.0,0.0)],# +# 1: [df.Point(0.0,0.0), df.Point(1.0,0.0)], # +# 2: [df.Point(1.0,0.0), interface12_vertices[1]] +# } +# subdomain2_outer_boundary_verts = { +# 0: None +# } + +# list of subdomains given by the boundary polygon vertices. +# Subdomains are given as a list of dolfin points forming +# a closed polygon, such that mshr.Polygon(subdomain_def_points[i]) can be used +# to create the subdomain. subdomain_def_points[0] contains the +# vertices of the global simulation domain and subdomain_def_points[i] contains the +# vertices of the subdomain i. +subdomain_def_points = [sub_domain0_vertices,# + sub_domain1_vertices,# + sub_domain2_vertices] +# in the below list, index 0 corresponds to the 12 interface which has index 1 +interface_def_points = [interface12_vertices] + +# if a subdomain has no outer boundary write None instead, i.e. +# i: None +# if i is the index of the inner subdomain. +outer_boundary_def_points = { + # subdomain number + 1 : subdomain1_outer_boundary_verts, + 2 : subdomain2_outer_boundary_verts +} + +# adjacent_subdomains[i] contains the indices of the subdomains sharing the +# interface i (i.e. given by interface_def_points[i]). +adjacent_subdomains = [[1,2]] +isRichards = { + 1: False, # + 2: False + } + + +############ GRID #######################ü +mesh_resolution = 51 +timestep_size = 0.01 +number_of_timesteps = 50 +# 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, + 'nonwetting': 1}, # + 2 : {'wetting' :1, + 'nonwetting': 1} +} + +porosity = {# +# subdom_num : porosity + 1 : 1,# + 2 : 1 +} + +# Dict of the form: { subdom_num : density } +densities = { + 1: {'wetting': 1, #997, + 'nonwetting': 1}, #1225}, + 2: {'wetting': 1, #997, + 'nonwetting': 1}, #1225}, +} + +gravity_acceleration = 9.81 + +L = {# +# subdom_num : subdomain L for L-scheme + 1 : {'wetting' :0.25, + 'nonwetting': 0.25},# + 2 : {'wetting' :0.25, + 'nonwetting': 0.25} +} + +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} +} + +## relative permeabilty functions on subdomain 1 +def rel_perm1w(s): + # relative permeabilty wetting on subdomain1 + return s**2 + +def rel_perm1nw(s): + # relative permeabilty nonwetting on subdomain1 + return (1-s)**2 + +_rel_perm1w = ft.partial(rel_perm1w) +_rel_perm1nw = ft.partial(rel_perm1nw) + +subdomain1_rel_perm = { + 'wetting': _rel_perm1w,# + 'nonwetting': _rel_perm1nw +} +## relative permeabilty functions on subdomain 2 +def rel_perm2w(s): + # relative permeabilty wetting on subdomain2 + return s**3 +def rel_perm2nw(s): + # relative permeabilty nonwetting on subdosym.cos(0.8*t - (0.8*x + 1/7*y))main2 + return (1-s)**3 + +_rel_perm2w = ft.partial(rel_perm2w) +_rel_perm2nw = ft.partial(rel_perm2nw) + +subdomain2_rel_perm = { + 'wetting': _rel_perm2w,# + 'nonwetting': _rel_perm2nw +} + +## dictionary of relative permeabilties on all domains. +relative_permeability = {# + 1: subdomain1_rel_perm, + 2: subdomain2_rel_perm +} + + +# definition of the derivatives of the relative permeabilities +# relative permeabilty functions on subdomain 1 +def rel_perm1w_prime(s): + # relative permeabilty on subdomain1 + return 2*s + +def rel_perm1nw_prime(s): + # relative permeabilty on subdomain1 + return 2*(1-s) + +# # definition of the derivatives of the relative permeabilities +# # relative permeabilty functions on subdomain 1 +def rel_perm2w_prime(s): + # relative permeabilty on subdomain1 + return 3*s**2 + +def rel_perm2nw_prime(s): + # relative permeabilty on subdomain1 + return 3*(1-s)**2 + +_rel_perm1w_prime = ft.partial(rel_perm1w_prime) +_rel_perm1nw_prime = ft.partial(rel_perm1nw_prime) +_rel_perm2w_prime = ft.partial(rel_perm2w_prime) +_rel_perm2nw_prime = ft.partial(rel_perm2nw_prime) + +subdomain1_rel_perm_prime = { + 'wetting': _rel_perm1w_prime, + 'nonwetting': _rel_perm1nw_prime +} + + +subdomain2_rel_perm_prime = { + 'wetting': _rel_perm2w_prime, + 'nonwetting': _rel_perm2nw_prime +} + +# dictionary of relative permeabilties on all domains. +ka_prime = { + 1: subdomain1_rel_perm_prime, + 2: subdomain2_rel_perm_prime, +} + + + +def saturation(pc, index): + # inverse capillary pressure-saturation-relationship + return df.conditional(pc > 0, 1/((1 + pc)**(1/(index + 1))), 1) + + +def saturation_sym(pc, index): + # inverse capillary pressure-saturation-relationship + return 1/((1 + pc)**(1/(index + 1))) + + +# derivative of S-pc relationship with respect to pc. This is needed for the +# construction of a analytic solution. +def saturation_sym_prime(pc, index): + # inverse capillary pressure-saturation-relationship + return -1/((index+1)*(1 + pc)**((index+2)/(index+1))) + + +# note that the conditional definition of S-pc in the nonsymbolic part will be +# incorporated in the construction of the exact solution below. +S_pc_sym = { + 1: ft.partial(saturation_sym, index=1), + 2: ft.partial(saturation_sym, index=2), + # 3: ft.partial(saturation_sym, index=2), + # 4: ft.partial(saturation_sym, index=1) +} + +S_pc_sym_prime = { + 1: ft.partial(saturation_sym_prime, index=1), + 2: ft.partial(saturation_sym_prime, index=2), + # 3: ft.partial(saturation_sym_prime, index=2), + # 4: ft.partial(saturation_sym_prime, index=1) +} + +sat_pressure_relationship = { + 1: ft.partial(saturation, index=1), + 2: ft.partial(saturation, index=2), + # 3: ft.partial(saturation, index=2), + # 4: ft.partial(saturation, index=1) +} + +# +# def saturation(pc, n_index, alpha): +# # inverse capillary pressure-saturation-relationship +# return df.conditional(pc > 0, 1/((1 + (alpha*pc)**n_index)**((n_index - 1)/n_index)), 1) +# +# # S-pc-relation ship. We use the van Genuchten approach, i.e. pc = 1/alpha*(S^{-1/m} -1)^1/n, where +# # we set alpha = 0, assume m = 1-1/n (see Helmig) and assume that residual saturation is Sw +# def saturation_sym(pc, n_index, alpha): +# # inverse capillary pressure-saturation-relationship +# #df.conditional(pc > 0, +# return 1/((1 + (alpha*pc)**n_index)**((n_index - 1)/n_index)) +# +# +# # derivative of S-pc relationship with respect to pc. This is needed for the +# # construction of a analytic solution. +# def saturation_sym_prime(pc, n_index, alpha): +# # inverse capillary pressure-saturation-relationship +# return -(alpha*(n_index - 1)*(alpha*pc)**(n_index - 1)) / ( (1 + (alpha*pc)**n_index)**((2*n_index - 1)/n_index) ) +# +# # note that the conditional definition of S-pc in the nonsymbolic part will be +# # incorporated in the construction of the exact solution below. +# S_pc_sym = { +# 1: ft.partial(saturation_sym, n_index=3, alpha=0.001), +# 2: ft.partial(saturation_sym, n_index=6, alpha=0.001), +# # 3: ft.partial(saturation_sym, n_index=3, alpha=0.001), +# # 4: ft.partial(saturation_sym, n_index=3, alpha=0.001), +# # 5: ft.partial(saturation_sym, n_index=3, alpha=0.001), +# # 6: ft.partial(saturation_sym, n_index=3, alpha=0.001) +# } +# +# S_pc_sym_prime = { +# 1: ft.partial(saturation_sym_prime, n_index=3, alpha=0.001), +# 2: ft.partial(saturation_sym_prime, n_index=6, alpha=0.001), +# # 3: ft.partial(saturation_sym_prime, n_index=3, alpha=0.001), +# # 4: ft.partial(saturation_sym_prime, n_index=3, alpha=0.001), +# # 5: ft.partial(saturation_sym_prime, n_index=3, alpha=0.001), +# # 6: ft.partial(saturation_sym_prime, n_index=3, alpha=0.001) +# } +# +# sat_pressure_relationship = { +# 1: ft.partial(saturation, n_index=3, alpha=0.001), +# 2: ft.partial(saturation, n_index=6, alpha=0.001), +# # 3: ft.partial(saturation, n_index=3, alpha=0.001), +# # 4: ft.partial(saturation, n_index=3, alpha=0.001), +# # 5: ft.partial(saturation, n_index=3, alpha=0.001), +# # 6: ft.partial(saturation, n_index=3, alpha=0.001) +# } +# + + +############################################# +# Manufacture source expressions with sympy # +############################################# +x, y = sym.symbols('x[0], x[1]') # needed by UFL +t = sym.symbols('t', positive=True) + +p_e_sym = { + 1: {'wetting': 1 - (1+t*t)*(1 + x*x + y*y), + 'nonwetting': -t*(1-y - x**2)**2 - sym.sqrt(2+t**2)*(1-y)}, + 2: {'wetting': 1.0 - (1.0 + t*t)*(1.0 + x*x), + 'nonwetting': -t*(1- x**2)**2 - sym.sqrt(2+t**2)*(1-y)}, +} + +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'], +} + + +# pc_e_sym = { +# 1: -1*p_e_sym[1]['wetting'], +# 2: -1*p_e_sym[2]['wetting'], +# } + +# 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() +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"] + 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]) + +# Dictionary of dirichlet boundary conditions. +dirichletBC = dict() +# similarly 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. + +# subdomain index: {outer boudary part index: {phase: expression}} +for subdomain in isRichards.keys(): + # if subdomain has no outer boundary, outer_boundary_def_points[subdomain] is None + if outer_boundary_def_points[subdomain] is None: + dirichletBC.update({subdomain: None}) + else: + dirichletBC.update({subdomain: dict()}) + # set the dirichlet conditions to be the same code as exact solution on + # the subdomain. + for outer_boundary_ind in outer_boundary_def_points[subdomain].keys(): + dirichletBC[subdomain].update( + {outer_boundary_ind: exact_solution[subdomain]} + ) + + +# def saturation(pressure, subdomain_index): +# # inverse capillary pressure-saturation-relationship +# return df.conditional(pressure < 0, 1/((1 - pressure)**(1/(subdomain_index + 1))), 1) +# +# sa + +write_to_file = { + 'meshes_and_markers': True, + 'L_iterations': True +} + + +# initialise LDD simulation class +simulation = ldd.LDDsimulation(tol = 1E-14, LDDsolver_tol = 1E-7, debug = False) +simulation.set_parameters(output_dir = "./output/",# + subdomain_def_points = subdomain_def_points,# + isRichards = isRichards,# + interface_def_points = interface_def_points,# + outer_boundary_def_points = outer_boundary_def_points,# + adjacent_subdomains = adjacent_subdomains,# + mesh_resolution = mesh_resolution,# + viscosity = viscosity,# + porosity = porosity,# + L = L,# + lambda_param = lambda_param,# + relative_permeability = relative_permeability,# + saturation = sat_pressure_relationship,# + starttime = starttime,# + number_of_timesteps = number_of_timesteps, + number_of_timesteps_to_analyse = number_of_timesteps_to_analyse, + timestep_size = timestep_size,# + sources = source_expression,# + initial_conditions = initial_condition,# + dirichletBC_expression_strings = dirichletBC,# + exact_solution = exact_solution,# + densities=densities, + include_gravity=True, + write2file = write_to_file,# + ) + +simulation.initialise() +# simulation.write_exact_solution_to_xdmf() +simulation.run() diff --git a/TP-multi-patch-plus-gravity-with-same-wetting-phase-as-RR/TP-multi-patch-with-gravity-same-wetting-phase-as-RR.py b/TP-multi-patch-plus-gravity-with-same-wetting-phase-as-RR/TP-multi-patch-with-gravity-same-wetting-phase-as-RR.py new file mode 100755 index 0000000000000000000000000000000000000000..74af2066898977981e19f9b5e7671834901a10a3 --- /dev/null +++ b/TP-multi-patch-plus-gravity-with-same-wetting-phase-as-RR/TP-multi-patch-with-gravity-same-wetting-phase-as-RR.py @@ -0,0 +1,519 @@ +#!/usr/bin/python3 +import dolfin as df +# import mshr +# import numpy as np +import sympy as sym +# import typing as tp +# import domainPatch as dp +import LDDsimulation as ldd +import functools as ft +# import ufl as ufl + +# init sympy session +sym.init_printing() + +# ----------------------------------------------------------------------------# +# ------------------- MESH ---------------------------------------------------# +# ----------------------------------------------------------------------------# +mesh_resolution = 51 +# ----------------------------------------:-------------------------------------# +# ------------------- TIME ---------------------------------------------------# +# ----------------------------------------------------------------------------# +timestep_size = 0.005 +number_of_timesteps = 160 +# 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 + +Lw = 1000 +Lnw = Lw + +l_param_w = 80 +l_param_nw = 80 + +# ----------------------------------------------------------------------------# +# ------------------- Domain and Interface -----------------------------------# +# ----------------------------------------------------------------------------# +# global simulation domain domain +sub_domain0_vertices = [df.Point(-1.0, -1.0), + df.Point(1.0, -1.0), + df.Point(1.0, 1.0), + df.Point(-1.0, 1.0)] +# interfaces +interface12_vertices = [df.Point(0.0, 0.0), + df.Point(1.0, 0.0)] + +interface14_vertices = [df.Point(0.0, 0.0), + df.Point(0.0, 1.0)] + +interface23_vertices = [df.Point(0.0, 0.0), + df.Point(0.0, -1.0)] + +interface34_vertices = [df.Point(-1.0, 0.0), + df.Point(0.0, 0.0)] +# subdomain1. +sub_domain1_vertices = [interface12_vertices[0], + interface12_vertices[1], + sub_domain0_vertices[2], + df.Point(0.0, 1.0)] + +# vertex coordinates of the outer boundaries. If it can not be specified as a +# polygon, use an entry per boundary polygon. This information is used for +# defining the Dirichlet boundary conditions. If a domain is completely inter- +# nal, the dictionary entry should be 0: None +subdomain1_outer_boundary_verts = { + 0: [interface12_vertices[1], + sub_domain0_vertices[2], + df.Point(0.0, 1.0)] +} +# subdomain2 +sub_domain2_vertices = [interface23_vertices[1], + sub_domain0_vertices[1], + interface12_vertices[1], + interface12_vertices[0]] + +subdomain2_outer_boundary_verts = { + 0: [df.Point(0.0, -1.0), + sub_domain0_vertices[1], + interface12_vertices[1]] +} +sub_domain3_vertices = [interface34_vertices[0], + sub_domain0_vertices[0], + interface23_vertices[1], + interface23_vertices[0]] + +subdomain3_outer_boundary_verts = { + 0: [interface34_vertices[0], + sub_domain0_vertices[0], + interface23_vertices[1]] +} + +sub_domain4_vertices = [interface34_vertices[0], + interface34_vertices[1], + interface14_vertices[1], + sub_domain0_vertices[3]] + +subdomain4_outer_boundary_verts = { + 0: [interface14_vertices[1], + sub_domain0_vertices[3], + interface34_vertices[0]] +} + +# list of subdomains given by the boundary polygon vertices. +# Subdomains are given as a list of dolfin points forming +# a closed polygon, such that mshr.Polygon(subdomain_def_points[i]) can be used +# to create the subdomain. subdomain_def_points[0] contains the +# vertices of the global simulation domain and subdomain_def_points[i] contains +# the vertices of the subdomain i. +subdomain_def_points = [sub_domain0_vertices, + sub_domain1_vertices, + sub_domain2_vertices, + sub_domain3_vertices, + sub_domain4_vertices] +# in the below list, index 0 corresponds to the 12 interface which has global +# marker value 1 +interface_def_points = [interface12_vertices, + interface14_vertices, + interface23_vertices, + interface34_vertices] + +# adjacent_subdomains[i] contains the indices of the subdomains sharing the +# interface i (i.e. given by interface_def_points[i]). +adjacent_subdomains = [[1, 2], [1, 4], [2, 3], [3, 4]] + +# if a subdomain has no outer boundary write None instead, i.e. +# i: None +# if i is the index of the inner subdomain. +outer_boundary_def_points = { + # subdomain number + 1: subdomain1_outer_boundary_verts, + 2: subdomain2_outer_boundary_verts, + 3: subdomain3_outer_boundary_verts, + 4: subdomain4_outer_boundary_verts +} + +isRichards = { + 1: False, + 2: False, + 3: False, + 4: False + } + +viscosity = { + 1: {'wetting' :1, + 'nonwetting': 1}, + 2: {'wetting' :1, + 'nonwetting': 1}, + 3: {'wetting' :1, + 'nonwetting': 1}, + 4: {'wetting' :1, + 'nonwetting': 1}, +} + +# Dict of the form: { subdom_num : density } +densities = { + 1: {'wetting': 1, #997 + 'nonwetting':1}, #1.225}}, + 2: {'wetting': 1, #997 + 'nonwetting':1}, #1.225}}, + 3: {'wetting': 1, #997 + 'nonwetting':1}, #1.225}}, + 4: {'wetting': 1, #997 + 'nonwetting':1}, #1.225}} +} + +gravity_acceleration = 9.81 +# porosities taken from +# https://www.geotechdata.info/parameter/soil-porosity.html +# Dict of the form: { subdom_num : porosity } +porosity = { + 1: 1, #0.2, # Clayey gravels, clayey sandy gravels + 2: 1, #0.22, # Silty gravels, silty sandy gravels + 3: 1, #0.37, # Clayey sands + 4: 1, #0.2 # Silty or sandy clay +} + +# subdom_num : subdomain L for L-scheme +L = { + 1: {'wetting' :Lw, + 'nonwetting': Lnw}, + 2: {'wetting' :Lw, + 'nonwetting': Lnw}, + 3: {'wetting' :Lw, + 'nonwetting': Lnw}, + 4: {'wetting' :Lw, + 'nonwetting': Lnw} +} + +# subdom_num : lambda parameter for the L-scheme +lambda_param = { + 1: {'wetting': l_param_w, + 'nonwetting': l_param_nw},# + 2: {'wetting': l_param_w, + 'nonwetting': l_param_nw},# + 3: {'wetting': l_param_w, + 'nonwetting': l_param_nw},# + 4: {'wetting': l_param_w, + 'nonwetting': l_param_nw},# +} + + +# relative permeabilty functions on subdomain 1 +def rel_perm1w(s): + # relative permeabilty on subdomain1 + return s**2 + + +def rel_perm1nw(s): + # relative permeabilty nonwetting on subdomain1 + return (1-s)**2 + + +## relative permeabilty functions on subdomain 2 +# relative permeabilty functions on subdomain 2 +def rel_perm2w(s): + # relative permeabilty on subdomain2 + return s**3 + + +def rel_perm2nw(s): + # relative permeabilty nonwetting on subdosym.cos(0.8*t - (0.8*x + 1/7*y))main2 + return (1-s)**3 + + +_rel_perm1w = ft.partial(rel_perm1w) +_rel_perm1nw = ft.partial(rel_perm1nw) +_rel_perm2w = ft.partial(rel_perm2w) +_rel_perm2nw = ft.partial(rel_perm2nw) + +subdomain1_rel_perm = { + 'wetting': _rel_perm1w,# + 'nonwetting': _rel_perm1nw +} + +subdomain2_rel_perm = { + 'wetting': _rel_perm2w,# + 'nonwetting': _rel_perm2nw +} + + +subdomain3_rel_perm = subdomain2_rel_perm.copy() +subdomain4_rel_perm = subdomain1_rel_perm.copy() + +# dictionary of relative permeabilties on all domains. +relative_permeability = { + 1: subdomain1_rel_perm, + 2: subdomain2_rel_perm, + 3: subdomain3_rel_perm, + 4: subdomain4_rel_perm +} + + +# definition of the derivatives of the relative permeabilities +# relative permeabilty functions on subdomain 1 +def rel_perm1w_prime(s): + # relative permeabilty on subdomain1 + return 2*s + +def rel_perm1nw_prime(s): + # relative permeabilty on subdomain1 + return 2*(1-s) + +# definition of the derivatives of the relative permeabilities +# relative permeabilty functions on subdomain 1 +def rel_perm2w_prime(s): + # relative permeabilty on subdomain1 + return 3*s**2 + +def rel_perm2nw_prime(s): + # relative permeabilty on subdomain1 + return 3*(1-s)**2 + +_rel_perm1w_prime = ft.partial(rel_perm1w_prime) +_rel_perm1nw_prime = ft.partial(rel_perm1nw_prime) +_rel_perm2w_prime = ft.partial(rel_perm2w_prime) +_rel_perm2nw_prime = ft.partial(rel_perm2nw_prime) + +subdomain1_rel_perm_prime = { + 'wetting': _rel_perm1w_prime, + 'nonwetting': _rel_perm1nw_prime +} + + +subdomain2_rel_perm_prime = { + 'wetting': _rel_perm2w_prime, + 'nonwetting': _rel_perm2nw_prime +} + +# _rel_perm3_prime = ft.partial(rel_perm2_prime) +subdomain3_rel_perm_prime = subdomain2_rel_perm_prime.copy() + +# _rel_perm4_prime = ft.partial(rel_perm1_prime) +subdomain4_rel_perm_prime = subdomain1_rel_perm_prime.copy() + +# dictionary of relative permeabilties on all domains. +ka_prime = { + 1: subdomain1_rel_perm_prime, + 2: subdomain2_rel_perm_prime, + 3: subdomain3_rel_perm_prime, + 4: subdomain4_rel_perm_prime +} + + +# this function needs to be monotonically decreasing in the capillary_pressure. +# since in the richards case pc=-pw, this becomes as a function of pw a mono +# tonically INCREASING function like in our Richards-Richards paper. However +# since we unify the treatment in the code for Richards and two-phase, we need +# the same requierment +# for both cases, two-phase and Richards. +def saturation(pc, index): + # inverse capillary pressure-saturation-relationship + return df.conditional(pc > 0, 1/((1 + pc)**(1/(index + 1))), 1) + + +def saturation_sym(pc, index): + # inverse capillary pressure-saturation-relationship + return 1/((1 + pc)**(1/(index + 1))) + + +# derivative of S-pc relationship with respect to pc. This is needed for the +# construction of a analytic solution. +def saturation_sym_prime(pc, index): + # inverse capillary pressure-saturation-relationship + return -1/((index+1)*(1 + pc)**((index+2)/(index+1))) + + +# note that the conditional definition of S-pc in the nonsymbolic part will be +# incorporated in the construction of the exact solution below. +S_pc_sym = { + 1: ft.partial(saturation_sym, index=1), + 2: ft.partial(saturation_sym, index=2), + 3: ft.partial(saturation_sym, index=2), + 4: ft.partial(saturation_sym, index=1) +} + +S_pc_sym_prime = { + 1: ft.partial(saturation_sym_prime, index=1), + 2: ft.partial(saturation_sym_prime, index=2), + 3: ft.partial(saturation_sym_prime, index=2), + 4: ft.partial(saturation_sym_prime, index=1) +} + +sat_pressure_relationship = { + 1: ft.partial(saturation, index=1), + 2: ft.partial(saturation, index=2), + 3: ft.partial(saturation, index=2), + 4: ft.partial(saturation, index=1) +} + +############################################# +# Manufacture source expressions with sympy # +############################################# +x, y = sym.symbols('x[0], x[1]') # needed by UFL +t = sym.symbols('t', positive=True) + +p_e_sym = { + 1: {'wetting': 1.0 - (1.0 + t*t)*(1.0 + x*x + y*y), + 'nonwetting': 0.0*t}, + 2: {'wetting': 1.0 - (1.0 + t*t)*(1.0 + x*x), + 'nonwetting': 0.0*t}, + 3: {'wetting': 1.0 - (1.0 + t*t)*(1.0 + x*x), + 'nonwetting': 0.0*t}, + 4: {'wetting': 1.0 - (1.0 + t*t)*(1.0 + x*x + y*y), + 'nonwetting': 0.0*t} +} + +# 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'] +# } + +pc_e_sym = dict() +for subdomain, isR in isRichards.items(): + if isR: + pc_e_sym.update({subdomain: -p_e_sym[subdomain]['wetting'].copy()}) + else: + pc_e_sym.update({subdomain: p_e_sym[subdomain]['nonwetting'].copy() - p_e_sym[subdomain]['wetting'].copy()}) + +# 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() +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"] + 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]) + +# Dictionary of dirichlet boundary conditions. +dirichletBC = dict() +# similarly 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. + +# subdomain index: {outer boudary part index: {phase: expression}} +for subdomain in isRichards.keys(): + # if subdomain has no outer boundary, outer_boundary_def_points[subdomain] is None + if outer_boundary_def_points[subdomain] is None: + dirichletBC.update({subdomain: None}) + else: + dirichletBC.update({subdomain: dict()}) + # set the dirichlet conditions to be the same code as exact solution on + # the subdomain. + for outer_boundary_ind in outer_boundary_def_points[subdomain].keys(): + dirichletBC[subdomain].update( + {outer_boundary_ind: exact_solution[subdomain]} + ) + + + +write_to_file = { + 'meshes_and_markers': True, + 'L_iterations': True +} + +# initialise LDD simulation class +simulation = ldd.LDDsimulation(tol=1E-14, debug=True, LDDsolver_tol=1E-6) +simulation.set_parameters(output_dir="./output/", + subdomain_def_points=subdomain_def_points, + isRichards=isRichards, + interface_def_points=interface_def_points, + outer_boundary_def_points=outer_boundary_def_points, + adjacent_subdomains=adjacent_subdomains, + mesh_resolution=mesh_resolution, + viscosity=viscosity, + porosity=porosity, + L=L, + lambda_param=lambda_param, + relative_permeability=relative_permeability, + saturation=sat_pressure_relationship, + starttime=starttime, + number_of_timesteps=number_of_timesteps, + number_of_timesteps_to_analyse=number_of_timesteps_to_analyse, + timestep_size=timestep_size, + sources=source_expression, + initial_conditions=initial_condition, + dirichletBC_expression_strings=dirichletBC, + exact_solution=exact_solution, + densities=densities, + include_gravity=True, + write2file=write_to_file, + ) + +simulation.initialise() +# print(simulation.__dict__) +simulation.run() +# simulation.LDDsolver(time=0, debug=True, analyse_timestep=True) +# df.info(parameters, True)