Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions UM2N/generator/burgers_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import movement as mv
import numpy as np # noqa

from firedrake.__future__ import interpolate

__all__ = ["BurgersSolver"]


Expand Down Expand Up @@ -198,12 +200,15 @@ def solve_problem(self, callback=None):
# solve on fine mesh
fd.solve(self.F_fine == 0, self.u_fine)
start = time.perf_counter()
adapter = mv.MongeAmpereMover(
adaptor = mv.MongeAmpereMover(
self.mesh,
monitor_function=self.monitor_function,
rtol=1e-3,
)
adapter.move()

raw_monitor_val = self.monitor_function(self.mesh)

adaptor.move()
end = time.perf_counter()
dur_ms = (end - start) * 1000

Expand All @@ -217,6 +222,9 @@ def solve_problem(self, callback=None):
uh_0 = fd.Function(function_space)
uh_0.project(self.u[0])

monitor_val = fd.Function(function_space)
monitor_val.assign(raw_monitor_val)

# calculate solution on adapted mesh
self.mesh.coordinates.dat.data[:] = self.adapt_coord
self.project_u_()
Expand All @@ -240,27 +248,30 @@ def solve_problem(self, callback=None):

func_vec_space = fd.VectorFunctionSpace(self.mesh, "CG", 1)
uh_grad = fd.interpolate(fd.grad(uh_0), func_vec_space)

grad_uh_interpolate = fd.assemble(
interpolate(fd.grad(self.u[0]), func_vec_space)
)
grad_norm = fd.Function(function_space)
grad_norm.project(grad_uh_interpolate[0] ** 2 + grad_uh_interpolate[1] ** 2)
grad_norm /= grad_norm.vector().max()

hessian_norm = self.f_norm
hessian = self.l2_projection
phi = adapter.phi
phi_grad = adapter.grad_phi
sigma = adapter.sigma
phi = adaptor.phi
phi_grad = adaptor.grad_phi
sigma = adaptor.H
I = fd.Identity(2) # noqa
jacobian = I + sigma
jacobian_det = fd.Function(function_space, name="jacobian_det")
jacobian_det.project(
self.jacob_det = fd.Function(adaptor.P1, name="jacobian_det").project(
jacobian[0, 0] * jacobian[1, 1] - jacobian[0, 1] * jacobian[1, 0]
)
self.jacob_det = fd.project(
jacobian_det, fd.FunctionSpace(self.mesh, "CG", 1)
)
self.jacob = fd.project(
jacobian, fd.TensorFunctionSpace(self.mesh, "CG", 1)
)
self.jacob = fd.Function(adaptor.P1_ten, name="jacobian").project(jacobian)

callback(
uh=uh_0,
uh_grad=uh_grad,
grad_norm=grad_norm,
hessian_norm=hessian_norm,
hessian=hessian,
phi=phi,
Expand All @@ -280,6 +291,7 @@ def solve_problem(self, callback=None):
dur=dur_ms,
t=t,
idx=self.idx,
monitor_val=monitor_val,
)

# step forward in time
Expand Down
2 changes: 1 addition & 1 deletion UM2N/generator/mesh_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def move_mesh(self):
)
mover.move()
# extract Hessian of the movement
sigma = mover.sigma
sigma = mover.H
I = fd.Identity(2) # noqa
jacobian = I + sigma
jacobian_det = fd.Function(mover.P1, name="jacobian_det")
Expand Down
35 changes: 18 additions & 17 deletions UM2N/generator/swirl_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from tqdm import tqdm # noqa
from UM2N.model.train_util import model_forward

from firedrake.__future__ import interpolate


def get_log_og(log_path, idx):
"""
Expand Down Expand Up @@ -453,7 +455,7 @@ def monitor_function(self, mesh, alpha=10, beta=5):
)

func_vec_space = fd.VectorFunctionSpace(mesh, "CG", 1)
uh_grad = fd.interpolate(fd.grad(self.u_cur), func_vec_space)
uh_grad = fd.assemble(interpolate(fd.grad(self.u_cur), func_vec_space))
self.grad_norm.interpolate(uh_grad[0] ** 2 + uh_grad[1] ** 2)

# filter_monitor_val = np.minimum(1e3, self.f_norm.dat.data[:])
Expand Down Expand Up @@ -526,7 +528,7 @@ def monitor_function_on_coarse_mesh(self, mesh, alpha=10, beta=5):
)

func_vec_space = fd.VectorFunctionSpace(mesh, "CG", 1)
uh_grad = fd.interpolate(fd.grad(self.u_cur), func_vec_space)
uh_grad = fd.assemble(interpolate(fd.grad(self.u_cur), func_vec_space))
self.grad_norm.interpolate(uh_grad[0] ** 2 + uh_grad[1] ** 2)

# Normlize the hessian
Expand Down Expand Up @@ -575,7 +577,7 @@ def solve_problem(self, callback=None, fail_callback=None):
print("In solve problem")
self.t = 0.0
step = 0
adapter = mv.MongeAmpereMover(
adaptor = mv.MongeAmpereMover(
self.mesh, monitor_function=self.monitor_function, rtol=1e-3, maxiter=500
)
for i in range(self.n_step):
Expand All @@ -602,15 +604,15 @@ def solve_problem(self, callback=None, fail_callback=None):
# self.mesh.coordinates.dat.data[:] = self.adapt_coord_prev
# mesh movement - calculate the adapted coords
start = time.perf_counter()
# adapter = mv.MongeAmpereMover(
# adaptor = mv.MongeAmpereMover(
# self.mesh, monitor_function=self.monitor_function, rtol=1e-3, maxiter=100
# )
adapter.move()
adaptor.move()
end = time.perf_counter()
dur_ms = (end - start) * 1e3
# self.mesh_new.coordinates.dat.data[:] = self.adapt_coord
self.mesh_new.coordinates.dat.data[:] = (
adapter.mesh.coordinates.dat.data[:]
adaptor.mesh.coordinates.dat.data[:]
)
# self.adapt_coord_prev = self.mesh_new.coordinates.dat.data[:]

Expand Down Expand Up @@ -655,23 +657,20 @@ def solve_problem(self, callback=None, fail_callback=None):
)

func_vec_space = fd.VectorFunctionSpace(self.mesh, "CG", 1)
uh_grad = fd.interpolate(fd.grad(uh), func_vec_space)
uh_grad = fd.assemble(interpolate(fd.grad(uh), func_vec_space))

hessian = self.l2_projection
phi = adapter.phi
phi_grad = adapter.grad_phi
sigma = adapter.sigma
phi = adaptor.phi
phi_grad = adaptor.grad_phi
sigma = adaptor.H
I = fd.Identity(2) # noqa
jacobian = I + sigma
jacobian_det = fd.Function(function_space, name="jacobian_det")
jacobian_det.project(
self.jacob_det = fd.Function(adaptor.P1, name="jacobian_det").project(
jacobian[0, 0] * jacobian[1, 1] - jacobian[0, 1] * jacobian[1, 0]
)
self.jacob_det = fd.project(
jacobian_det, fd.FunctionSpace(self.mesh, "CG", 1)
)
self.jacob = fd.project(
jacobian, fd.TensorFunctionSpace(self.mesh, "CG", 1)

self.jacob = fd.Function(adaptor.P1_ten, name="jacobian").project(
jacobian
)

if ((step + 1) % self.save_interval == 0) or (step == 0):
Expand All @@ -698,6 +697,8 @@ def solve_problem(self, callback=None, fail_callback=None):
sigma=self.sigma,
alpha=self.alpha,
r_0=self.r_0,
x_0=self.x_0,
y_0=self.y_0,
t=self.t,
)

Expand Down
27 changes: 14 additions & 13 deletions UM2N/generator/swirl_solver_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

from tqdm import tqdm # noqa

from firedrake.__future__ import interpolate


def get_c(x, y, t, threshold=0.5, alpha=1.5):
"""
Expand Down Expand Up @@ -459,7 +461,7 @@ def monitor_function_grad(self, mesh, alpha=5):
)

