diff --git a/Two-phase-Two-phase/one-patch/mesh_study/R-one-patch-mesh-study-alternative.py b/Two-phase-Two-phase/one-patch/mesh_study/R-one-patch-mesh-study-alternative.py new file mode 100755 index 0000000000000000000000000000000000000000..825595390f4b32d71239cbf6439c8f01ea3f35ea --- /dev/null +++ b/Two-phase-Two-phase/one-patch/mesh_study/R-one-patch-mesh-study-alternative.py @@ -0,0 +1,491 @@ +#!/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 helpers as hlp +import datetime +import os +import pandas as pd + +date = datetime.datetime.now() +datestr = date.strftime("%Y-%m-%d") +#import ufl as ufl + +# init sympy session +sym.init_printing() + +use_case = "R-one-patch-mesh-study" +# solver_tol = 5E-9 +max_iter_num = 1000 +FEM_Lagrange_degree = 1 +mesh_study = True +# resolutions = {128: 1e-7} #[1,2,3,4,5,10,20,40,75,100] +resolutions = { + # 1: 1e-7, + # 2: 1e-7, + # 4: 1e-7, + # 8: 1e-7, + # 16: 1e-7, + 32: 1e-7, + # 64: 1e-7, + # 128: 1e-7, + # 256: 1e-7, + # 512: 1e-7, + } + +############ GRID ####################### +# mesh_resolution = 20 +timestep_size = 0.001 +number_of_timesteps = 10 +plot_timestep_every = 1 +# 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 = 5 +starttimes = [0.5] +# starttimes = [0.0, 0.05] + +# starttimes = { +# 1: 0.0 +# 2: 0.05 +# 4: 0.1 +# 8: 0.2 +# 16: 0.4 +# 32: 0.7 +# 64: 1.0 +# 128: 1.3 +# } + +Lw = 0.5 #/timestep_size +Lnw=Lw + +lambda_w = 0 +lambda_nw = 0 + +include_gravity = False +debugflag = False +analyse_condition = True + +if mesh_study: + output_string = "./output/{}-{}_timesteps{}_P{}".format(datestr, use_case, number_of_timesteps, FEM_Lagrange_degree) +else: + for tol in resolutions.values(): + solver_tol = tol + output_string = "./output/{}-{}_timesteps{}_P{}_solver_tol{}".format(datestr, use_case, number_of_timesteps, FEM_Lagrange_degree, solver_tol) + +# toggle what should be written to files +if mesh_study: + write_to_file = { + 'space_errornorms': True, + 'meshes_and_markers': True, + 'L_iterations_per_timestep': True, + 'solutions': True, + 'absolute_differences': True, + 'condition_numbers': analyse_condition, + 'subsequent_errors': True + } +else: + write_to_file = { + 'space_errornorms': True, + 'meshes_and_markers': True, + 'L_iterations_per_timestep': False, + 'solutions': True, + 'absolute_differences': True, + 'condition_numbers': analyse_condition, + 'subsequent_errors': True + } + +##### 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)] + +subdomain0_outer_boundary_verts = { + 0: [sub_domain0_vertices[0], + sub_domain0_vertices[1], + sub_domain0_vertices[2], + sub_domain0_vertices[3], + sub_domain0_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] +# in the below list, index 0 corresponds to the 12 interface which has index 1 +interface_def_points = None + +# 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 + 0 : subdomain0_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 = None +isRichards = { + 0: True, # + } + +viscosity = {# +# subdom_num : viscosity + 0 : {'wetting' :1, + 'nonwetting': 1}, # +} + +porosity = {# +# subdom_num : porosity + 0: 1,# +} + +# Dict of the form: { subdom_num : density } +densities = { + 0: {'wetting': 1, #997, + 'nonwetting': 1}, #1225} +} + +gravity_acceleration = 9.81 + +L = {# +# subdom_num : subdomain L for L-scheme + 0: {'wetting' :Lw, + 'nonwetting': Lnw},# +} + +lambda_param = {# +# subdom_num : lambda parameter for the L-scheme + 0: {'wetting' :lambda_w, + 'nonwetting': lambda_nw},# +} + +## 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 +} + +## dictionary of relative permeabilties on all domains. +relative_permeability = {# + 0: subdomain1_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) + +_rel_perm1w_prime = ft.partial(rel_perm1w_prime) +_rel_perm1nw_prime = ft.partial(rel_perm1nw_prime) + +subdomain1_rel_perm_prime = { + 'wetting': _rel_perm1w_prime, + 'nonwetting': _rel_perm1nw_prime +} + +# dictionary of relative permeabilties on all domains. +ka_prime = { + 0: subdomain1_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))) + + +# def saturation(pc, index): +# # inverse capillary pressure-saturation-relationship +# return df.conditional(pc > 0, -index*pc, 1) +# +# +# def saturation_sym(pc, index): +# # inverse capillary pressure-saturation-relationship +# return -index*pc +# +# +# # 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 -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 = { + 0: ft.partial(saturation_sym, index=1), +} + +S_pc_sym_prime = { + 0: ft.partial(saturation_sym_prime, index=1), +} + +sat_pressure_relationship = { + 0: 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) + +epsilon_x_inner = 0.7 +epsilon_x_outer = 0.99 +epsilon_y_inner = epsilon_x_inner +epsilon_y_outer = epsilon_x_outer + +def mollifier(x, epsilon): + """ one d mollifier """ + out_expr = sym.exp(-1/(1-(x/epsilon)**2) + 1) + return out_expr + +mollifier_handle = ft.partial(mollifier, epsilon=epsilon_x_inner) + +pw_sym_x = sym.Piecewise( + (mollifier_handle(x), x**2 < epsilon_x_outer**2), + (0, True) +) +pw_sym_y = sym.Piecewise( + (mollifier_handle(y), y**2 < epsilon_y_outer**2), + (0, True) +) + +def mollifier2d(x, y, epsilon): + """ one d mollifier """ + out_expr = sym.exp(-1/(1-(x**2 + y**2)/epsilon**2) + 1) + return out_expr + +mollifier2d_handle = ft.partial(mollifier2d, epsilon=epsilon_x_outer) + +pw_sym2d_x = sym.Piecewise( + (mollifier2d_handle(x, y), x**2 + y**2 < epsilon_x_outer**2), + (0, True) +) + +zero_on_epsilon_shrinking_of_subdomain = sym.Piecewise( + (mollifier_handle(sym.sqrt(x**2 + y**2)+2*epsilon_x_inner), ((-2*epsilon_x_inner<sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<=epsilon_x_inner))), + (mollifier_handle(sym.sqrt(x**2 + y**2)-2*epsilon_x_inner), ((epsilon_x_inner<sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<2*epsilon_x_inner))), + (1, True), +) + +zero_on_epsilon_shrinking_of_subdomain_x = sym.Piecewise( + (mollifier_handle(x+2*epsilon_x_inner), ((-2*epsilon_x_inner<x) & (x<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=x) & (x<=epsilon_x_inner))), + (mollifier_handle(x-2*epsilon_x_inner), ((epsilon_x_inner<x) & (x<2*epsilon_x_inner))), + (1, True), +) + +zero_on_epsilon_shrinking_of_subdomain_y = sym.Piecewise( + (1, y<=-2*epsilon_x_inner), + (mollifier_handle(y+2*epsilon_x_inner), ((-2*epsilon_x_inner<y) & (y<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=y) & (y<=epsilon_x_inner))), + (mollifier_handle(y-2*epsilon_x_inner), ((epsilon_x_inner<y) & (y<2*epsilon_x_inner))), + (1, True), +) + +zero_on_shrinking = zero_on_epsilon_shrinking_of_subdomain #zero_on_epsilon_shrinking_of_subdomain_x + zero_on_epsilon_shrinking_of_subdomain_y +gaussian = pw_sym2d_x# pw_sym_y*pw_sym_x +cutoff = gaussian/(gaussian + zero_on_shrinking) + +# # construction of differentiable characteristic function. +# def smooth_characteristic_func_on_epsilon_shrinking_of_subdomain0(x, y, epsilon_x_inner, epsilon_y_inner, epsilon_x_outer, epsilon_y_outer): +# dist_to_complement_x = ft.partial(mollifier, epsilon=epsilon_x_inner) +# dist_to_complement_y = ft.partial(mollifier, epsilon=epsilon_y_inner) +# dist_to_complement = dist_to_complement_y(y)*dist_to_complement_x(x) +# dist_to_eps_shrinking_x = ft.partial(zero_outside_epsilon_thickening_of_subdomain, epsilon=epsilon_x_outer) +# dist_to_eps_shrinking_y = ft.partial(zero_outside_epsilon_thickening_of_subdomain, epsilon=epsilon_y_outer) +# dist_to_eps_shrinking = dist_to_eps_shrinking_y(y)*dist_to_eps_shrinking_x(x) +# return dist_to_complement/(dist_to_eps_shrinking + dist_to_complement) +# + +# def dist_to_epsilon_thickening_of_subdomain0_complement(x, y, epsilon): +# """ calculates the (euklidian distance)^2 of a point x,y to the epsilon +# thickening of the complement of the domain. +# """ +# is_inside = ((1-sym.Abs(x) > epsilon) & (1-sym.Abs(y) > epsilon)) +# sym.Piecewise((0, is_inside)) + +p_e_sym = { + 0: {'wetting': (-7 -1*t*(1 + x + y)), #*cutoff, + 'nonwetting': (-1 -1*t*(1.1+y + x))}, #*cutoff}, +} + +pc_e_sym = dict() +for subdomain, isR in isRichards.items(): + if isR: + pc_e_sym.update({subdomain: -p_e_sym[subdomain]['wetting']}) + else: + 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() +# 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 +for starttime in starttimes: + for mesh_resolution, solver_tol in resolutions.items(): + # initialise LDD simulation class + simulation = ldd.LDDsimulation( + tol=1E-14, + LDDsolver_tol=solver_tol, + debug=debugflag, + max_iter_num=max_iter_num, + FEM_Lagrange_degree=FEM_Lagrange_degree, + mesh_study=mesh_study + ) + + simulation.set_parameters(use_case=use_case, + output_dir=output_string, + 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, + plot_timestep_every=plot_timestep_every, + timestep_size=timestep_size, + sources=source_expression, + initial_conditions=initial_condition, + dirichletBC_expression_strings=dirichletBC, + exact_solution=exact_solution, + densities=densities, + include_gravity=include_gravity, + write2file=write_to_file, + ) + + simulation.initialise() + output_dir = simulation.output_dir + # simulation.write_exact_solution_to_xdmf() + output = simulation.run(analyse_condition=analyse_condition) + for subdomain_index, subdomain_output in output.items(): + mesh_h = subdomain_output['mesh_size'] + for phase, different_errornorms in subdomain_output['errornorm'].items(): + filename = output_dir + "subdomain{}-space-time-errornorm-{}-phase.csv".format(subdomain_index, phase) + # for errortype, errornorm in different_errornorms.items(): + + # eocfile = open("eoc_filename", "a") + # eocfile.write( str(mesh_h) + " " + str(errornorm) + "\n" ) + # eocfile.close() + # if subdomain.isRichards:mesh_h + data_dict = { + 'mesh_parameter': mesh_resolution, + 'mesh_h': mesh_h, + } + for error_type, errornorms in different_errornorms.items(): + data_dict.update( + {error_type: errornorms} + ) + errors = pd.DataFrame(data_dict, index=[mesh_resolution]) + # check if file exists + if os.path.isfile(filename) == True: + with open(filename, 'a') as f: + errors.to_csv(f, header=False, sep='\t', encoding='utf-8', index=False) + else: + errors.to_csv(filename, sep='\t', encoding='utf-8', index=False) diff --git a/Two-phase-Two-phase/one-patch/mesh_study/R-one-patch-mesh-study.py b/Two-phase-Two-phase/one-patch/mesh_study/R-one-patch-mesh-study.py new file mode 100755 index 0000000000000000000000000000000000000000..ff81ca563e67101e3b0f2b6804c3e2717eaf2fda --- /dev/null +++ b/Two-phase-Two-phase/one-patch/mesh_study/R-one-patch-mesh-study.py @@ -0,0 +1,491 @@ +#!/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 helpers as hlp +import datetime +import os +import pandas as pd + +date = datetime.datetime.now() +datestr = date.strftime("%Y-%m-%d") +#import ufl as ufl + +# init sympy session +sym.init_printing() + +use_case = "R-one-patch-mesh-study" +# solver_tol = 5E-9 +max_iter_num = 1000 +FEM_Lagrange_degree = 1 +mesh_study = True +# resolutions = {128: 1e-7} #[1,2,3,4,5,10,20,40,75,100] +resolutions = { + # 1: 1e-7, + 2: 1e-7, + 4: 1e-7, + 8: 1e-7, + 16: 1e-7, + 32: 1e-7, + 64: 1e-7, + 128: 1e-7, + 256: 1e-7, + 512: 1e-7, + } + +############ GRID ####################### +# mesh_resolution = 20 +timestep_size = 0.01 +number_of_timesteps = 70 +plot_timestep_every = 1 +# 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 = 5 +starttimes = [0.0,0.25,0.5] +# starttimes = [0.0, 0.05] + +# starttimes = { +# 1: 0.0 +# 2: 0.05 +# 4: 0.1 +# 8: 0.2 +# 16: 0.4 +# 32: 0.7 +# 64: 1.0 +# 128: 1.3 +# } + +Lw = 0.025 #/timestep_size +Lnw=Lw + +lambda_w = 0 +lambda_nw = 0 + +include_gravity = True +debugflag = False +analyse_condition = False + +if mesh_study: + output_string = "./output/{}-{}_timesteps{}_P{}".format(datestr, use_case, number_of_timesteps, FEM_Lagrange_degree) +else: + for tol in resolutions.values(): + solver_tol = tol + output_string = "./output/{}-{}_timesteps{}_P{}_solver_tol{}".format(datestr, use_case, number_of_timesteps, FEM_Lagrange_degree, solver_tol) + +# toggle what should be written to files +if mesh_study: + write_to_file = { + 'space_errornorms': True, + 'meshes_and_markers': True, + 'L_iterations_per_timestep': True, + 'solutions': True, + 'absolute_differences': True, + 'condition_numbers': analyse_condition, + 'subsequent_errors': True + } +else: + write_to_file = { + 'space_errornorms': True, + 'meshes_and_markers': True, + 'L_iterations_per_timestep': False, + 'solutions': True, + 'absolute_differences': True, + 'condition_numbers': analyse_condition, + 'subsequent_errors': True + } + +##### 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)] + +subdomain0_outer_boundary_verts = { + 0: [sub_domain0_vertices[0], + sub_domain0_vertices[1], + sub_domain0_vertices[2], + sub_domain0_vertices[3], + sub_domain0_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] +# in the below list, index 0 corresponds to the 12 interface which has index 1 +interface_def_points = None + +# 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 + 0 : subdomain0_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 = None +isRichards = { + 0: True, # + } + +viscosity = {# +# subdom_num : viscosity + 0 : {'wetting' :1, + 'nonwetting': 1}, # +} + +porosity = {# +# subdom_num : porosity + 0: 1,# +} + +# Dict of the form: { subdom_num : density } +densities = { + 0: {'wetting': 1, #997, + 'nonwetting': 1}, #1225} +} + +gravity_acceleration = 9.81 + +L = {# +# subdom_num : subdomain L for L-scheme + 0: {'wetting' :Lw, + 'nonwetting': Lnw},# +} + +lambda_param = {# +# subdom_num : lambda parameter for the L-scheme + 0: {'wetting' :lambda_w, + 'nonwetting': lambda_nw},# +} + +## 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 +} + +## dictionary of relative permeabilties on all domains. +relative_permeability = {# + 0: subdomain1_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) + +_rel_perm1w_prime = ft.partial(rel_perm1w_prime) +_rel_perm1nw_prime = ft.partial(rel_perm1nw_prime) + +subdomain1_rel_perm_prime = { + 'wetting': _rel_perm1w_prime, + 'nonwetting': _rel_perm1nw_prime +} + +# dictionary of relative permeabilties on all domains. +ka_prime = { + 0: subdomain1_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))) + + +# def saturation(pc, index): +# # inverse capillary pressure-saturation-relationship +# return df.conditional(pc > 0, -index*pc, 1) +# +# +# def saturation_sym(pc, index): +# # inverse capillary pressure-saturation-relationship +# return -index*pc +# +# +# # 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 -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 = { + 0: ft.partial(saturation_sym, index=1), +} + +S_pc_sym_prime = { + 0: ft.partial(saturation_sym_prime, index=1), +} + +sat_pressure_relationship = { + 0: 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) + +epsilon_x_inner = 0.7 +epsilon_x_outer = 0.99 +epsilon_y_inner = epsilon_x_inner +epsilon_y_outer = epsilon_x_outer + +def mollifier(x, epsilon): + """ one d mollifier """ + out_expr = sym.exp(-1/(1-(x/epsilon)**2) + 1) + return out_expr + +mollifier_handle = ft.partial(mollifier, epsilon=epsilon_x_inner) + +pw_sym_x = sym.Piecewise( + (mollifier_handle(x), x**2 < epsilon_x_outer**2), + (0, True) +) +pw_sym_y = sym.Piecewise( + (mollifier_handle(y), y**2 < epsilon_y_outer**2), + (0, True) +) + +def mollifier2d(x, y, epsilon): + """ one d mollifier """ + out_expr = sym.exp(-1/(1-(x**2 + y**2)/epsilon**2) + 1) + return out_expr + +mollifier2d_handle = ft.partial(mollifier2d, epsilon=epsilon_x_outer) + +pw_sym2d_x = sym.Piecewise( + (mollifier2d_handle(x, y), x**2 + y**2 < epsilon_x_outer**2), + (0, True) +) + +zero_on_epsilon_shrinking_of_subdomain = sym.Piecewise( + (mollifier_handle(sym.sqrt(x**2 + y**2)+2*epsilon_x_inner), ((-2*epsilon_x_inner<sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<=epsilon_x_inner))), + (mollifier_handle(sym.sqrt(x**2 + y**2)-2*epsilon_x_inner), ((epsilon_x_inner<sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<2*epsilon_x_inner))), + (1, True), +) + +zero_on_epsilon_shrinking_of_subdomain_x = sym.Piecewise( + (mollifier_handle(x+2*epsilon_x_inner), ((-2*epsilon_x_inner<x) & (x<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=x) & (x<=epsilon_x_inner))), + (mollifier_handle(x-2*epsilon_x_inner), ((epsilon_x_inner<x) & (x<2*epsilon_x_inner))), + (1, True), +) + +zero_on_epsilon_shrinking_of_subdomain_y = sym.Piecewise( + (1, y<=-2*epsilon_x_inner), + (mollifier_handle(y+2*epsilon_x_inner), ((-2*epsilon_x_inner<y) & (y<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=y) & (y<=epsilon_x_inner))), + (mollifier_handle(y-2*epsilon_x_inner), ((epsilon_x_inner<y) & (y<2*epsilon_x_inner))), + (1, True), +) + +zero_on_shrinking = zero_on_epsilon_shrinking_of_subdomain #zero_on_epsilon_shrinking_of_subdomain_x + zero_on_epsilon_shrinking_of_subdomain_y +gaussian = pw_sym2d_x# pw_sym_y*pw_sym_x +cutoff = gaussian/(gaussian + zero_on_shrinking) + +# # construction of differentiable characteristic function. +# def smooth_characteristic_func_on_epsilon_shrinking_of_subdomain0(x, y, epsilon_x_inner, epsilon_y_inner, epsilon_x_outer, epsilon_y_outer): +# dist_to_complement_x = ft.partial(mollifier, epsilon=epsilon_x_inner) +# dist_to_complement_y = ft.partial(mollifier, epsilon=epsilon_y_inner) +# dist_to_complement = dist_to_complement_y(y)*dist_to_complement_x(x) +# dist_to_eps_shrinking_x = ft.partial(zero_outside_epsilon_thickening_of_subdomain, epsilon=epsilon_x_outer) +# dist_to_eps_shrinking_y = ft.partial(zero_outside_epsilon_thickening_of_subdomain, epsilon=epsilon_y_outer) +# dist_to_eps_shrinking = dist_to_eps_shrinking_y(y)*dist_to_eps_shrinking_x(x) +# return dist_to_complement/(dist_to_eps_shrinking + dist_to_complement) +# + +# def dist_to_epsilon_thickening_of_subdomain0_complement(x, y, epsilon): +# """ calculates the (euklidian distance)^2 of a point x,y to the epsilon +# thickening of the complement of the domain. +# """ +# is_inside = ((1-sym.Abs(x) > epsilon) & (1-sym.Abs(y) > epsilon)) +# sym.Piecewise((0, is_inside)) + +p_e_sym = { + 0: {'wetting': (-7 - (1+t*t)*(1 + x*x + y*y)), #*cutoff, + 'nonwetting': (-1 -t*(1.1+y + x**2))}, #*cutoff}, +} + +pc_e_sym = dict() +for subdomain, isR in isRichards.items(): + if isR: + pc_e_sym.update({subdomain: -p_e_sym[subdomain]['wetting']}) + else: + 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() +# 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 +for starttime in starttimes: + for mesh_resolution, solver_tol in resolutions.items(): + # initialise LDD simulation class + simulation = ldd.LDDsimulation( + tol=1E-14, + LDDsolver_tol=solver_tol, + debug=debugflag, + max_iter_num=max_iter_num, + FEM_Lagrange_degree=FEM_Lagrange_degree, + mesh_study=mesh_study + ) + + simulation.set_parameters(use_case=use_case, + output_dir=output_string, + 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, + plot_timestep_every=plot_timestep_every, + timestep_size=timestep_size, + sources=source_expression, + initial_conditions=initial_condition, + dirichletBC_expression_strings=dirichletBC, + exact_solution=exact_solution, + densities=densities, + include_gravity=include_gravity, + write2file=write_to_file, + ) + + simulation.initialise() + output_dir = simulation.output_dir + # simulation.write_exact_solution_to_xdmf() + output = simulation.run(analyse_condition=analyse_condition) + for subdomain_index, subdomain_output in output.items(): + mesh_h = subdomain_output['mesh_size'] + for phase, different_errornorms in subdomain_output['errornorm'].items(): + filename = output_dir + "subdomain{}-space-time-errornorm-{}-phase.csv".format(subdomain_index, phase) + # for errortype, errornorm in different_errornorms.items(): + + # eocfile = open("eoc_filename", "a") + # eocfile.write( str(mesh_h) + " " + str(errornorm) + "\n" ) + # eocfile.close() + # if subdomain.isRichards:mesh_h + data_dict = { + 'mesh_parameter': mesh_resolution, + 'mesh_h': mesh_h, + } + for error_type, errornorms in different_errornorms.items(): + data_dict.update( + {error_type: errornorms} + ) + errors = pd.DataFrame(data_dict, index=[mesh_resolution]) + # check if file exists + if os.path.isfile(filename) == True: + with open(filename, 'a') as f: + errors.to_csv(f, header=False, sep='\t', encoding='utf-8', index=False) + else: + errors.to_csv(filename, sep='\t', encoding='utf-8', index=False) diff --git a/Two-phase-Two-phase/one-patch/mesh_study/TP-one-patch-mesh-study.py b/Two-phase-Two-phase/one-patch/mesh_study/TP-one-patch-mesh-study.py new file mode 100755 index 0000000000000000000000000000000000000000..bed62b609b08a817ee764588d230195c31e6a9d2 --- /dev/null +++ b/Two-phase-Two-phase/one-patch/mesh_study/TP-one-patch-mesh-study.py @@ -0,0 +1,471 @@ +#!/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 helpers as hlp +import datetime +import os +import pandas as pd + +date = datetime.datetime.now() +datestr = date.strftime("%Y-%m-%d") +#import ufl as ufl + +# init sympy session +sym.init_printing() + +use_case = "TP-one-patch" +# solver_tol = 5E-9 +max_iter_num = 500 +FEM_Lagrange_degree = 1 +mesh_study = True +# resolutions = {128: 1e-7} #[1,2,3,4,5,10,20,40,75,100] +resolutions = { 1: 1e-7, + 2: 1e-7, + 4: 1e-7, + 8: 1e-7, + 16: 1e-7, + 32: 1e-7, + 64: 1e-7, + 128: 1e-7, + 256: 1e-7} + +############ GRID ####################### +# mesh_resolution = 20 +timestep_size = 0.01 +number_of_timesteps = 80 +plot_timestep_every = 1 +# 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 = 4 +starttime = 0.0 + +Lw = 0.025 #/timestep_size +Lnw=Lw + +lambda_w = 40 +lambda_nw = 40 + +include_gravity = False +debugflag = False +analyse_condition = False + +output_string = "./output/{}-{}_timesteps{}_P{}".format(datestr, use_case, number_of_timesteps, FEM_Lagrange_degree) + +# toggle what should be written to files +if mesh_study: + write_to_file = { + 'space_errornorms': True, + 'meshes_and_markers': True, + 'L_iterations_per_timestep': False, + 'solutions': True, + 'absolute_differences': False, + 'condition_numbers': analyse_condition, + 'subsequent_errors': True + } +else: + write_to_file = { + 'space_errornorms': True, + 'meshes_and_markers': True, + 'L_iterations_per_timestep': False, + 'solutions': True, + 'absolute_differences': True, + 'condition_numbers': analyse_condition, + 'subsequent_errors': True + } + +##### 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)] + +subdomain0_outer_boundary_verts = { + 0: [sub_domain0_vertices[0], + sub_domain0_vertices[1], + sub_domain0_vertices[2], + sub_domain0_vertices[3], + sub_domain0_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] +# in the below list, index 0 corresponds to the 12 interface which has index 1 +interface_def_points = None + +# 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 + 0 : subdomain0_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 = None +isRichards = { + 0: False, # + } + +viscosity = {# +# subdom_num : viscosity + 0 : {'wetting' :1, + 'nonwetting': 1}, # +} + +porosity = {# +# subdom_num : porosity + 0: 1,# +} + +# Dict of the form: { subdom_num : density } +densities = { + 0: {'wetting': 1, #997, + 'nonwetting': 1}, #1225} +} + +gravity_acceleration = 9.81 + +L = {# +# subdom_num : subdomain L for L-scheme + 0: {'wetting' :Lw, + 'nonwetting': Lnw},# +} + +lambda_param = {# +# subdom_num : lambda parameter for the L-scheme + 0: {'wetting' :lambda_w, + 'nonwetting': lambda_nw},# +} + +## 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 +} + +## dictionary of relative permeabilties on all domains. +relative_permeability = {# + 0: subdomain1_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) + +_rel_perm1w_prime = ft.partial(rel_perm1w_prime) +_rel_perm1nw_prime = ft.partial(rel_perm1nw_prime) + +subdomain1_rel_perm_prime = { + 'wetting': _rel_perm1w_prime, + 'nonwetting': _rel_perm1nw_prime +} + +# dictionary of relative permeabilties on all domains. +ka_prime = { + 0: subdomain1_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))) + + +# def saturation(pc, index): +# # inverse capillary pressure-saturation-relationship +# return df.conditional(pc > 0, -index*pc, 1) +# +# +# def saturation_sym(pc, index): +# # inverse capillary pressure-saturation-relationship +# return -index*pc +# +# +# # 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 -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 = { + 0: ft.partial(saturation_sym, index=1), +} + +S_pc_sym_prime = { + 0: ft.partial(saturation_sym_prime, index=1), +} + +sat_pressure_relationship = { + 0: 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) + +epsilon_x_inner = 0.7 +epsilon_x_outer = 0.99 +epsilon_y_inner = epsilon_x_inner +epsilon_y_outer = epsilon_x_outer + +def mollifier(x, epsilon): + """ one d mollifier """ + out_expr = sym.exp(-1/(1-(x/epsilon)**2) + 1) + return out_expr + +mollifier_handle = ft.partial(mollifier, epsilon=epsilon_x_inner) + +pw_sym_x = sym.Piecewise( + (mollifier_handle(x), x**2 < epsilon_x_outer**2), + (0, True) +) +pw_sym_y = sym.Piecewise( + (mollifier_handle(y), y**2 < epsilon_y_outer**2), + (0, True) +) + +def mollifier2d(x, y, epsilon): + """ one d mollifier """ + out_expr = sym.exp(-1/(1-(x**2 + y**2)/epsilon**2) + 1) + return out_expr + +mollifier2d_handle = ft.partial(mollifier2d, epsilon=epsilon_x_outer) + +pw_sym2d_x = sym.Piecewise( + (mollifier2d_handle(x, y), x**2 + y**2 < epsilon_x_outer**2), + (0, True) +) + +zero_on_epsilon_shrinking_of_subdomain = sym.Piecewise( + (mollifier_handle(sym.sqrt(x**2 + y**2)+2*epsilon_x_inner), ((-2*epsilon_x_inner<sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<=epsilon_x_inner))), + (mollifier_handle(sym.sqrt(x**2 + y**2)-2*epsilon_x_inner), ((epsilon_x_inner<sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<2*epsilon_x_inner))), + (1, True), +) + +zero_on_epsilon_shrinking_of_subdomain_x = sym.Piecewise( + (mollifier_handle(x+2*epsilon_x_inner), ((-2*epsilon_x_inner<x) & (x<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=x) & (x<=epsilon_x_inner))), + (mollifier_handle(x-2*epsilon_x_inner), ((epsilon_x_inner<x) & (x<2*epsilon_x_inner))), + (1, True), +) + +zero_on_epsilon_shrinking_of_subdomain_y = sym.Piecewise( + (1, y<=-2*epsilon_x_inner), + (mollifier_handle(y+2*epsilon_x_inner), ((-2*epsilon_x_inner<y) & (y<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=y) & (y<=epsilon_x_inner))), + (mollifier_handle(y-2*epsilon_x_inner), ((epsilon_x_inner<y) & (y<2*epsilon_x_inner))), + (1, True), +) + +zero_on_shrinking = zero_on_epsilon_shrinking_of_subdomain #zero_on_epsilon_shrinking_of_subdomain_x + zero_on_epsilon_shrinking_of_subdomain_y +gaussian = pw_sym2d_x# pw_sym_y*pw_sym_x +cutoff = gaussian/(gaussian + zero_on_shrinking) + +# # construction of differentiable characteristic function. +# def smooth_characteristic_func_on_epsilon_shrinking_of_subdomain0(x, y, epsilon_x_inner, epsilon_y_inner, epsilon_x_outer, epsilon_y_outer): +# dist_to_complement_x = ft.partial(mollifier, epsilon=epsilon_x_inner) +# dist_to_complement_y = ft.partial(mollifier, epsilon=epsilon_y_inner) +# dist_to_complement = dist_to_complement_y(y)*dist_to_complement_x(x) +# dist_to_eps_shrinking_x = ft.partial(zero_outside_epsilon_thickening_of_subdomain, epsilon=epsilon_x_outer) +# dist_to_eps_shrinking_y = ft.partial(zero_outside_epsilon_thickening_of_subdomain, epsilon=epsilon_y_outer) +# dist_to_eps_shrinking = dist_to_eps_shrinking_y(y)*dist_to_eps_shrinking_x(x) +# return dist_to_complement/(dist_to_eps_shrinking + dist_to_complement) +# + +# def dist_to_epsilon_thickening_of_subdomain0_complement(x, y, epsilon): +# """ calculates the (euklidian distance)^2 of a point x,y to the epsilon +# thickening of the complement of the domain. +# """ +# is_inside = ((1-sym.Abs(x) > epsilon) & (1-sym.Abs(y) > epsilon)) +# sym.Piecewise((0, is_inside)) + +p_e_sym = { + 0: {'wetting': (-7 - (1+t*t)*(1 + x*x + y*y)), #*cutoff, + 'nonwetting': (-1 -t*(1.1+y + x**2))}, #*cutoff}, +} + +pc_e_sym = dict() +for subdomain, isR in isRichards.items(): + if isR: + pc_e_sym.update({subdomain: -p_e_sym[subdomain]['wetting']}) + else: + 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() +# 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 + +for mesh_resolution, solver_tol in resolutions.items(): + # initialise LDD simulation class + simulation = ldd.LDDsimulation( + tol=1E-14, + LDDsolver_tol=solver_tol, + debug=debugflag, + max_iter_num=max_iter_num, + FEM_Lagrange_degree=FEM_Lagrange_degree, + mesh_study=mesh_study + ) + + simulation.set_parameters(use_case=use_case, + output_dir=output_string, + 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, + plot_timestep_every=plot_timestep_every, + timestep_size=timestep_size, + sources=source_expression, + initial_conditions=initial_condition, + dirichletBC_expression_strings=dirichletBC, + exact_solution=exact_solution, + densities=densities, + include_gravity=include_gravity, + write2file=write_to_file, + ) + + simulation.initialise() + output_dir = simulation.output_dir + # simulation.write_exact_solution_to_xdmf() + output = simulation.run(analyse_condition=analyse_condition) + for subdomain_index, subdomain_output in output.items(): + mesh_h = subdomain_output['mesh_size'] + for phase, different_errornorms in subdomain_output['errornorm'].items(): + filename = output_dir + "subdomain{}-space-time-errornorm-{}-phase.csv".format(subdomain_index, phase) + # for errortype, errornorm in different_errornorms.items(): + + # eocfile = open("eoc_filename", "a") + # eocfile.write( str(mesh_h) + " " + str(errornorm) + "\n" ) + # eocfile.close() + # if subdomain.isRichards:mesh_h + data_dict = { + 'mesh_parameter': mesh_resolution, + 'mesh_h': mesh_h, + } + for error_type, errornorms in different_errornorms.items(): + data_dict.update( + {error_type: errornorms} + ) + errors = pd.DataFrame(data_dict, index=[mesh_resolution]) + # check if file exists + if os.path.isfile(filename) == True: + with open(filename, 'a') as f: + errors.to_csv(f, header=False, sep='\t', encoding='utf-8', index=False) + else: + errors.to_csv(filename, sep='\t', encoding='utf-8', index=False) diff --git a/Two-phase-Two-phase/one-patch/mesh_study/run-simulation b/Two-phase-Two-phase/one-patch/mesh_study/run-simulation new file mode 100755 index 0000000000000000000000000000000000000000..0eb497502a082a0fec07a5449b1fe946d59c8cc7 --- /dev/null +++ b/Two-phase-Two-phase/one-patch/mesh_study/run-simulation @@ -0,0 +1,16 @@ +#!/bin/bash + +[ $# -eq 0 ] && { echo "Usage: $0 simulation_file [logfile_name]"; exit 1; } + +SIMULATION_FILE=$1 +SIMULATION=${SIMULATION_FILE%.py} +LOGFILE_DEFAULT="$SIMULATION.log" + +DATE=$(date -I) +LOGFILE=${2:-$DATE-$LOGFILE_DEFAULT} + +GREETING="Simulation $SIMULATION is run on $DATE by $USER" + +echo $GREETING +echo "running $SIMULATION_FILE | tee $LOGFILE" +./$SIMULATION_FILE | tee $LOGFILE diff --git a/Two-phase-Two-phase/one-patch/mesh_study_for_fixed_timestep/R-one-patch-mesh-study-fixed-timestep.py b/Two-phase-Two-phase/one-patch/mesh_study_for_fixed_timestep/R-one-patch-mesh-study-fixed-timestep.py new file mode 100755 index 0000000000000000000000000000000000000000..14677c93cdbe98edb0217fbb0021084a48e7e232 --- /dev/null +++ b/Two-phase-Two-phase/one-patch/mesh_study_for_fixed_timestep/R-one-patch-mesh-study-fixed-timestep.py @@ -0,0 +1,491 @@ +#!/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 helpers as hlp +import datetime +import os +import pandas as pd + +date = datetime.datetime.now() +datestr = date.strftime("%Y-%m-%d") +#import ufl as ufl + +# init sympy session +sym.init_printing() + +use_case = "R-one-patch-mesh-study-fixed-timestep-new-errornorm" +# solver_tol = 5E-9 +max_iter_num = 1000 +FEM_Lagrange_degree = 1 +mesh_study = True +# resolutions = {128: 1e-8} #[1,2,3,4,5,10,20,40,75,100] +resolutions = { + 1: 1e-8, + 2: 1e-8, + 4: 1e-8, + 8: 1e-8, + 16: 1e-8, + 32: 1e-8, + 64: 1e-8, + # 128: 1e-8, + # 256: 1e-8, + # 512: 1e-8, + } + +############ GRID ####################### +# mesh_resolution = 20 +timestep_size = 0.012 +number_of_timesteps = 1 +plot_timestep_every = 1 +# 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 = 1 +starttimes = [0.0] +# starttimes = [0.0, 0.05] + +# starttimes = { +# 1: 0.0 +# 2: 0.05 +# 4: 0.1 +# 8: 0.2 +# 16: 0.4 +# 32: 0.7 +# 64: 1.0 +# 128: 1.3 +# } + +Lw = 0.025 #/timestep_size +Lnw=Lw + +lambda_w = 0 +lambda_nw = 0 + +include_gravity = False +debugflag = True +analyse_condition = False + +if mesh_study: + output_string = "./output/{}-{}_timesteps{}_P{}".format(datestr, use_case, number_of_timesteps, FEM_Lagrange_degree) +else: + for tol in resolutions.values(): + solver_tol = tol + output_string = "./output/{}-{}_timesteps{}_P{}_solver_tol{}".format(datestr, use_case, number_of_timesteps, FEM_Lagrange_degree, solver_tol) + +# toggle what should be written to files +if mesh_study: + write_to_file = { + 'space_errornorms': True, + 'meshes_and_markers': True, + 'L_iterations_per_timestep': True, + 'solutions': True, + 'absolute_differences': True, + 'condition_numbers': analyse_condition, + 'subsequent_errors': True + } +else: + write_to_file = { + 'space_errornorms': True, + 'meshes_and_markers': True, + 'L_iterations_per_timestep': False, + 'solutions': True, + 'absolute_differences': True, + 'condition_numbers': analyse_condition, + 'subsequent_errors': True + } + +##### 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)] + +subdomain0_outer_boundary_verts = { + 0: [sub_domain0_vertices[0], + sub_domain0_vertices[1], + sub_domain0_vertices[2], + sub_domain0_vertices[3], + sub_domain0_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] +# in the below list, index 0 corresponds to the 12 interface which has index 1 +interface_def_points = None + +# 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 + 0 : subdomain0_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 = None +isRichards = { + 0: True, # + } + +viscosity = {# +# subdom_num : viscosity + 0 : {'wetting' :1, + 'nonwetting': 1}, # +} + +porosity = {# +# subdom_num : porosity + 0: 1,# +} + +# Dict of the form: { subdom_num : density } +densities = { + 0: {'wetting': 1, #997, + 'nonwetting': 1}, #1225} +} + +gravity_acceleration = 9.81 + +L = {# +# subdom_num : subdomain L for L-scheme + 0: {'wetting' :Lw, + 'nonwetting': Lnw},# +} + +lambda_param = {# +# subdom_num : lambda parameter for the L-scheme + 0: {'wetting' :lambda_w, + 'nonwetting': lambda_nw},# +} + +## 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 +} + +## dictionary of relative permeabilties on all domains. +relative_permeability = {# + 0: subdomain1_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) + +_rel_perm1w_prime = ft.partial(rel_perm1w_prime) +_rel_perm1nw_prime = ft.partial(rel_perm1nw_prime) + +subdomain1_rel_perm_prime = { + 'wetting': _rel_perm1w_prime, + 'nonwetting': _rel_perm1nw_prime +} + +# dictionary of relative permeabilties on all domains. +ka_prime = { + 0: subdomain1_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))) + + +# def saturation(pc, index): +# # inverse capillary pressure-saturation-relationship +# return df.conditional(pc > 0, -index*pc, 1) +# +# +# def saturation_sym(pc, index): +# # inverse capillary pressure-saturation-relationship +# return -index*pc +# +# +# # 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 -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 = { + 0: ft.partial(saturation_sym, index=1), +} + +S_pc_sym_prime = { + 0: ft.partial(saturation_sym_prime, index=1), +} + +sat_pressure_relationship = { + 0: 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) + +epsilon_x_inner = 0.7 +epsilon_x_outer = 0.99 +epsilon_y_inner = epsilon_x_inner +epsilon_y_outer = epsilon_x_outer + +def mollifier(x, epsilon): + """ one d mollifier """ + out_expr = sym.exp(-1/(1-(x/epsilon)**2) + 1) + return out_expr + +mollifier_handle = ft.partial(mollifier, epsilon=epsilon_x_inner) + +pw_sym_x = sym.Piecewise( + (mollifier_handle(x), x**2 < epsilon_x_outer**2), + (0, True) +) +pw_sym_y = sym.Piecewise( + (mollifier_handle(y), y**2 < epsilon_y_outer**2), + (0, True) +) + +def mollifier2d(x, y, epsilon): + """ one d mollifier """ + out_expr = sym.exp(-1/(1-(x**2 + y**2)/epsilon**2) + 1) + return out_expr + +mollifier2d_handle = ft.partial(mollifier2d, epsilon=epsilon_x_outer) + +pw_sym2d_x = sym.Piecewise( + (mollifier2d_handle(x, y), x**2 + y**2 < epsilon_x_outer**2), + (0, True) +) + +zero_on_epsilon_shrinking_of_subdomain = sym.Piecewise( + (mollifier_handle(sym.sqrt(x**2 + y**2)+2*epsilon_x_inner), ((-2*epsilon_x_inner<sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<=epsilon_x_inner))), + (mollifier_handle(sym.sqrt(x**2 + y**2)-2*epsilon_x_inner), ((epsilon_x_inner<sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<2*epsilon_x_inner))), + (1, True), +) + +zero_on_epsilon_shrinking_of_subdomain_x = sym.Piecewise( + (mollifier_handle(x+2*epsilon_x_inner), ((-2*epsilon_x_inner<x) & (x<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=x) & (x<=epsilon_x_inner))), + (mollifier_handle(x-2*epsilon_x_inner), ((epsilon_x_inner<x) & (x<2*epsilon_x_inner))), + (1, True), +) + +zero_on_epsilon_shrinking_of_subdomain_y = sym.Piecewise( + (1, y<=-2*epsilon_x_inner), + (mollifier_handle(y+2*epsilon_x_inner), ((-2*epsilon_x_inner<y) & (y<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=y) & (y<=epsilon_x_inner))), + (mollifier_handle(y-2*epsilon_x_inner), ((epsilon_x_inner<y) & (y<2*epsilon_x_inner))), + (1, True), +) + +zero_on_shrinking = zero_on_epsilon_shrinking_of_subdomain #zero_on_epsilon_shrinking_of_subdomain_x + zero_on_epsilon_shrinking_of_subdomain_y +gaussian = pw_sym2d_x# pw_sym_y*pw_sym_x +cutoff = gaussian/(gaussian + zero_on_shrinking) + +# # construction of differentiable characteristic function. +# def smooth_characteristic_func_on_epsilon_shrinking_of_subdomain0(x, y, epsilon_x_inner, epsilon_y_inner, epsilon_x_outer, epsilon_y_outer): +# dist_to_complement_x = ft.partial(mollifier, epsilon=epsilon_x_inner) +# dist_to_complement_y = ft.partial(mollifier, epsilon=epsilon_y_inner) +# dist_to_complement = dist_to_complement_y(y)*dist_to_complement_x(x) +# dist_to_eps_shrinking_x = ft.partial(zero_outside_epsilon_thickening_of_subdomain, epsilon=epsilon_x_outer) +# dist_to_eps_shrinking_y = ft.partial(zero_outside_epsilon_thickening_of_subdomain, epsilon=epsilon_y_outer) +# dist_to_eps_shrinking = dist_to_eps_shrinking_y(y)*dist_to_eps_shrinking_x(x) +# return dist_to_complement/(dist_to_eps_shrinking + dist_to_complement) +# + +# def dist_to_epsilon_thickening_of_subdomain0_complement(x, y, epsilon): +# """ calculates the (euklidian distance)^2 of a point x,y to the epsilon +# thickening of the complement of the domain. +# """ +# is_inside = ((1-sym.Abs(x) > epsilon) & (1-sym.Abs(y) > epsilon)) +# sym.Piecewise((0, is_inside)) + +p_e_sym = { + 0: {'wetting': (-7 - (1+t*t)*(1 + x*x + y*y)), #*cutoff, + 'nonwetting': (-1 -t*(1.1+y + x**2))}, #*cutoff}, +} + +pc_e_sym = dict() +for subdomain, isR in isRichards.items(): + if isR: + pc_e_sym.update({subdomain: -p_e_sym[subdomain]['wetting']}) + else: + 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() +# 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 +for starttime in starttimes: + for mesh_resolution, solver_tol in resolutions.items(): + # initialise LDD simulation class + simulation = ldd.LDDsimulation( + tol=1E-14, + LDDsolver_tol=solver_tol, + debug=debugflag, + max_iter_num=max_iter_num, + FEM_Lagrange_degree=FEM_Lagrange_degree, + mesh_study=mesh_study + ) + + simulation.set_parameters(use_case=use_case, + output_dir=output_string, + 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, + plot_timestep_every=plot_timestep_every, + timestep_size=timestep_size, + sources=source_expression, + initial_conditions=initial_condition, + dirichletBC_expression_strings=dirichletBC, + exact_solution=exact_solution, + densities=densities, + include_gravity=include_gravity, + write2file=write_to_file, + ) + + simulation.initialise() + output_dir = simulation.output_dir + # simulation.write_exact_solution_to_xdmf() + output = simulation.run(analyse_condition=analyse_condition) + for subdomain_index, subdomain_output in output.items(): + mesh_h = subdomain_output['mesh_size'] + for phase, different_errornorms in subdomain_output['errornorm'].items(): + filename = output_dir + "subdomain{}-space-time-errornorm-{}-phase.csv".format(subdomain_index, phase) + # for errortype, errornorm in different_errornorms.items(): + + # eocfile = open("eoc_filename", "a") + # eocfile.write( str(mesh_h) + " " + str(errornorm) + "\n" ) + # eocfile.close() + # if subdomain.isRichards:mesh_h + data_dict = { + 'mesh_parameter': mesh_resolution, + 'mesh_h': mesh_h, + } + for error_type, errornorms in different_errornorms.items(): + data_dict.update( + {error_type: errornorms} + ) + errors = pd.DataFrame(data_dict, index=[mesh_resolution]) + # check if file exists + if os.path.isfile(filename) == True: + with open(filename, 'a') as f: + errors.to_csv(f, header=False, sep='\t', encoding='utf-8', index=False) + else: + errors.to_csv(filename, sep='\t', encoding='utf-8', index=False) diff --git a/Two-phase-Two-phase/one-patch/mesh_study_for_fixed_timestep/TP-one-patch-mesh-study-fixed-timestep-constant-pressures.py b/Two-phase-Two-phase/one-patch/mesh_study_for_fixed_timestep/TP-one-patch-mesh-study-fixed-timestep-constant-pressures.py new file mode 100755 index 0000000000000000000000000000000000000000..3816aa6041dafdc822e600be7ba2ee2f13e2c3dc --- /dev/null +++ b/Two-phase-Two-phase/one-patch/mesh_study_for_fixed_timestep/TP-one-patch-mesh-study-fixed-timestep-constant-pressures.py @@ -0,0 +1,490 @@ +#!/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 helpers as hlp +import datetime +import os +import pandas as pd + +date = datetime.datetime.now() +datestr = date.strftime("%Y-%m-%d") +#import ufl as ufl + +# init sympy session +sym.init_printing() + +use_case = "TP-one-patch-mesh-study-fixed-timestep-constant-pressures" +# solver_tol = 5E-9 +max_iter_num = 2000 +FEM_Lagrange_degree = 1 +mesh_study = True +# resolutions = {128: 1e-7} #[1,2,3,4,5,10,20,40,75,100] +resolutions = { + 1: 1e-10, + 2: 1e-10, + 4: 1e-10, + 8: 1e-10, + 16: 1e-10, + 32: 1e-10, + 64: 1e-10, + 128: 1e-10, + 256: 1e-10, + 512: 1e-10, + } + +############ GRID ####################### +# mesh_resolution = 20 +timestep_size = 0.01 +number_of_timesteps = 1 +plot_timestep_every = 1 +# 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 = 1 +starttimes = [0.0, 0.05, 0.1, 0.7, 1.3] + +# starttimes = { +# 1: 0.0 +# 2: 0.05 +# 4: 0.1 +# 8: 0.2 +# 16: 0.4 +# 32: 0.7 +# 64: 1.0 +# 128: 1.3 +# } + +Lw = 0.05 #/timestep_size +Lnw=Lw + +lambda_w = 0 +lambda_nw = 0 + +include_gravity = False +debugflag = True +analyse_condition = False + +if mesh_study: + output_string = "./output/{}-{}_timesteps{}_P{}".format(datestr, use_case, number_of_timesteps, FEM_Lagrange_degree) +else: + for tol in resolutions.values(): + solver_tol = tol + output_string = "./output/{}-{}_timesteps{}_P{}_solver_tol{}".format(datestr, use_case, number_of_timesteps, FEM_Lagrange_degree, solver_tol) + +# toggle what should be written to files +if mesh_study: + write_to_file = { + 'space_errornorms': True, + 'meshes_and_markers': True, + 'L_iterations_per_timestep': True, + 'solutions': True, + 'absolute_differences': True, + 'condition_numbers': analyse_condition, + 'subsequent_errors': True + } +else: + write_to_file = { + 'space_errornorms': True, + 'meshes_and_markers': True, + 'L_iterations_per_timestep': False, + 'solutions': True, + 'absolute_differences': True, + 'condition_numbers': analyse_condition, + 'subsequent_errors': True + } + +##### 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)] + +subdomain0_outer_boundary_verts = { + 0: [sub_domain0_vertices[0], + sub_domain0_vertices[1], + sub_domain0_vertices[2], + sub_domain0_vertices[3], + sub_domain0_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] +# in the below list, index 0 corresponds to the 12 interface which has index 1 +interface_def_points = None + +# 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 + 0 : subdomain0_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 = None +isRichards = { + 0: False, # + } + +viscosity = {# +# subdom_num : viscosity + 0 : {'wetting' :1, + 'nonwetting': 1}, # +} + +porosity = {# +# subdom_num : porosity + 0: 1,# +} + +# Dict of the form: { subdom_num : density } +densities = { + 0: {'wetting': 1, #997, + 'nonwetting': 1}, #1225} +} + +gravity_acceleration = 9.81 + +L = {# +# subdom_num : subdomain L for L-scheme + 0: {'wetting' :Lw, + 'nonwetting': Lnw},# +} + +lambda_param = {# +# subdom_num : lambda parameter for the L-scheme + 0: {'wetting' :lambda_w, + 'nonwetting': lambda_nw},# +} + +## 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 +} + +## dictionary of relative permeabilties on all domains. +relative_permeability = {# + 0: subdomain1_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) + +_rel_perm1w_prime = ft.partial(rel_perm1w_prime) +_rel_perm1nw_prime = ft.partial(rel_perm1nw_prime) + +subdomain1_rel_perm_prime = { + 'wetting': _rel_perm1w_prime, + 'nonwetting': _rel_perm1nw_prime +} + +# dictionary of relative permeabilties on all domains. +ka_prime = { + 0: subdomain1_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))) + + +# def saturation(pc, index): +# # inverse capillary pressure-saturation-relationship +# return df.conditional(pc > 0, -index*pc, 1) +# +# +# def saturation_sym(pc, index): +# # inverse capillary pressure-saturation-relationship +# return -index*pc +# +# +# # 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 -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 = { + 0: ft.partial(saturation_sym, index=1), +} + +S_pc_sym_prime = { + 0: ft.partial(saturation_sym_prime, index=1), +} + +sat_pressure_relationship = { + 0: 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) + +epsilon_x_inner = 0.7 +epsilon_x_outer = 0.99 +epsilon_y_inner = epsilon_x_inner +epsilon_y_outer = epsilon_x_outer + +def mollifier(x, epsilon): + """ one d mollifier """ + out_expr = sym.exp(-1/(1-(x/epsilon)**2) + 1) + return out_expr + +mollifier_handle = ft.partial(mollifier, epsilon=epsilon_x_inner) + +pw_sym_x = sym.Piecewise( + (mollifier_handle(x), x**2 < epsilon_x_outer**2), + (0, True) +) +pw_sym_y = sym.Piecewise( + (mollifier_handle(y), y**2 < epsilon_y_outer**2), + (0, True) +) + +def mollifier2d(x, y, epsilon): + """ one d mollifier """ + out_expr = sym.exp(-1/(1-(x**2 + y**2)/epsilon**2) + 1) + return out_expr + +mollifier2d_handle = ft.partial(mollifier2d, epsilon=epsilon_x_outer) + +pw_sym2d_x = sym.Piecewise( + (mollifier2d_handle(x, y), x**2 + y**2 < epsilon_x_outer**2), + (0, True) +) + +zero_on_epsilon_shrinking_of_subdomain = sym.Piecewise( + (mollifier_handle(sym.sqrt(x**2 + y**2)+2*epsilon_x_inner), ((-2*epsilon_x_inner<sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<=epsilon_x_inner))), + (mollifier_handle(sym.sqrt(x**2 + y**2)-2*epsilon_x_inner), ((epsilon_x_inner<sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<2*epsilon_x_inner))), + (1, True), +) + +zero_on_epsilon_shrinking_of_subdomain_x = sym.Piecewise( + (mollifier_handle(x+2*epsilon_x_inner), ((-2*epsilon_x_inner<x) & (x<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=x) & (x<=epsilon_x_inner))), + (mollifier_handle(x-2*epsilon_x_inner), ((epsilon_x_inner<x) & (x<2*epsilon_x_inner))), + (1, True), +) + +zero_on_epsilon_shrinking_of_subdomain_y = sym.Piecewise( + (1, y<=-2*epsilon_x_inner), + (mollifier_handle(y+2*epsilon_x_inner), ((-2*epsilon_x_inner<y) & (y<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=y) & (y<=epsilon_x_inner))), + (mollifier_handle(y-2*epsilon_x_inner), ((epsilon_x_inner<y) & (y<2*epsilon_x_inner))), + (1, True), +) + +zero_on_shrinking = zero_on_epsilon_shrinking_of_subdomain #zero_on_epsilon_shrinking_of_subdomain_x + zero_on_epsilon_shrinking_of_subdomain_y +gaussian = pw_sym2d_x# pw_sym_y*pw_sym_x +cutoff = gaussian/(gaussian + zero_on_shrinking) + +# # construction of differentiable characteristic function. +# def smooth_characteristic_func_on_epsilon_shrinking_of_subdomain0(x, y, epsilon_x_inner, epsilon_y_inner, epsilon_x_outer, epsilon_y_outer): +# dist_to_complement_x = ft.partial(mollifier, epsilon=epsilon_x_inner) +# dist_to_complement_y = ft.partial(mollifier, epsilon=epsilon_y_inner) +# dist_to_complement = dist_to_complement_y(y)*dist_to_complement_x(x) +# dist_to_eps_shrinking_x = ft.partial(zero_outside_epsilon_thickening_of_subdomain, epsilon=epsilon_x_outer) +# dist_to_eps_shrinking_y = ft.partial(zero_outside_epsilon_thickening_of_subdomain, epsilon=epsilon_y_outer) +# dist_to_eps_shrinking = dist_to_eps_shrinking_y(y)*dist_to_eps_shrinking_x(x) +# return dist_to_complement/(dist_to_eps_shrinking + dist_to_complement) +# + +# def dist_to_epsilon_thickening_of_subdomain0_complement(x, y, epsilon): +# """ calculates the (euklidian distance)^2 of a point x,y to the epsilon +# thickening of the complement of the domain. +# """ +# is_inside = ((1-sym.Abs(x) > epsilon) & (1-sym.Abs(y) > epsilon)) +# sym.Piecewise((0, is_inside)) + +p_e_sym = { + 0: {'wetting': -3 +0.0*t, #*cutoff, + 'nonwetting': -1 +0.0*t}, #*cutoff}, +} + +pc_e_sym = dict() +for subdomain, isR in isRichards.items(): + if isR: + pc_e_sym.update({subdomain: -p_e_sym[subdomain]['wetting']}) + else: + 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() +# 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 +for starttime in starttimes: + for mesh_resolution, solver_tol in resolutions.items(): + # initialise LDD simulation class + simulation = ldd.LDDsimulation( + tol=1E-14, + LDDsolver_tol=solver_tol, + debug=debugflag, + max_iter_num=max_iter_num, + FEM_Lagrange_degree=FEM_Lagrange_degree, + mesh_study=mesh_study + ) + + simulation.set_parameters(use_case=use_case, + output_dir=output_string, + 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, + plot_timestep_every=plot_timestep_every, + timestep_size=timestep_size, + sources=source_expression, + initial_conditions=initial_condition, + dirichletBC_expression_strings=dirichletBC, + exact_solution=exact_solution, + densities=densities, + include_gravity=include_gravity, + write2file=write_to_file, + ) + + simulation.initialise() + output_dir = simulation.output_dir + # simulation.write_exact_solution_to_xdmf() + output = simulation.run(analyse_condition=analyse_condition) + for subdomain_index, subdomain_output in output.items(): + mesh_h = subdomain_output['mesh_size'] + for phase, different_errornorms in subdomain_output['errornorm'].items(): + filename = output_dir + "subdomain{}-space-time-errornorm-{}-phase.csv".format(subdomain_index, phase) + # for errortype, errornorm in different_errornorms.items(): + + # eocfile = open("eoc_filename", "a") + # eocfile.write( str(mesh_h) + " " + str(errornorm) + "\n" ) + # eocfile.close() + # if subdomain.isRichards:mesh_h + data_dict = { + 'mesh_parameter': mesh_resolution, + 'mesh_h': mesh_h, + } + for error_type, errornorms in different_errornorms.items(): + data_dict.update( + {error_type: errornorms} + ) + errors = pd.DataFrame(data_dict, index=[mesh_resolution]) + # check if file exists + if os.path.isfile(filename) == True: + with open(filename, 'a') as f: + errors.to_csv(f, header=False, sep='\t', encoding='utf-8', index=False) + else: + errors.to_csv(filename, sep='\t', encoding='utf-8', index=False) diff --git a/Two-phase-Two-phase/one-patch/mesh_study_for_fixed_timestep/TP-one-patch-mesh-study-fixed-timestep-nonwetting0.py b/Two-phase-Two-phase/one-patch/mesh_study_for_fixed_timestep/TP-one-patch-mesh-study-fixed-timestep-nonwetting0.py new file mode 100755 index 0000000000000000000000000000000000000000..f15efcf437c5a960dff1b9133ba6c4f36b30f844 --- /dev/null +++ b/Two-phase-Two-phase/one-patch/mesh_study_for_fixed_timestep/TP-one-patch-mesh-study-fixed-timestep-nonwetting0.py @@ -0,0 +1,491 @@ +#!/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 helpers as hlp +import datetime +import os +import pandas as pd + +date = datetime.datetime.now() +datestr = date.strftime("%Y-%m-%d") +#import ufl as ufl + +# init sympy session +sym.init_printing() + +use_case = "TP-one-patch-mesh-study-fixed-timestep-nonwetting0" +# solver_tol = 5E-9 +max_iter_num = 2000 +FEM_Lagrange_degree = 1 +mesh_study = True +# resolutions = {128: 1e-7} #[1,2,3,4,5,10,20,40,75,100] +resolutions = { + 1: 1e-10, + 2: 1e-10, + 4: 1e-10, + 8: 1e-10, + 16: 1e-10, + 32: 1e-10, + 64: 1e-10, + 128: 1e-10, + 256: 1e-10, + # 512: 1e-10, + } + +############ GRID ####################### +# mesh_resolution = 20 +timestep_size = 0.01 +number_of_timesteps = 1 +plot_timestep_every = 1 +# 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 = 1 +# starttimes = [0.0, 0.05, 0.1, 0.7, 1.3] +starttimes = [0.7] + +# starttimes = { +# 1: 0.0 +# 2: 0.05 +# 4: 0.1 +# 8: 0.2 +# 16: 0.4 +# 32: 0.7 +# 64: 1.0 +# 128: 1.3 +# } + +Lw = 0.05 #/timestep_size +Lnw=Lw + +lambda_w = 0 +lambda_nw = 0 + +include_gravity = False +debugflag = True +analyse_condition = False + +if mesh_study: + output_string = "./output/{}-{}_timesteps{}_P{}".format(datestr, use_case, number_of_timesteps, FEM_Lagrange_degree) +else: + for tol in resolutions.values(): + solver_tol = tol + output_string = "./output/{}-{}_timesteps{}_P{}_solver_tol{}".format(datestr, use_case, number_of_timesteps, FEM_Lagrange_degree, solver_tol) + +# toggle what should be written to files +if mesh_study: + write_to_file = { + 'space_errornorms': True, + 'meshes_and_markers': True, + 'L_iterations_per_timestep': True, + 'solutions': True, + 'absolute_differences': True, + 'condition_numbers': analyse_condition, + 'subsequent_errors': True + } +else: + write_to_file = { + 'space_errornorms': True, + 'meshes_and_markers': True, + 'L_iterations_per_timestep': False, + 'solutions': True, + 'absolute_differences': True, + 'condition_numbers': analyse_condition, + 'subsequent_errors': True + } + +##### 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)] + +subdomain0_outer_boundary_verts = { + 0: [sub_domain0_vertices[0], + sub_domain0_vertices[1], + sub_domain0_vertices[2], + sub_domain0_vertices[3], + sub_domain0_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] +# in the below list, index 0 corresponds to the 12 interface which has index 1 +interface_def_points = None + +# 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 + 0 : subdomain0_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 = None +isRichards = { + 0: False, # + } + +viscosity = {# +# subdom_num : viscosity + 0 : {'wetting' :1, + 'nonwetting': 1}, # +} + +porosity = {# +# subdom_num : porosity + 0: 1,# +} + +# Dict of the form: { subdom_num : density } +densities = { + 0: {'wetting': 1, #997, + 'nonwetting': 1}, #1225} +} + +gravity_acceleration = 9.81 + +L = {# +# subdom_num : subdomain L for L-scheme + 0: {'wetting' :Lw, + 'nonwetting': Lnw},# +} + +lambda_param = {# +# subdom_num : lambda parameter for the L-scheme + 0: {'wetting' :lambda_w, + 'nonwetting': lambda_nw},# +} + +## 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 +} + +## dictionary of relative permeabilties on all domains. +relative_permeability = {# + 0: subdomain1_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) + +_rel_perm1w_prime = ft.partial(rel_perm1w_prime) +_rel_perm1nw_prime = ft.partial(rel_perm1nw_prime) + +subdomain1_rel_perm_prime = { + 'wetting': _rel_perm1w_prime, + 'nonwetting': _rel_perm1nw_prime +} + +# dictionary of relative permeabilties on all domains. +ka_prime = { + 0: subdomain1_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))) + + +# def saturation(pc, index): +# # inverse capillary pressure-saturation-relationship +# return df.conditional(pc > 0, -index*pc, 1) +# +# +# def saturation_sym(pc, index): +# # inverse capillary pressure-saturation-relationship +# return -index*pc +# +# +# # 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 -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 = { + 0: ft.partial(saturation_sym, index=1), +} + +S_pc_sym_prime = { + 0: ft.partial(saturation_sym_prime, index=1), +} + +sat_pressure_relationship = { + 0: 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) + +epsilon_x_inner = 0.7 +epsilon_x_outer = 0.99 +epsilon_y_inner = epsilon_x_inner +epsilon_y_outer = epsilon_x_outer + +def mollifier(x, epsilon): + """ one d mollifier """ + out_expr = sym.exp(-1/(1-(x/epsilon)**2) + 1) + return out_expr + +mollifier_handle = ft.partial(mollifier, epsilon=epsilon_x_inner) + +pw_sym_x = sym.Piecewise( + (mollifier_handle(x), x**2 < epsilon_x_outer**2), + (0, True) +) +pw_sym_y = sym.Piecewise( + (mollifier_handle(y), y**2 < epsilon_y_outer**2), + (0, True) +) + +def mollifier2d(x, y, epsilon): + """ one d mollifier """ + out_expr = sym.exp(-1/(1-(x**2 + y**2)/epsilon**2) + 1) + return out_expr + +mollifier2d_handle = ft.partial(mollifier2d, epsilon=epsilon_x_outer) + +pw_sym2d_x = sym.Piecewise( + (mollifier2d_handle(x, y), x**2 + y**2 < epsilon_x_outer**2), + (0, True) +) + +zero_on_epsilon_shrinking_of_subdomain = sym.Piecewise( + (mollifier_handle(sym.sqrt(x**2 + y**2)+2*epsilon_x_inner), ((-2*epsilon_x_inner<sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<=epsilon_x_inner))), + (mollifier_handle(sym.sqrt(x**2 + y**2)-2*epsilon_x_inner), ((epsilon_x_inner<sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<2*epsilon_x_inner))), + (1, True), +) + +zero_on_epsilon_shrinking_of_subdomain_x = sym.Piecewise( + (mollifier_handle(x+2*epsilon_x_inner), ((-2*epsilon_x_inner<x) & (x<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=x) & (x<=epsilon_x_inner))), + (mollifier_handle(x-2*epsilon_x_inner), ((epsilon_x_inner<x) & (x<2*epsilon_x_inner))), + (1, True), +) + +zero_on_epsilon_shrinking_of_subdomain_y = sym.Piecewise( + (1, y<=-2*epsilon_x_inner), + (mollifier_handle(y+2*epsilon_x_inner), ((-2*epsilon_x_inner<y) & (y<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=y) & (y<=epsilon_x_inner))), + (mollifier_handle(y-2*epsilon_x_inner), ((epsilon_x_inner<y) & (y<2*epsilon_x_inner))), + (1, True), +) + +zero_on_shrinking = zero_on_epsilon_shrinking_of_subdomain #zero_on_epsilon_shrinking_of_subdomain_x + zero_on_epsilon_shrinking_of_subdomain_y +gaussian = pw_sym2d_x# pw_sym_y*pw_sym_x +cutoff = gaussian/(gaussian + zero_on_shrinking) + +# # construction of differentiable characteristic function. +# def smooth_characteristic_func_on_epsilon_shrinking_of_subdomain0(x, y, epsilon_x_inner, epsilon_y_inner, epsilon_x_outer, epsilon_y_outer): +# dist_to_complement_x = ft.partial(mollifier, epsilon=epsilon_x_inner) +# dist_to_complement_y = ft.partial(mollifier, epsilon=epsilon_y_inner) +# dist_to_complement = dist_to_complement_y(y)*dist_to_complement_x(x) +# dist_to_eps_shrinking_x = ft.partial(zero_outside_epsilon_thickening_of_subdomain, epsilon=epsilon_x_outer) +# dist_to_eps_shrinking_y = ft.partial(zero_outside_epsilon_thickening_of_subdomain, epsilon=epsilon_y_outer) +# dist_to_eps_shrinking = dist_to_eps_shrinking_y(y)*dist_to_eps_shrinking_x(x) +# return dist_to_complement/(dist_to_eps_shrinking + dist_to_complement) +# + +# def dist_to_epsilon_thickening_of_subdomain0_complement(x, y, epsilon): +# """ calculates the (euklidian distance)^2 of a point x,y to the epsilon +# thickening of the complement of the domain. +# """ +# is_inside = ((1-sym.Abs(x) > epsilon) & (1-sym.Abs(y) > epsilon)) +# sym.Piecewise((0, is_inside)) + +p_e_sym = { + 0: {'wetting': (-7 - (1+t*t)*(1 + x*x + y*y)), #*cutoff, + 'nonwetting': 0.0*t}, #*cutoff}, +} + +pc_e_sym = dict() +for subdomain, isR in isRichards.items(): + if isR: + pc_e_sym.update({subdomain: -p_e_sym[subdomain]['wetting']}) + else: + 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() +# 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 +for starttime in starttimes: + for mesh_resolution, solver_tol in resolutions.items(): + # initialise LDD simulation class + simulation = ldd.LDDsimulation( + tol=1E-14, + LDDsolver_tol=solver_tol, + debug=debugflag, + max_iter_num=max_iter_num, + FEM_Lagrange_degree=FEM_Lagrange_degree, + mesh_study=mesh_study + ) + + simulation.set_parameters(use_case=use_case, + output_dir=output_string, + 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, + plot_timestep_every=plot_timestep_every, + timestep_size=timestep_size, + sources=source_expression, + initial_conditions=initial_condition, + dirichletBC_expression_strings=dirichletBC, + exact_solution=exact_solution, + densities=densities, + include_gravity=include_gravity, + write2file=write_to_file, + ) + + simulation.initialise() + output_dir = simulation.output_dir + # simulation.write_exact_solution_to_xdmf() + output = simulation.run(analyse_condition=analyse_condition) + for subdomain_index, subdomain_output in output.items(): + mesh_h = subdomain_output['mesh_size'] + for phase, different_errornorms in subdomain_output['errornorm'].items(): + filename = output_dir + "subdomain{}-space-time-errornorm-{}-phase.csv".format(subdomain_index, phase) + # for errortype, errornorm in different_errornorms.items(): + + # eocfile = open("eoc_filename", "a") + # eocfile.write( str(mesh_h) + " " + str(errornorm) + "\n" ) + # eocfile.close() + # if subdomain.isRichards:mesh_h + data_dict = { + 'mesh_parameter': mesh_resolution, + 'mesh_h': mesh_h, + } + for error_type, errornorms in different_errornorms.items(): + data_dict.update( + {error_type: errornorms} + ) + errors = pd.DataFrame(data_dict, index=[mesh_resolution]) + # check if file exists + if os.path.isfile(filename) == True: + with open(filename, 'a') as f: + errors.to_csv(f, header=False, sep='\t', encoding='utf-8', index=False) + else: + errors.to_csv(filename, sep='\t', encoding='utf-8', index=False) diff --git a/Two-phase-Two-phase/one-patch/mesh_study_for_fixed_timestep/TP-one-patch-mesh-study-fixed-timestep-wetting0.py b/Two-phase-Two-phase/one-patch/mesh_study_for_fixed_timestep/TP-one-patch-mesh-study-fixed-timestep-wetting0.py new file mode 100755 index 0000000000000000000000000000000000000000..9821788e1557ed8c15233d9efe9b1941cb129e7b --- /dev/null +++ b/Two-phase-Two-phase/one-patch/mesh_study_for_fixed_timestep/TP-one-patch-mesh-study-fixed-timestep-wetting0.py @@ -0,0 +1,490 @@ +#!/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 helpers as hlp +import datetime +import os +import pandas as pd + +date = datetime.datetime.now() +datestr = date.strftime("%Y-%m-%d") +#import ufl as ufl + +# init sympy session +sym.init_printing() + +use_case = "TP-one-patch-mesh-study-fixed-timestep-wetting-constantexi" +# solver_tol = 5E-9 +max_iter_num = 2000 +FEM_Lagrange_degree = 1 +mesh_study = True +# resolutions = {128: 1e-7} #[1,2,3,4,5,10,20,40,75,100] +resolutions = { + 1: 1e-10, + 2: 1e-10, + 4: 1e-10, + 8: 1e-10, + 16: 1e-10, + 32: 1e-10, + 64: 1e-10, + 128: 1e-10, + 256: 1e-10, + 512: 1e-10, + } + +############ GRID ####################### +# mesh_resolution = 20 +timestep_size = 0.01 +number_of_timesteps = 1 +plot_timestep_every = 1 +# 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 = 1 +starttimes = [0.0, 0.05, 0.1, 0.7, 1.3] + +# starttimes = { +# 1: 0.0 +# 2: 0.05 +# 4: 0.1 +# 8: 0.2 +# 16: 0.4 +# 32: 0.7 +# 64: 1.0 +# 128: 1.3 +# } + +Lw = 0.05 #/timestep_size +Lnw=Lw + +lambda_w = 0 +lambda_nw = 0 + +include_gravity = False +debugflag = True +analyse_condition = False + +if mesh_study: + output_string = "./output/{}-{}_timesteps{}_P{}".format(datestr, use_case, number_of_timesteps, FEM_Lagrange_degree) +else: + for tol in resolutions.values(): + solver_tol = tol + output_string = "./output/{}-{}_timesteps{}_P{}_solver_tol{}".format(datestr, use_case, number_of_timesteps, FEM_Lagrange_degree, solver_tol) + +# toggle what should be written to files +if mesh_study: + write_to_file = { + 'space_errornorms': True, + 'meshes_and_markers': True, + 'L_iterations_per_timestep': True, + 'solutions': True, + 'absolute_differences': True, + 'condition_numbers': analyse_condition, + 'subsequent_errors': True + } +else: + write_to_file = { + 'space_errornorms': True, + 'meshes_and_markers': True, + 'L_iterations_per_timestep': False, + 'solutions': True, + 'absolute_differences': True, + 'condition_numbers': analyse_condition, + 'subsequent_errors': True + } + +##### 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)] + +subdomain0_outer_boundary_verts = { + 0: [sub_domain0_vertices[0], + sub_domain0_vertices[1], + sub_domain0_vertices[2], + sub_domain0_vertices[3], + sub_domain0_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] +# in the below list, index 0 corresponds to the 12 interface which has index 1 +interface_def_points = None + +# 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 + 0 : subdomain0_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 = None +isRichards = { + 0: False, # + } + +viscosity = {# +# subdom_num : viscosity + 0 : {'wetting' :1, + 'nonwetting': 1}, # +} + +porosity = {# +# subdom_num : porosity + 0: 1,# +} + +# Dict of the form: { subdom_num : density } +densities = { + 0: {'wetting': 1, #997, + 'nonwetting': 1}, #1225} +} + +gravity_acceleration = 9.81 + +L = {# +# subdom_num : subdomain L for L-scheme + 0: {'wetting' :Lw, + 'nonwetting': Lnw},# +} + +lambda_param = {# +# subdom_num : lambda parameter for the L-scheme + 0: {'wetting' :lambda_w, + 'nonwetting': lambda_nw},# +} + +## 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 +} + +## dictionary of relative permeabilties on all domains. +relative_permeability = {# + 0: subdomain1_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) + +_rel_perm1w_prime = ft.partial(rel_perm1w_prime) +_rel_perm1nw_prime = ft.partial(rel_perm1nw_prime) + +subdomain1_rel_perm_prime = { + 'wetting': _rel_perm1w_prime, + 'nonwetting': _rel_perm1nw_prime +} + +# dictionary of relative permeabilties on all domains. +ka_prime = { + 0: subdomain1_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))) + + +# def saturation(pc, index): +# # inverse capillary pressure-saturation-relationship +# return df.conditional(pc > 0, -index*pc, 1) +# +# +# def saturation_sym(pc, index): +# # inverse capillary pressure-saturation-relationship +# return -index*pc +# +# +# # 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 -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 = { + 0: ft.partial(saturation_sym, index=1), +} + +S_pc_sym_prime = { + 0: ft.partial(saturation_sym_prime, index=1), +} + +sat_pressure_relationship = { + 0: 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) + +epsilon_x_inner = 0.7 +epsilon_x_outer = 0.99 +epsilon_y_inner = epsilon_x_inner +epsilon_y_outer = epsilon_x_outer + +def mollifier(x, epsilon): + """ one d mollifier """ + out_expr = sym.exp(-1/(1-(x/epsilon)**2) + 1) + return out_expr + +mollifier_handle = ft.partial(mollifier, epsilon=epsilon_x_inner) + +pw_sym_x = sym.Piecewise( + (mollifier_handle(x), x**2 < epsilon_x_outer**2), + (0, True) +) +pw_sym_y = sym.Piecewise( + (mollifier_handle(y), y**2 < epsilon_y_outer**2), + (0, True) +) + +def mollifier2d(x, y, epsilon): + """ one d mollifier """ + out_expr = sym.exp(-1/(1-(x**2 + y**2)/epsilon**2) + 1) + return out_expr + +mollifier2d_handle = ft.partial(mollifier2d, epsilon=epsilon_x_outer) + +pw_sym2d_x = sym.Piecewise( + (mollifier2d_handle(x, y), x**2 + y**2 < epsilon_x_outer**2), + (0, True) +) + +zero_on_epsilon_shrinking_of_subdomain = sym.Piecewise( + (mollifier_handle(sym.sqrt(x**2 + y**2)+2*epsilon_x_inner), ((-2*epsilon_x_inner<sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<=epsilon_x_inner))), + (mollifier_handle(sym.sqrt(x**2 + y**2)-2*epsilon_x_inner), ((epsilon_x_inner<sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<2*epsilon_x_inner))), + (1, True), +) + +zero_on_epsilon_shrinking_of_subdomain_x = sym.Piecewise( + (mollifier_handle(x+2*epsilon_x_inner), ((-2*epsilon_x_inner<x) & (x<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=x) & (x<=epsilon_x_inner))), + (mollifier_handle(x-2*epsilon_x_inner), ((epsilon_x_inner<x) & (x<2*epsilon_x_inner))), + (1, True), +) + +zero_on_epsilon_shrinking_of_subdomain_y = sym.Piecewise( + (1, y<=-2*epsilon_x_inner), + (mollifier_handle(y+2*epsilon_x_inner), ((-2*epsilon_x_inner<y) & (y<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=y) & (y<=epsilon_x_inner))), + (mollifier_handle(y-2*epsilon_x_inner), ((epsilon_x_inner<y) & (y<2*epsilon_x_inner))), + (1, True), +) + +zero_on_shrinking = zero_on_epsilon_shrinking_of_subdomain #zero_on_epsilon_shrinking_of_subdomain_x + zero_on_epsilon_shrinking_of_subdomain_y +gaussian = pw_sym2d_x# pw_sym_y*pw_sym_x +cutoff = gaussian/(gaussian + zero_on_shrinking) + +# # construction of differentiable characteristic function. +# def smooth_characteristic_func_on_epsilon_shrinking_of_subdomain0(x, y, epsilon_x_inner, epsilon_y_inner, epsilon_x_outer, epsilon_y_outer): +# dist_to_complement_x = ft.partial(mollifier, epsilon=epsilon_x_inner) +# dist_to_complement_y = ft.partial(mollifier, epsilon=epsilon_y_inner) +# dist_to_complement = dist_to_complement_y(y)*dist_to_complement_x(x) +# dist_to_eps_shrinking_x = ft.partial(zero_outside_epsilon_thickening_of_subdomain, epsilon=epsilon_x_outer) +# dist_to_eps_shrinking_y = ft.partial(zero_outside_epsilon_thickening_of_subdomain, epsilon=epsilon_y_outer) +# dist_to_eps_shrinking = dist_to_eps_shrinking_y(y)*dist_to_eps_shrinking_x(x) +# return dist_to_complement/(dist_to_eps_shrinking + dist_to_complement) +# + +# def dist_to_epsilon_thickening_of_subdomain0_complement(x, y, epsilon): +# """ calculates the (euklidian distance)^2 of a point x,y to the epsilon +# thickening of the complement of the domain. +# """ +# is_inside = ((1-sym.Abs(x) > epsilon) & (1-sym.Abs(y) > epsilon)) +# sym.Piecewise((0, is_inside)) + +p_e_sym = { + 0: {'wetting': -10+0*t, #*cutoff, + 'nonwetting': (-1 -t*(1.1+y + x**2))}, #*cutoff}, +} + +pc_e_sym = dict() +for subdomain, isR in isRichards.items(): + if isR: + pc_e_sym.update({subdomain: -p_e_sym[subdomain]['wetting']}) + else: + 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() +# 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 +for starttime in starttimes: + for mesh_resolution, solver_tol in resolutions.items(): + # initialise LDD simulation class + simulation = ldd.LDDsimulation( + tol=1E-14, + LDDsolver_tol=solver_tol, + debug=debugflag, + max_iter_num=max_iter_num, + FEM_Lagrange_degree=FEM_Lagrange_degree, + mesh_study=mesh_study + ) + + simulation.set_parameters(use_case=use_case, + output_dir=output_string, + 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, + plot_timestep_every=plot_timestep_every, + timestep_size=timestep_size, + sources=source_expression, + initial_conditions=initial_condition, + dirichletBC_expression_strings=dirichletBC, + exact_solution=exact_solution, + densities=densities, + include_gravity=include_gravity, + write2file=write_to_file, + ) + + simulation.initialise() + output_dir = simulation.output_dir + # simulation.write_exact_solution_to_xdmf() + output = simulation.run(analyse_condition=analyse_condition) + for subdomain_index, subdomain_output in output.items(): + mesh_h = subdomain_output['mesh_size'] + for phase, different_errornorms in subdomain_output['errornorm'].items(): + filename = output_dir + "subdomain{}-space-time-errornorm-{}-phase.csv".format(subdomain_index, phase) + # for errortype, errornorm in different_errornorms.items(): + + # eocfile = open("eoc_filename", "a") + # eocfile.write( str(mesh_h) + " " + str(errornorm) + "\n" ) + # eocfile.close() + # if subdomain.isRichards:mesh_h + data_dict = { + 'mesh_parameter': mesh_resolution, + 'mesh_h': mesh_h, + } + for error_type, errornorms in different_errornorms.items(): + data_dict.update( + {error_type: errornorms} + ) + errors = pd.DataFrame(data_dict, index=[mesh_resolution]) + # check if file exists + if os.path.isfile(filename) == True: + with open(filename, 'a') as f: + errors.to_csv(f, header=False, sep='\t', encoding='utf-8', index=False) + else: + errors.to_csv(filename, sep='\t', encoding='utf-8', index=False) diff --git a/Two-phase-Two-phase/one-patch/mesh_study_for_fixed_timestep/TP-one-patch-mesh-study-fixed-timestep.py b/Two-phase-Two-phase/one-patch/mesh_study_for_fixed_timestep/TP-one-patch-mesh-study-fixed-timestep.py new file mode 100755 index 0000000000000000000000000000000000000000..e172f97fda09fce04ade6693b1ca2562fc370e44 --- /dev/null +++ b/Two-phase-Two-phase/one-patch/mesh_study_for_fixed_timestep/TP-one-patch-mesh-study-fixed-timestep.py @@ -0,0 +1,490 @@ +#!/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 helpers as hlp +import datetime +import os +import pandas as pd + +date = datetime.datetime.now() +datestr = date.strftime("%Y-%m-%d") +#import ufl as ufl + +# init sympy session +sym.init_printing() + +use_case = "TP-one-patch-mesh-study-fixed-timestep" +# solver_tol = 5E-9 +max_iter_num = 1000 +FEM_Lagrange_degree = 1 +mesh_study = True +# resolutions = {128: 1e-7} #[1,2,3,4,5,10,20,40,75,100] +resolutions = { + 1: 5e-7, + 2: 5e-7, + 4: 5e-7, + 8: 5e-7, + 16: 5e-7, + 32: 5e-7, + 64: 5e-7, + 128: 5e-7, + 256: 5e-7, + # 512: 1e-10, + } + +############ GRID ####################### +# mesh_resolution = 20 +timestep_size = 0.0025 +number_of_timesteps = 1 +plot_timestep_every = 1 +# 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 = 1 +starttimes = [0.0, 0.05, 0.1, 0.7, 1.3] + +# starttimes = { +# 1: 0.0 +# 2: 0.05 +# 4: 0.1 +# 8: 0.2 +# 16: 0.4 +# 32: 0.7 +# 64: 1.0 +# 128: 1.3 +# } + +Lw = 0.025 #/timestep_size +Lnw=Lw + +lambda_w = 0 +lambda_nw = 0 + +include_gravity = False +debugflag = False +analyse_condition = False + +if mesh_study: + output_string = "./output/{}-{}_timesteps{}_P{}".format(datestr, use_case, number_of_timesteps, FEM_Lagrange_degree) +else: + for tol in resolutions.values(): + solver_tol = tol + output_string = "./output/{}-{}_timesteps{}_P{}_solver_tol{}".format(datestr, use_case, number_of_timesteps, FEM_Lagrange_degree, solver_tol) + +# toggle what should be written to files +if mesh_study: + write_to_file = { + 'space_errornorms': True, + 'meshes_and_markers': True, + 'L_iterations_per_timestep': True, + 'solutions': True, + 'absolute_differences': True, + 'condition_numbers': analyse_condition, + 'subsequent_errors': True + } +else: + write_to_file = { + 'space_errornorms': True, + 'meshes_and_markers': True, + 'L_iterations_per_timestep': False, + 'solutions': True, + 'absolute_differences': True, + 'condition_numbers': analyse_condition, + 'subsequent_errors': True + } + +##### 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)] + +subdomain0_outer_boundary_verts = { + 0: [sub_domain0_vertices[0], + sub_domain0_vertices[1], + sub_domain0_vertices[2], + sub_domain0_vertices[3], + sub_domain0_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] +# in the below list, index 0 corresponds to the 12 interface which has index 1 +interface_def_points = None + +# 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 + 0 : subdomain0_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 = None +isRichards = { + 0: False, # + } + +viscosity = {# +# subdom_num : viscosity + 0 : {'wetting' :1, + 'nonwetting': 1}, # +} + +porosity = {# +# subdom_num : porosity + 0: 1,# +} + +# Dict of the form: { subdom_num : density } +densities = { + 0: {'wetting': 1, #997, + 'nonwetting': 1}, #1225} +} + +gravity_acceleration = 9.81 + +L = {# +# subdom_num : subdomain L for L-scheme + 0: {'wetting' :Lw, + 'nonwetting': Lnw},# +} + +lambda_param = {# +# subdom_num : lambda parameter for the L-scheme + 0: {'wetting' :lambda_w, + 'nonwetting': lambda_nw},# +} + +## 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 +} + +## dictionary of relative permeabilties on all domains. +relative_permeability = {# + 0: subdomain1_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) + +_rel_perm1w_prime = ft.partial(rel_perm1w_prime) +_rel_perm1nw_prime = ft.partial(rel_perm1nw_prime) + +subdomain1_rel_perm_prime = { + 'wetting': _rel_perm1w_prime, + 'nonwetting': _rel_perm1nw_prime +} + +# dictionary of relative permeabilties on all domains. +ka_prime = { + 0: subdomain1_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))) + + +# def saturation(pc, index): +# # inverse capillary pressure-saturation-relationship +# return df.conditional(pc > 0, -index*pc, 1) +# +# +# def saturation_sym(pc, index): +# # inverse capillary pressure-saturation-relationship +# return -index*pc +# +# +# # 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 -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 = { + 0: ft.partial(saturation_sym, index=1), +} + +S_pc_sym_prime = { + 0: ft.partial(saturation_sym_prime, index=1), +} + +sat_pressure_relationship = { + 0: 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) + +epsilon_x_inner = 0.7 +epsilon_x_outer = 0.99 +epsilon_y_inner = epsilon_x_inner +epsilon_y_outer = epsilon_x_outer + +def mollifier(x, epsilon): + """ one d mollifier """ + out_expr = sym.exp(-1/(1-(x/epsilon)**2) + 1) + return out_expr + +mollifier_handle = ft.partial(mollifier, epsilon=epsilon_x_inner) + +pw_sym_x = sym.Piecewise( + (mollifier_handle(x), x**2 < epsilon_x_outer**2), + (0, True) +) +pw_sym_y = sym.Piecewise( + (mollifier_handle(y), y**2 < epsilon_y_outer**2), + (0, True) +) + +def mollifier2d(x, y, epsilon): + """ one d mollifier """ + out_expr = sym.exp(-1/(1-(x**2 + y**2)/epsilon**2) + 1) + return out_expr + +mollifier2d_handle = ft.partial(mollifier2d, epsilon=epsilon_x_outer) + +pw_sym2d_x = sym.Piecewise( + (mollifier2d_handle(x, y), x**2 + y**2 < epsilon_x_outer**2), + (0, True) +) + +zero_on_epsilon_shrinking_of_subdomain = sym.Piecewise( + (mollifier_handle(sym.sqrt(x**2 + y**2)+2*epsilon_x_inner), ((-2*epsilon_x_inner<sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<=epsilon_x_inner))), + (mollifier_handle(sym.sqrt(x**2 + y**2)-2*epsilon_x_inner), ((epsilon_x_inner<sym.sqrt(x**2 + y**2)) & (sym.sqrt(x**2 + y**2)<2*epsilon_x_inner))), + (1, True), +) + +zero_on_epsilon_shrinking_of_subdomain_x = sym.Piecewise( + (mollifier_handle(x+2*epsilon_x_inner), ((-2*epsilon_x_inner<x) & (x<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=x) & (x<=epsilon_x_inner))), + (mollifier_handle(x-2*epsilon_x_inner), ((epsilon_x_inner<x) & (x<2*epsilon_x_inner))), + (1, True), +) + +zero_on_epsilon_shrinking_of_subdomain_y = sym.Piecewise( + (1, y<=-2*epsilon_x_inner), + (mollifier_handle(y+2*epsilon_x_inner), ((-2*epsilon_x_inner<y) & (y<-epsilon_x_inner))), + (0, ((-epsilon_x_inner<=y) & (y<=epsilon_x_inner))), + (mollifier_handle(y-2*epsilon_x_inner), ((epsilon_x_inner<y) & (y<2*epsilon_x_inner))), + (1, True), +) + +zero_on_shrinking = zero_on_epsilon_shrinking_of_subdomain #zero_on_epsilon_shrinking_of_subdomain_x + zero_on_epsilon_shrinking_of_subdomain_y +gaussian = pw_sym2d_x# pw_sym_y*pw_sym_x +cutoff = gaussian/(gaussian + zero_on_shrinking) + +# # construction of differentiable characteristic function. +# def smooth_characteristic_func_on_epsilon_shrinking_of_subdomain0(x, y, epsilon_x_inner, epsilon_y_inner, epsilon_x_outer, epsilon_y_outer): +# dist_to_complement_x = ft.partial(mollifier, epsilon=epsilon_x_inner) +# dist_to_complement_y = ft.partial(mollifier, epsilon=epsilon_y_inner) +# dist_to_complement = dist_to_complement_y(y)*dist_to_complement_x(x) +# dist_to_eps_shrinking_x = ft.partial(zero_outside_epsilon_thickening_of_subdomain, epsilon=epsilon_x_outer) +# dist_to_eps_shrinking_y = ft.partial(zero_outside_epsilon_thickening_of_subdomain, epsilon=epsilon_y_outer) +# dist_to_eps_shrinking = dist_to_eps_shrinking_y(y)*dist_to_eps_shrinking_x(x) +# return dist_to_complement/(dist_to_eps_shrinking + dist_to_complement) +# + +# def dist_to_epsilon_thickening_of_subdomain0_complement(x, y, epsilon): +# """ calculates the (euklidian distance)^2 of a point x,y to the epsilon +# thickening of the complement of the domain. +# """ +# is_inside = ((1-sym.Abs(x) > epsilon) & (1-sym.Abs(y) > epsilon)) +# sym.Piecewise((0, is_inside)) + +p_e_sym = { + 0: {'wetting': (-7 - (1+t*t)*(1 + x*x + y*y)), #*cutoff, + 'nonwetting': (-1 -t*(1.1+y + x**2))}, #*cutoff}, +} + +pc_e_sym = dict() +for subdomain, isR in isRichards.items(): + if isR: + pc_e_sym.update({subdomain: -p_e_sym[subdomain]['wetting']}) + else: + 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() +# 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 +for starttime in starttimes: + for mesh_resolution, solver_tol in resolutions.items(): + # initialise LDD simulation class + simulation = ldd.LDDsimulation( + tol=1E-14, + LDDsolver_tol=solver_tol, + debug=debugflag, + max_iter_num=max_iter_num, + FEM_Lagrange_degree=FEM_Lagrange_degree, + mesh_study=mesh_study + ) + + simulation.set_parameters(use_case=use_case, + output_dir=output_string, + 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, + plot_timestep_every=plot_timestep_every, + timestep_size=timestep_size, + sources=source_expression, + initial_conditions=initial_condition, + dirichletBC_expression_strings=dirichletBC, + exact_solution=exact_solution, + densities=densities, + include_gravity=include_gravity, + write2file=write_to_file, + ) + + simulation.initialise() + output_dir = simulation.output_dir + # simulation.write_exact_solution_to_xdmf() + output = simulation.run(analyse_condition=analyse_condition) + for subdomain_index, subdomain_output in output.items(): + mesh_h = subdomain_output['mesh_size'] + for phase, different_errornorms in subdomain_output['errornorm'].items(): + filename = output_dir + "subdomain{}-space-time-errornorm-{}-phase.csv".format(subdomain_index, phase) + # for errortype, errornorm in different_errornorms.items(): + + # eocfile = open("eoc_filename", "a") + # eocfile.write( str(mesh_h) + " " + str(errornorm) + "\n" ) + # eocfile.close() + # if subdomain.isRichards:mesh_h + data_dict = { + 'mesh_parameter': mesh_resolution, + 'mesh_h': mesh_h, + } + for error_type, errornorms in different_errornorms.items(): + data_dict.update( + {error_type: errornorms} + ) + errors = pd.DataFrame(data_dict, index=[mesh_resolution]) + # check if file exists + if os.path.isfile(filename) == True: + with open(filename, 'a') as f: + errors.to_csv(f, header=False, sep='\t', encoding='utf-8', index=False) + else: + errors.to_csv(filename, sep='\t', encoding='utf-8', index=False) diff --git a/Two-phase-Two-phase/one-patch/mesh_study_for_fixed_timestep/run-simulation b/Two-phase-Two-phase/one-patch/mesh_study_for_fixed_timestep/run-simulation new file mode 100755 index 0000000000000000000000000000000000000000..0eb497502a082a0fec07a5449b1fe946d59c8cc7 --- /dev/null +++ b/Two-phase-Two-phase/one-patch/mesh_study_for_fixed_timestep/run-simulation @@ -0,0 +1,16 @@ +#!/bin/bash + +[ $# -eq 0 ] && { echo "Usage: $0 simulation_file [logfile_name]"; exit 1; } + +SIMULATION_FILE=$1 +SIMULATION=${SIMULATION_FILE%.py} +LOGFILE_DEFAULT="$SIMULATION.log" + +DATE=$(date -I) +LOGFILE=${2:-$DATE-$LOGFILE_DEFAULT} + +GREETING="Simulation $SIMULATION is run on $DATE by $USER" + +echo $GREETING +echo "running $SIMULATION_FILE | tee $LOGFILE" +./$SIMULATION_FILE | tee $LOGFILE diff --git a/Two-phase-Two-phase/one-patch/run-simulation b/Two-phase-Two-phase/one-patch/run-simulation new file mode 100755 index 0000000000000000000000000000000000000000..0eb497502a082a0fec07a5449b1fe946d59c8cc7 --- /dev/null +++ b/Two-phase-Two-phase/one-patch/run-simulation @@ -0,0 +1,16 @@ +#!/bin/bash + +[ $# -eq 0 ] && { echo "Usage: $0 simulation_file [logfile_name]"; exit 1; } + +SIMULATION_FILE=$1 +SIMULATION=${SIMULATION_FILE%.py} +LOGFILE_DEFAULT="$SIMULATION.log" + +DATE=$(date -I) +LOGFILE=${2:-$DATE-$LOGFILE_DEFAULT} + +GREETING="Simulation $SIMULATION is run on $DATE by $USER" + +echo $GREETING +echo "running $SIMULATION_FILE | tee $LOGFILE" +./$SIMULATION_FILE | tee $LOGFILE diff --git a/Two-phase-Two-phase/two-patch/archive/TP-TP-patch-test-case-sinus_solution/TP-TP-2-patch-test_sinus.py b/Two-phase-Two-phase/two-patch/archive/TP-TP-patch-test-case-sinus_solution/TP-TP-2-patch-test_sinus.py new file mode 100755 index 0000000000000000000000000000000000000000..500b636c624226cd86928fa4f13e2856e2955a68 --- /dev/null +++ b/Two-phase-Two-phase/two-patch/archive/TP-TP-patch-test-case-sinus_solution/TP-TP-2-patch-test_sinus.py @@ -0,0 +1,350 @@ +#!/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(0.0,0.0), # + df.Point(1.0,0.0),# + df.Point(1.0,1.0),# + df.Point(0.0,1.0)] +# interface between subdomain1 and subdomain2 +interface12_vertices = [df.Point(0.0, 0.5), + df.Point(1.0, 0.5) ] +# subdomain1. +sub_domain1_vertices = [interface12_vertices[0], + interface12_vertices[1], + df.Point(1.0,1.0), + 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 internal, the +# dictionary entry should be 0: None +subdomain1_outer_boundary_verts = { + 0: [interface12_vertices[0], # + df.Point(0.0,1.0), # + df.Point(1.0,1.0), # + interface12_vertices[1]] +} +# subdomain2 +sub_domain2_vertices = [df.Point(0.0,0.0), + df.Point(1.0,0.0), + interface12_vertices[1], + interface12_vertices[0] ] + +subdomain2_outer_boundary_verts = { + 0: [interface12_vertices[1], # + df.Point(1.0,0.0), # + df.Point(0.0,0.0), # + interface12_vertices[0]] +} +# 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 = 35 +timestep_size = 1*0.0001 +number_of_timesteps = 500 +# 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/50}, # + 2 : {'wetting' :1, + 'nonwetting': 1/50} +} + +porosity = {# +# subdom_num : porosity + 1 : 1,# + 2 : 1 +} + +L = {# +# subdom_num : subdomain L for L-scheme + 1 : {'wetting' :0.6, + 'nonwetting': 0.6},# + 2 : {'wetting' :0.6, + 'nonwetting': 0.6} +} + +lambda_param = {# +# subdom_num : lambda parameter for the L-scheme + 1 : {'wetting' :600, + 'nonwetting': 1500},# + 2 : {'wetting' :600, + 'nonwetting': 1500} +} + +## 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 subdomain2 + 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 +} + +# 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(capillary_pressure, n_index, alpha): + # inverse capillary pressure-saturation-relationship + return df.conditional(capillary_pressure > 0, 1/((1 + (alpha*capillary_pressure)**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(capillary_pressure, n_index, alpha): + # inverse capillary pressure-saturation-relationship + #df.conditional(capillary_pressure > 0, + return 1/((1 + (alpha*capillary_pressure)**n_index)**((n_index - 1)/n_index)) + +S_pc_rel = {# + 1: ft.partial(saturation_sym, n_index = 6, alpha=0.001),# n= 3 stands for non-uniform porous media + 2: ft.partial(saturation_sym, n_index = 6, alpha=0.001) # n=6 stands for uniform porous media matrix (siehe Helmig) +} + +S_pc_rel_sym = {# + 1: ft.partial(saturation_sym, n_index = sym.Symbol('n'), alpha = sym.Symbol('a')),# n= 3 stands for non-uniform porous media + 2: ft.partial(saturation_sym, n_index = sym.Symbol('n'), alpha = sym.Symbol('a')) # n=6 stands for uniform porous media matrix (siehe Helmig) +} + +#### 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 = -4 - 3*sym.sin(-2*t+2*x)*sym.sin(1/2*y-1.2*t) +p1_nw = -10 - 6*sym.sin(x-1*t)*sym.sin(-0.5*t + y) + +#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(S_pc_rel[1](p1_nw - p1_w), t, 1) +dtS1_nw = -porosity[1]*sym.diff(S_pc_rel[1](p1_nw - p1_w), 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'](S_pc_rel[1](p1_nw - p1_w))*sym.diff(p1_w, x, 1), x, 1) +dydyflux1_w = -1/viscosity[1]['wetting']*sym.diff(relative_permeability[1]['wetting'](S_pc_rel[1](p1_nw - p1_w))*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-S_pc_rel[1](p1_nw - p1_w))*sym.diff(p1_nw, x, 1), x, 1) +dydyflux1_nw = -1/viscosity[1]['nonwetting']*sym.diff(relative_permeability[1]['nonwetting'](1-S_pc_rel[1](p1_nw - p1_w))*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 = -4 - 3*sym.sin(-2*t+2*x)*sym.sin(1/2*y-1.2*t) +p2_nw = -10 - 6*sym.sin(x-1*t)*sym.sin(-0.5*t + y) + + + +#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(S_pc_rel[2](p2_nw - p2_w), t, 1) +dtS2_nw = -porosity[2]*sym.diff(S_pc_rel[2](p2_nw - p2_w), 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'](S_pc_rel[2](p2_nw - p2_w))*sym.diff(p2_w, x, 1), x, 1) +dydyflux2_w = -1/viscosity[2]['wetting']*sym.diff(relative_permeability[2]['wetting'](S_pc_rel[2](p2_nw - p2_w))*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-S_pc_rel[2](p2_nw - p2_w))*sym.diff(p2_nw, x, 1), x, 1) +dydyflux2_nw = -1/viscosity[2]['nonwetting']*sym.diff(relative_permeability[2]['nonwetting'](1-S_pc_rel[2](p2_nw - p2_w))*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)}} +} + +# 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, debug = False, 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 = S_pc_rel,# + 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,# + write2file = write_to_file,# + ) + +simulation.initialise() +# simulation.write_exact_solution_to_xdmf() +simulation.run() diff --git a/Two-phase-Two-phase/two-patch/archive/TP-TP-patch-test-case-sinus_solution/startup.sh b/Two-phase-Two-phase/two-patch/archive/TP-TP-patch-test-case-sinus_solution/startup.sh new file mode 120000 index 0000000000000000000000000000000000000000..e845a35044d6c2295b0bbf425f94f815da87e858 --- /dev/null +++ b/Two-phase-Two-phase/two-patch/archive/TP-TP-patch-test-case-sinus_solution/startup.sh @@ -0,0 +1 @@ +../Jupyter_Notebook_Setup/startup.sh \ No newline at end of file diff --git a/Two-phase-Two-phase/two-patch/archive/TP-TP-patch-test-case/startup.sh b/Two-phase-Two-phase/two-patch/archive/TP-TP-patch-test-case/startup.sh new file mode 100755 index 0000000000000000000000000000000000000000..2dce5dcce065f5cb67d57d34a01d88b59d6abcf5 --- /dev/null +++ b/Two-phase-Two-phase/two-patch/archive/TP-TP-patch-test-case/startup.sh @@ -0,0 +1,3 @@ +#!/bin/bash +source ../.env/bin/activate +jupyter notebook diff --git a/Two-phase-Two-phase/two-patch/archive/TP-TP-patch-test-case_sanitycheck/TP-TP-2-patch-test_sanity-check.py b/Two-phase-Two-phase/two-patch/archive/TP-TP-patch-test-case_sanitycheck/TP-TP-2-patch-test_sanity-check.py new file mode 100755 index 0000000000000000000000000000000000000000..b6d89ef6dc9de492689348456f299e321ab5b9f2 --- /dev/null +++ b/Two-phase-Two-phase/two-patch/archive/TP-TP-patch-test-case_sanitycheck/TP-TP-2-patch-test_sanity-check.py @@ -0,0 +1,350 @@ +#!/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(0.0,0.0), # + df.Point(1.0,0.0),# + df.Point(1.0,1.0),# + df.Point(0.0,1.0)] +# interface between subdomain1 and subdomain2 +interface12_vertices = [df.Point(0.0, 0.5), + df.Point(1.0, 0.5) ] +# subdomain1. +sub_domain1_vertices = [interface12_vertices[0], + interface12_vertices[1], + df.Point(1.0,1.0), + 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 internal, the +# dictionary entry should be 0: None +subdomain1_outer_boundary_verts = { + 0: [interface12_vertices[0], # + df.Point(0.0,1.0), # + df.Point(1.0,1.0), # + interface12_vertices[1]] +} +# subdomain2 +sub_domain2_vertices = [df.Point(0.0,0.0), + df.Point(1.0,0.0), + interface12_vertices[1], + interface12_vertices[0] ] + +subdomain2_outer_boundary_verts = { + 0: [interface12_vertices[1], # + df.Point(1.0,0.0), # + df.Point(0.0,0.0), # + interface12_vertices[0]] +} +# 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 = 20 +timestep_size = 1*0.001 +number_of_timesteps = 20 +# 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/50}, # + 2 : {'wetting' :1, + 'nonwetting': 1/50} +} + +porosity = {# +# subdom_num : porosity + 1 : 1,# + 2 : 1 +} + +L = {# +# subdom_num : subdomain L for L-scheme + 1 : {'wetting' :0.25, + 'nonwetting': 0.25},# + 2 : {'wetting' :0.25, + 'nonwetting': 0.25} +} + +lambda_param = {# +# subdom_num : lambda parameter for the L-scheme + 1 : {'wetting' :140, + 'nonwetting': 2400},# + 2 : {'wetting' :140, + 'nonwetting': 2400} +} + +## 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 subdomain2 + 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 +} + +# 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(capillary_pressure, n_index, alpha): + # inverse capillary pressure-saturation-relationship + return df.conditional(capillary_pressure > 0, 1/((1 + (alpha*capillary_pressure)**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(capillary_pressure, n_index, alpha): + # inverse capillary pressure-saturation-relationship + #df.conditional(capillary_pressure > 0, + return 1/((1 + (alpha*capillary_pressure)**n_index)**((n_index - 1)/n_index)) + +S_pc_rel = {# + 1: ft.partial(saturation_sym, n_index = 3, alpha=0.001),# n= 3 stands for non-uniform porous media + 2: ft.partial(saturation_sym, n_index = 3, alpha=0.001) # n=6 stands for uniform porous media matrix (siehe Helmig) +} + +S_pc_rel_sym = {# + 1: ft.partial(saturation_sym, n_index = sym.Symbol('n'), alpha = sym.Symbol('a')),# n= 3 stands for non-uniform porous media + 2: ft.partial(saturation_sym, n_index = sym.Symbol('n'), alpha = sym.Symbol('a')) # n=6 stands for uniform porous media matrix (siehe Helmig) +} + +#### 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 = -20 - (1+t*t)*(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)**2 + x**2) + + +#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(S_pc_rel[1](p1_nw - p1_w), t, 1) +dtS1_nw = -porosity[1]*sym.diff(S_pc_rel[1](p1_nw - p1_w), 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'](S_pc_rel[1](p1_nw - p1_w))*sym.diff(p1_w, x, 1), x, 1) +dydyflux1_w = -1/viscosity[1]['wetting']*sym.diff(relative_permeability[1]['wetting'](S_pc_rel[1](p1_nw - p1_w))*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-S_pc_rel[1](p1_nw - p1_w))*sym.diff(p1_nw, x, 1), x, 1) +dydyflux1_nw = -1/viscosity[1]['nonwetting']*sym.diff(relative_permeability[1]['nonwetting'](1-S_pc_rel[1](p1_nw - p1_w))*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 = -20 - (1+t*t)*(1 + x**2) +p2_nw = -t*(1 + x**2)**2 - sym.sqrt(2+t**2)*(1 + x**2) + + +#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(S_pc_rel[2](p2_nw - p2_w), t, 1) +dtS2_nw = -porosity[2]*sym.diff(S_pc_rel[2](p2_nw - p2_w), 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'](S_pc_rel[2](p2_nw - p2_w))*sym.diff(p2_w, x, 1), x, 1) +dydyflux2_w = -1/viscosity[2]['wetting']*sym.diff(relative_permeability[2]['wetting'](S_pc_rel[2](p2_nw - p2_w))*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-S_pc_rel[2](p2_nw - p2_w))*sym.diff(p2_nw, x, 1), x, 1) +dydyflux2_nw = -1/viscosity[2]['nonwetting']*sym.diff(relative_permeability[2]['nonwetting'](1-S_pc_rel[2](p2_nw - p2_w))*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)}} +} + +# 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) +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 = S_pc_rel,# + 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,# + write2file = write_to_file,# + ) + +simulation.initialise() +# simulation.write_exact_solution_to_xdmf() +simulation.run() diff --git a/Two-phase-Two-phase/two-patch/archive/TP-TP-patch-test-case_sanitycheck/startup.sh b/Two-phase-Two-phase/two-patch/archive/TP-TP-patch-test-case_sanitycheck/startup.sh new file mode 120000 index 0000000000000000000000000000000000000000..e845a35044d6c2295b0bbf425f94f815da87e858 --- /dev/null +++ b/Two-phase-Two-phase/two-patch/archive/TP-TP-patch-test-case_sanitycheck/startup.sh @@ -0,0 +1 @@ +../Jupyter_Notebook_Setup/startup.sh \ No newline at end of file