-
Notifications
You must be signed in to change notification settings - Fork 7
/
solvers.py
196 lines (145 loc) · 5.9 KB
/
solvers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import logging, time, sys
import scipy.sparse as sps
import numpy as np
import porepy as pp
import data
# ------------------------------------------------------------------------------#
def setup_custom_logger():
formatter = logging.Formatter(
fmt="%(asctime)s %(levelname)-8s %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
)
handler = logging.FileHandler("log.txt", mode="w")
handler.setFormatter(formatter)
screen_handler = logging.StreamHandler(stream=sys.stdout)
screen_handler.setFormatter(formatter)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
logger.addHandler(screen_handler)
return logger
logger = setup_custom_logger()
# ------------------------------------------------------------------------------#
def flow(gb, param):
model = "flow"
model_data = data.flow(gb, model, param)
# discretization operator name
flux_id = "flux"
# master variable name
variable = "flux_pressure"
mortar = "lambda_" + variable
# post process variables
pressure = "pressure"
flux = "darcy_flux" # it has to be this one
P0_flux = "P0_flux"
# save variable name for the advection-diffusion problem
param["pressure"] = pressure
param["flux"] = flux
param["P0_flux"] = P0_flux
param["mortar_flux"] = mortar
# discretization operators
discr = pp.MVEM(model_data)
coupling = pp.RobinCoupling(model_data, discr)
# define the dof and discretization for the grids
for g, d in gb:
d[pp.PRIMARY_VARIABLES] = {variable: {"cells": 1, "faces": 1}}
d[pp.DISCRETIZATION] = {variable: {flux_id: discr}}
# define the interface terms to couple the grids
for e, d in gb.edges():
g_slave, g_master = gb.nodes_of_edge(e)
d[pp.PRIMARY_VARIABLES] = {mortar: {"cells": 1}}
d[pp.COUPLING_DISCRETIZATION] = {
variable: {
g_slave: (variable, flux_id),
g_master: (variable, flux_id),
e: (mortar, coupling),
}
}
# solution of the darcy problem
assembler = pp.Assembler()
logger.info("Assemble the flow problem")
A, b, block_dof, full_dof = assembler.assemble_matrix_rhs(gb)
logger.info("done")
logger.info("Solve the linear system")
up = sps.linalg.spsolve(A, b)
logger.info("done")
logger.info("Variable post-process")
assembler.distribute_variable(gb, up, block_dof, full_dof)
for g, d in gb:
d[pressure] = discr.extract_pressure(g, d[variable])
d[flux] = discr.extract_flux(g, d[variable])
pp.project_flux(gb, discr, flux, P0_flux, mortar)
logger.info("done")
return model_data
# ------------------------------------------------------------------------------#
def advdiff(gb, param, model_flow):
model = "transport"
model_data_adv, model_data_diff = data.advdiff(gb, model, model_flow, param)
# discretization operator names
adv_id = "advection"
diff_id = "diffusion"
# variable names
variable = "scalar"
mortar_adv = "lambda_" + variable + "_" + adv_id
mortar_diff = "lambda_" + variable + "_" + diff_id
# discretization operatr
discr_adv = pp.Upwind(model_data_adv)
discr_diff = pp.Tpfa(model_data_diff)
coupling_adv = pp.UpwindCoupling(model_data_adv)
coupling_diff = pp.RobinCoupling(model_data_diff, discr_diff)
for g, d in gb:
d[pp.PRIMARY_VARIABLES] = {variable: {"cells": 1}}
d[pp.DISCRETIZATION] = {variable: {adv_id: discr_adv, diff_id: discr_diff}}
for e, d in gb.edges():
g_slave, g_master = gb.nodes_of_edge(e)
d[pp.PRIMARY_VARIABLES] = {mortar_adv: {"cells": 1}, mortar_diff: {"cells": 1}}
d[pp.COUPLING_DISCRETIZATION] = {
adv_id: {
g_slave: (variable, adv_id),
g_master: (variable, adv_id),
e: (mortar_adv, coupling_adv),
},
diff_id: {
g_slave: (variable, diff_id),
g_master: (variable, diff_id),
e: (mortar_diff, coupling_diff),
},
}
# setup the advection-diffusion problem
assembler = pp.Assembler()
logger.info("Assemble the advective and diffusive terms of the transport problem")
A, b, block_dof, full_dof = assembler.assemble_matrix_rhs(gb)
logger.info("done")
# mass term
mass_id = "mass"
discr_mass = pp.MassMatrix(model_data_adv)
for g, d in gb:
d[pp.PRIMARY_VARIABLES] = {variable: {"cells": 1}}
d[pp.DISCRETIZATION] = {variable: {mass_id: discr_mass}}
gb.remove_edge_props(pp.COUPLING_DISCRETIZATION)
for e, d in gb.edges():
g_slave, g_master = gb.nodes_of_edge(e)
d[pp.PRIMARY_VARIABLES] = {mortar_adv: {"cells": 1}, mortar_diff: {"cells": 1}}
logger.info("Assemble the mass term of the transport problem")
M, _, _, _ = assembler.assemble_matrix_rhs(gb)
logger.info("done")
# Perform an LU factorization to speedup the solver
# IE_solver = sps.linalg.factorized((M + A).tocsc())
# time loop
logger.info("Prepare the exporting")
save = pp.Exporter(gb, "solution", folder=param["folder"])
logger.info("done")
variables = [variable, param["pressure"], param["P0_flux"]]
x = np.ones(A.shape[0]) * param["initial_advdiff"]
logger.info("Start the time loop with " + str(param["n_steps"]) + " steps")
for i in np.arange(param["n_steps"]):
# x = IE_solver(b + M.dot(x))
logger.info("Solve the linear system for time step " + str(i))
x = sps.linalg.spsolve(M + A, b + M.dot(x))
logger.info("done")
logger.info("Variable post-process")
assembler.distribute_variable(gb, x, block_dof, full_dof)
logger.info("done")
logger.info("Export variable")
save.write_vtk(variables, time_step=i)
logger.info("done")
save.write_pvd(np.arange(param["n_steps"]) * param["time_step"])