func_vec_space = fd.VectorFunctionSpace(self.mesh, "CG", 1)
uh_grad = fd.interpolate(fd.grad(self.u_cur), func_vec_space)
uh_grad = fd.assemble(interpolate(fd.grad(self.u_cur), func_vec_space))
self.grad_norm.project(uh_grad[0] ** 2 + uh_grad[1] ** 2)

self.adapt_coord = mesh.coordinates.vector().array().reshape(-1, 2) # noqa
Expand All @@ -481,7 +483,7 @@ def monitor_function_smoothed_grad(self, mesh, alpha=5):
)

func_vec_space = fd.VectorFunctionSpace(self.mesh, "CG", 1)
uh_grad = fd.interpolate(fd.grad(self.u_cur), func_vec_space)
uh_grad = fd.assemble(interpolate(fd.grad(self.u_cur), func_vec_space))
self.grad_norm.project(uh_grad[0] ** 2 + uh_grad[1] ** 2)

# Normlize the grad
Expand All @@ -508,7 +510,7 @@ def monitor_function_for_merge(self, mesh, alpha=10, beta=5):
)

func_vec_space = fd.VectorFunctionSpace(self.mesh, "CG", 1)
uh_grad = fd.interpolate(fd.grad(self.u_cur), func_vec_space)
uh_grad = fd.assemble(interpolate(fd.grad(self.u_cur), func_vec_space))
self.grad_norm.project(uh_grad[0] ** 2 + uh_grad[1] ** 2)

