Skip to content

Commit

Permalink
Update of Exercise 5
Browse files Browse the repository at this point in the history
Signed-off-by: nick7 <[email protected]>
  • Loading branch information
nheilenkoetter committed Nov 12, 2024
1 parent 973b075 commit 8dbc46f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 49 deletions.
56 changes: 26 additions & 30 deletions examples/Day1/Exercise_5.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@
"# We do this here to speed up the training process, since sampling in time dependent domains\n",
"# is a little bit slower than in normal domains. \n",
"inner_sampler = tp.samplers.RandomUniformSampler(omega * I_t, 75000).make_static(resample_interval=250)\n",
"bc_sampler = tp.samplers.RandomUniformSampler(box.boundary*I_t, 25000)\n",
"def bc_filter(x):\n",
" return (x[:, 1] > 0.0001) & ((x[:, 0] < x_start) | (x[:, 0] > x_end))\n",
"bc_sampler = tp.samplers.RandomUniformSampler(box.boundary*I_t, 25000, filter_fn=bc_filter)\n",
"\n",
"time_sampler = tp.samplers.GridSampler(I_t, 120).make_static()\n",
"drill_sampler = tp.samplers.RandomUniformSampler(drill_hole.boundary, 2000) * time_sampler"
Expand Down Expand Up @@ -305,15 +307,13 @@
"metadata": {},
"outputs": [],
"source": [
"# Next the boundary conditions at the drill hole.\n",
"# It looks a bit complicated, therefore we implemented it for you.\n",
"# Next the boundary conditions at the drill hole:\n",
"tol = 0.0001 # small tolerance for filtering points\n",
"\n",
"def drill_residual(u, x, t):\n",
" # Apply hard constrains:\n",
" u = constrain_fn(u, x, t)\n",
"\n",
" resiudal = torch.zeros_like(u) # a tensor to write the residual into\n",
" residual = torch.zeros_like(u) # a tensor to write the residual into\n",
" normal_vectors = -drill_hole.boundary.normal(x, {\"t\": t})\n",
" u_n = tp.utils.normal_derivative(u, normal_vectors, x)\n",
" \n",
Expand All @@ -323,17 +323,10 @@
" # -> filter out the correct points\n",
" point_in_domain = (x[:, 1:] <= size_y)\n",
"\n",
" # Homogeneous Neumann condition on drill hole\n",
" resiudal[point_in_domain] = u_n[point_in_domain]\n",
" \n",
" # On the bottom side on the hole we have a heat source.\n",
" # First filter the points: \n",
" drill_section_x = torch.logical_and(x[:, :1] > x_start + tol, x[:, :1] < x_end - tol)\n",
" drill_section = torch.logical_and(point_in_domain, drill_section_x)\n",
" # Then construct residual:\n",
" resiudal[drill_section] = (u_n - heat_source/kappa)[drill_section]\n",
" residual[point_in_domain] = (u_n - heat_source/kappa)[point_in_domain]\n",
"\n",
" return resiudal\n",
" return residual\n",
"\n",
"drill_condition = tp.conditions.PINNCondition(module=model,\n",
" sampler=drill_sampler,\n",
Expand All @@ -343,20 +336,20 @@
},
{
"cell_type": "code",
"execution_count": 115,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# TODO: Implement the condition for the outer boundary (that is not on the drill hole).\n",
"# This resiudal will automatically only be applied on points on the boundary of the unit \n",
"# This residual will automatically only be applied on points on the boundary of the unit \n",
"# square (0, 1) x (0, 1).\n",
"\n",
"def bc_residual(u, x, t):\n",
" # Apply hard constrains:\n",
" \n",
" # Compute normal vectors and derivative:\n",
"\n",
" # Construct residual for the Neumann condition:\n",
" # Return residual for the Neumann condition:\n",
"\n",
" return \n",
"\n",
Expand Down Expand Up @@ -409,19 +402,22 @@
"metadata": {},
"outputs": [],
"source": [
"optim = tp.OptimizerSetting(optimizer_class=torch.optim.Adam, lr=1.e-3) \n",
"solver = tp.solver.Solver([pde_condition, drill_condition, bc_condition],\n",
" optimizer_setting=optim)\n",
"\n",
"trainer = pl.Trainer(devices=1, accelerator=\"gpu\",\n",
" num_sanity_val_steps=0,\n",
" benchmark=True,\n",
" max_steps=1500,\n",
" logger=False, \n",
" enable_checkpointing=False\n",
" )\n",
"\n",
"trainer.fit(solver)"
"learning_rates = [1.e-3, 2.e-4, 1e-4, 5e-5, 2.5e-5]\n",
"iterations = [1500, 2500, 4000, 1000, 2000]\n",
"for i in range(len(learning_rates)):\n",
" optim = tp.OptimizerSetting(optimizer_class=torch.optim.Adam, lr=learning_rates[i]) \n",
" solver = tp.solver.Solver([pde_condition, drill_condition, bc_condition],\n",
" optimizer_setting=optim)\n",
"\n",
" trainer = pl.Trainer(devices=1, accelerator=\"gpu\",\n",
" num_sanity_val_steps=0,\n",
" benchmark=True,\n",
" max_steps=iterations[i],\n",
" logger=False, \n",
" enable_checkpointing=False\n",
" )\n",
"\n",
" trainer.fit(solver)"
]
},
{
Expand Down
27 changes: 8 additions & 19 deletions examples/Day1/Sol_5.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@
"# We do this here to speed up the training process, since sampling in time dependent domains\n",
"# is a little bit slower than in normal domains. \n",
"inner_sampler = tp.samplers.RandomUniformSampler(omega * I_t, 75000).make_static(resample_interval=250)\n",
"bc_sampler = tp.samplers.RandomUniformSampler(box.boundary*I_t, 25000)\n",
"def bc_filter(x):\n",
" return (x[:, 1] > 0.0001) & ((x[:, 0] < x_start) | (x[:, 0] > x_end))\n",
"bc_sampler = tp.samplers.RandomUniformSampler(box.boundary*I_t, 25000, filter_fn=bc_filter)\n",
"\n",
"time_sampler = tp.samplers.GridSampler(I_t, 120).make_static()\n",
"drill_sampler = tp.samplers.RandomUniformSampler(drill_hole.boundary, 2000) * time_sampler"
Expand Down Expand Up @@ -304,7 +306,7 @@
"def drill_residual(u, x, t):\n",
" u = constrain_fn(u, x, t)\n",
"\n",
" resiudal = torch.zeros_like(u) # a tensor to write the residual into\n",
" residual = torch.zeros_like(u) # a tensor to write the residual into\n",
" normal_vectors = -drill_hole.boundary.normal(x, {\"t\": t})\n",
" u_n = tp.utils.normal_derivative(u, normal_vectors, x)\n",
" \n",
Expand All @@ -314,17 +316,10 @@
" # -> filter out the correct points\n",
" point_in_domain = (x[:, 1:] <= size_y)\n",
"\n",
" # Homogeneous Neumann condition on drill hole\n",
" resiudal[point_in_domain] = u_n[point_in_domain]\n",
" \n",
" # On the bottom side on the hole we have a heat source.\n",
" # First filter the points: \n",
" drill_section_x = torch.logical_and(x[:, :1] > x_start + tol, x[:, :1] < x_end - tol)\n",
" drill_section = torch.logical_and(point_in_domain, drill_section_x)\n",
" # Then construct residual:\n",
" resiudal[drill_section] = (u_n - heat_source/kappa)[drill_section]\n",
" residual[point_in_domain] = (u_n - heat_source/kappa)[point_in_domain]\n",
"\n",
" return resiudal\n",
" return residual\n",
"\n",
"drill_condition = tp.conditions.PINNCondition(module=model,\n",
" sampler=drill_sampler,\n",
Expand All @@ -334,7 +329,7 @@
},
{
"cell_type": "code",
"execution_count": 115,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -345,17 +340,11 @@
" u = constrain_fn(u, x, t)\n",
" \n",
" # Compute normal vectors and derivative:\n",
" resiudal = torch.zeros_like(u)\n",
" normals = box.boundary.normal(x)\n",
" u_n = tp.utils.normal_derivative(u, normals, x)\n",
"\n",
" # Construct residual:\n",
" resiudal = u_n\n",
"\n",
" lower_boundary = x[:, 1:] < tol # small tolerance for lower boundary\n",
" resiudal[lower_boundary] = u[lower_boundary] # = 0.0 (for fixed temperature)\n",
"\n",
" return resiudal\n",
" return u_n\n",
"\n",
"bc_condition = tp.conditions.PINNCondition(module=model,\n",
" sampler=bc_sampler,\n",
Expand Down

0 comments on commit 8dbc46f

Please sign in to comment.