# Normlize the hessian
Expand Down Expand Up @@ -568,7 +570,7 @@ def monitor_function(self, mesh, alpha=10, beta=5):
)

func_vec_space = fd.VectorFunctionSpace(self.mesh, "CG", 1)
uh_grad = fd.interpolate(fd.grad(self.u_cur), func_vec_space)
uh_grad = fd.assemble(interpolate(fd.grad(self.u_cur), func_vec_space))
self.grad_norm.project(uh_grad[0] ** 2 + uh_grad[1] ** 2)

# Normlize the hessian
Expand Down Expand Up @@ -631,7 +633,7 @@ def monitor_function_on_coarse_mesh(self, mesh, alpha=10, beta=5):
)

func_vec_space = fd.VectorFunctionSpace(mesh, "CG", 1)
uh_grad = fd.interpolate(fd.grad(self.u_cur), func_vec_space)
uh_grad = fd.assemble(interpolate(fd.grad(self.u_cur), func_vec_space))
self.grad_norm.project(uh_grad[0] ** 2 + uh_grad[1] ** 2)

# Normlize the hessian
Expand Down Expand Up @@ -698,13 +700,13 @@ def solve_problem(self, callback=None, fail_callback=None):
# self.mesh.coordinates.dat.data[:] = self.adapt_coord_prev
# mesh movement - calculate the adapted coords
start = time.perf_counter()
adapter = mv.MongeAmpereMover(
adaptor = mv.MongeAmpereMover(
self.mesh,
monitor_function=self.monitor_function,
rtol=1e-3,
maxiter=100,
)
adapter.move()
adaptor.move()
end = time.perf_counter()
dur_ms = (end - start) * 1e3
self.mesh_new.coordinates.dat.data[:] = self.adapt_coord
Expand Down Expand Up @@ -749,13 +751,12 @@ def solve_problem(self, callback=None, fail_callback=None):
uh_fine.project(self.u_cur_fine)

func_vec_space = fd.VectorFunctionSpace(self.mesh, "CG", 1)
uh_grad = fd.interpolate(fd.grad(self.uh), func_vec_space)
# hessian_norm = self.f_norm
# monitor_values = adapter.monitor
uh_grad = fd.assemble(interpolate(fd.grad(self.uh), func_vec_space))

hessian = self.l2_projection
phi = adapter.phi
phi_grad = adapter.grad_phi
sigma = adapter.sigma
phi = adaptor.phi
phi_grad = adaptor.grad_phi
sigma = adaptor.H
I = fd.Identity(2) # noqa
jacobian = I + sigma
jacobian_det = fd.Function(function_space, name="jacobian_det")
Expand Down
46 changes: 30 additions & 16 deletions UM2N/processor/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,18 @@ def get_conv_feat(self, fix_reso_x=20, fix_reso_y=20):
for j in range(len(conv_y_fix)):
# (x, y) conv_feat
conv_xy_fix[:, i, j] = np.array([conv_x_fix[i], conv_y_fix[j]])
conv_uh_fix[:, i, j] = self.raw_feature["uh"].at(
[conv_x_fix[i], conv_y_fix[j]], tolerance=1e-3
)
if "uh" in self.raw_feature:
conv_uh_fix[:, i, j] = self.raw_feature["uh"].at(
[conv_x_fix[i], conv_y_fix[j]], tolerance=1e-3
)
if "grad_uh_norm" in self.raw_feature:
conv_grad_uh_norm_fix[:, i, j] = self.raw_feature[
"grad_uh_norm"
].at([conv_x_fix[i], conv_y_fix[j]], tolerance=1e-3)
conv_hessian_norm_fix[:, i, j] = self.raw_feature["hessian_norm"].at(
[conv_x_fix[i], conv_y_fix[j]], tolerance=1e-3
)
if "hessian_norm" in self.raw_feature:
conv_hessian_norm_fix[:, i, j] = self.raw_feature[
"hessian_norm"
].at([conv_x_fix[i], conv_y_fix[j]], tolerance=1e-3)
conv_monitor_val_fix[:, i, j] = self.raw_feature["monitor_val"].at(
[conv_x_fix[i], conv_y_fix[j]], tolerance=1e-3
)
Expand Down Expand Up @@ -316,16 +318,6 @@ def to_train_data(self):
np_data = {
"x": self.x,
"coord": self.coordinates,
"u": self.feature["uh"],
"grad_u": self.feature["grad_uh"],
"grad_u_norm": self.feature["grad_uh_norm"],
"hessian": self.feature["hessian"],
"phi": self.feature["phi"],
"grad_phi": self.feature["grad_phi"],
"hessian_norm": self.feature["hessian_norm"],
"jacobian": self.feature["jacobian"],
"jacobian_det": self.feature["jacobian_det"],
"monitor_val": self.feature["monitor_val"],
"edge_index": self.edge_T,
"edge_index_bi": self.edge_bi_T,
"cluster_edges": None, # this will be added if we use data_transform.py to add cluster edges # noqa
Expand Down Expand Up @@ -365,6 +357,28 @@ def to_train_data(self):
"swirl_params": self.swirl_params,
"t": self.t, # time step when solving burgers eq.
"idx": self.idx, # index number for picking params for burgers tracer. # noqa
"u": self.feature["uh"] if "uh" in self.feature else None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is equivalent to the following. If you could replicate this for the others, that'd be great.

Suggested change
"u": self.feature["uh"] if "uh" in self.feature else None,
"u": self.feature.get("uh"),

"grad_u": self.feature["grad_uh"] if "grad_uh" in self.feature else None,
"grad_u_norm": self.feature["grad_uh_norm"]
if "grad_uh_norm" in self.feature
else None,
"hessian": self.feature["hessian"] if "hessian" in self.feature else None,
"phi": self.feature["phi"] if "phi" in self.feature else None,
"grad_phi": self.feature["grad_phi"]
if "grad_phi" in self.feature
else None,
"hessian_norm": self.feature["hessian_norm"]
if "hessian_norm" in self.feature
else None,
"jacobian": self.feature["jacobian"]
if "jacobian" in self.feature
else None,
"jacobian_det": self.feature["jacobian_det"]
if "jacobian_det" in self.feature
else None,
"monitor_val": self.feature["monitor_val"]
if "monitor_val" in self.feature
else None,
"f": self.feature["f"] if "f" in self.feature else None,
}
if "uh_adapt" in self.feature: # currently only in swirl case
Expand Down
Loading