Disabled external gits
This commit is contained in:
@@ -0,0 +1,387 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import torch\n",
|
||||
"import igl\n",
|
||||
"\n",
|
||||
"import meshplot as mp\n",
|
||||
"import sys as _sys\n",
|
||||
"_sys.path.append(\"../src\")\n",
|
||||
"from elasticenergy import *\n",
|
||||
"from elasticsolid import *\n",
|
||||
"from adjoint_sensitivity import *\n",
|
||||
"from vis_utils import *\n",
|
||||
"from objectives import *\n",
|
||||
"from harmonic_interpolator import *\n",
|
||||
"from shape_optimizer import *\n",
|
||||
"\n",
|
||||
"from utils import *\n",
|
||||
"\n",
|
||||
"shadingOptions = {\n",
|
||||
" \"flat\":True,\n",
|
||||
" \"wireframe\":False, \n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"rot = np.array(\n",
|
||||
" [[1, 0, 0 ],\n",
|
||||
" [0, 0, 1],\n",
|
||||
" [0, -1, 0 ]]\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"torch.set_default_dtype(torch.float64)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Create the deformed object\n",
|
||||
"\n",
|
||||
"## Load the mesh"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"scrolled": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "9ae930440a43419c88fd82d71d7a6fa4",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"Renderer(camera=PerspectiveCamera(children=(DirectionalLight(color='white', intensity=0.6, position=(-1.987469…"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"<meshplot.Viewer.Viewer at 0x7f86840382e0>"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"vNP, _, _, tNP, _, _ = igl.read_obj(\"../data/dinosaur.obj\")\n",
|
||||
"# vNP, _, _, tNP, _, _ = igl.read_obj(\"../data/beam.obj\")\n",
|
||||
"\n",
|
||||
"aabb = np.max(vNP, axis=0) - np.min(vNP, axis=0)\n",
|
||||
"length_scale = np.mean(aabb)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"v, t = torch.tensor(vNP), torch.tensor(tNP)\n",
|
||||
"eNP = igl.edges(tNP)\n",
|
||||
"beNP = igl.edges(igl.boundary_facets(tNP))\n",
|
||||
"\n",
|
||||
"bvNP, ivNP = get_boundary_and_interior(v.shape[0], tNP)\n",
|
||||
"\n",
|
||||
"mp.plot(vNP @ rot.T, np.array(tNP), shading=shadingOptions)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Add some physical characteristics"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Pinned vertices: [ 46 47 50 59 60 62 64 65 88 89 91 98 99 102 103 104]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"rho = 131 # [kg.m-3], if aabb[0] ~ 14m, and m_tot = 6000kg\n",
|
||||
"young = 3e8 # [Pa] \n",
|
||||
"poisson = 0.2\n",
|
||||
"\n",
|
||||
"# Find some of the lowest vertices and pin them\n",
|
||||
"minZ = torch.min(v[:, 2])\n",
|
||||
"pin_idx = torch.arange(v.shape[0])[v[:, 2] < minZ + 0.01*aabb[2]]\n",
|
||||
"vIdx = np.arange(v.shape[0])\n",
|
||||
"pin_idx = vIdx[np.in1d(vIdx, bvNP) & np.in1d(vIdx, pin_idx)]\n",
|
||||
"print(\"Pinned vertices: {}\".format(pin_idx))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Initial guess\n",
|
||||
"\n",
|
||||
"The idea is that we start deforming the mesh by inverting gravity."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"scrolled": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "1ee888e94cd641f4bbbce4b051acccfb",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"Renderer(camera=PerspectiveCamera(children=(DirectionalLight(color='white', intensity=0.6, position=(-2.468079…"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Inverted gravity\n",
|
||||
"force_mass = torch.zeros(size=(3,))\n",
|
||||
"force_mass[2] = + rho * 9.81\n",
|
||||
"\n",
|
||||
"# Gravity going in the wrong direction\n",
|
||||
"\n",
|
||||
"ee = NeoHookeanElasticEnergy(young, poisson)\n",
|
||||
"\n",
|
||||
"v = HarmonicInterpolator(v, t, ivNP).interpolate(v[bvNP])\n",
|
||||
"solid_init = ElasticSolid(v, t, ee, rho=rho, pin_idx=pin_idx, f_mass=force_mass)\n",
|
||||
"\n",
|
||||
"solid_init.find_equilibrium()\n",
|
||||
"plot_torch_solid(solid_init, beNP, rot, length_scale)\n",
|
||||
"\n",
|
||||
"# Use these as initial guesses\n",
|
||||
"v_init_rest = solid_init.v_def.clone().detach()\n",
|
||||
"v_init_def = solid_init.v_rest.clone().detach()\n",
|
||||
"\n",
|
||||
"# v_init_rest = solid_init.v_rest.clone().detach()\n",
|
||||
"# v_init_def = solid_init.v_def.clone().detach()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Inverse design\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"scrolled": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "TypeError",
|
||||
"evalue": "unsupported format string passed to NoneType.__format__",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[0;32m/tmp/ipykernel_46/1868162750.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0msolid_\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate_def_shape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mv_init_def\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0moptimizer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mShapeOptimizer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msolid_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvt_surf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mweight_reg\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0.\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 17\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0mv_eq_init\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0moptimizer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msolid\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mv_def\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclone\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdetach\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m#bookkeeping\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/opt/notebooks/assignment_2_4/notebook/../src/shape_optimizer.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, solid, vt_surf, weight_reg)\u001b[0m\n\u001b[1;32m 48\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msolid\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfind_equilibrium\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 49\u001b[0m \u001b[0mobj_init\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtgt_fit\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msolid\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mv_def\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclone\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdetach\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 50\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Initial objective: {:.4e}\\n\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj_init\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 51\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 52\u001b[0m \u001b[0;31m# Initialize grad\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;31mTypeError\u001b[0m: unsupported format string passed to NoneType.__format__"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"force_mass = torch.zeros(size=(3,))\n",
|
||||
"force_mass[2] = - rho * 9.81\n",
|
||||
"use_linear = False\n",
|
||||
"\n",
|
||||
"# The target is the initial raw mesh\n",
|
||||
"vt_surf = torch.tensor(vNP[bvNP, :])\n",
|
||||
"\n",
|
||||
"# Create solid\n",
|
||||
"if use_linear:\n",
|
||||
" ee = LinearElasticEnergy(young, poisson)\n",
|
||||
"else:\n",
|
||||
" ee = NeoHookeanElasticEnergy(young, poisson)\n",
|
||||
"solid_ = ElasticSolid(v_init_rest, t, ee, rho=rho, pin_idx=pin_idx, f_mass=force_mass)\n",
|
||||
"solid_.update_def_shape(v_init_def)\n",
|
||||
"\n",
|
||||
"optimizer = ShapeOptimizer(solid_, vt_surf, weight_reg=0.)\n",
|
||||
"\n",
|
||||
"v_eq_init = optimizer.solid.v_def.clone().detach() #bookkeeping"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"optimizer.optimize(step_size_init=1e-4, max_l_iter=10, n_optim_steps=40)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"\n",
|
||||
"plt.figure(figsize=(10, 6))\n",
|
||||
"plt.plot(to_numpy(optimizer.objectives[optimizer.objectives > 0]))\n",
|
||||
"plt.title(\"Objective as optimization goes\", fontsize=14)\n",
|
||||
"plt.xlabel(\"Optimization steps\", fontsize=12)\n",
|
||||
"plt.ylabel(\"Objective\", fontsize=12)\n",
|
||||
"plt.grid()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Green (Initial guess for rest state) deploys to Black\n",
|
||||
"\n",
|
||||
"Blue (Optimized rest state) deploys to Yellow\n",
|
||||
"\n",
|
||||
"Red is the Target Shape\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"p = mp.plot(np.array(optimizer.solid.v_def) @ rot.T, tNP, shading=shadingOptions)\n",
|
||||
"# p.add_points(np.array(optimizer.solid.v_def)[pin_idx, :] @ rot.T, shading={\"point_color\":\"black\", \"point_size\": 0.2})\n",
|
||||
"p.add_edges(np.array(v_init_rest) @ rot.T, beNP, shading={\"line_color\": \"green\"})\n",
|
||||
"p.add_edges(vNP @ rot.T, beNP, shading={\"line_color\": \"red\"})\n",
|
||||
"p.add_edges(np.array(v_eq_init) @ rot.T, beNP, shading={\"line_color\": \"black\"})\n",
|
||||
"p.add_edges(np.array(optimizer.solid.v_rest) @ rot.T, beNP, shading={\"line_color\": \"blue\"})\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"v_rest_optim_g = optimizer.solid.v_rest.clone().detach() #bookkeeping"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Add point load to the right most vertices\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"maxX = torch.min(v[:, 0])\n",
|
||||
"f_point_idx = torch.arange(v.shape[0])[v[:, 0] > maxX - 0.01*aabb[0]]\n",
|
||||
"\n",
|
||||
"f_point = torch.zeros(size=(f_point_idx.shape[0], 3))\n",
|
||||
"f_point[:, 2] = -5e3\n",
|
||||
"\n",
|
||||
"optimizer.solid.add_point_load(f_point_idx, f_point)\n",
|
||||
"optimizer.set_params(optimizer.params)\n",
|
||||
"v_def_optim_g_under_point = optimizer.solid.v_def.clone().detach() #bookkeeping"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"optimizer.reset_BFGS()\n",
|
||||
"optimizer.optimize(step_size_init=1e-4, max_l_iter=10, n_optim_steps=20)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Green (Optimum rest state under gravity) deploys to Black with the additional point load\n",
|
||||
"\n",
|
||||
"Blue (Optimized rest state) deploys to Yellow\n",
|
||||
"\n",
|
||||
"Red is the Target Shape\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"p = mp.plot(np.array(optimizer.solid.v_def) @ rot.T, tNP, shading=shadingOptions)\n",
|
||||
"# p.add_points(np.array(optimizer.solid.v_def)[pin_idx, :] @ rot.T, shading={\"point_color\":\"black\", \"point_size\": 0.2})\n",
|
||||
"p.add_edges(np.array(v_rest_optim_g) @ rot.T, beNP, shading={\"line_color\": \"green\"})\n",
|
||||
"p.add_edges(vNP @ rot.T, beNP, shading={\"line_color\": \"red\"})\n",
|
||||
"p.add_edges(np.array(v_def_optim_g_under_point) @ rot.T, beNP, shading={\"line_color\": \"black\"})\n",
|
||||
"p.add_edges(np.array(optimizer.solid.v_rest) @ rot.T, beNP, shading={\"line_color\": \"blue\"})\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.7"
|
||||
},
|
||||
"toc": {
|
||||
"base_numbering": 1,
|
||||
"nav_menu": {},
|
||||
"number_sections": true,
|
||||
"sideBar": true,
|
||||
"skip_h1_title": false,
|
||||
"title_cell": "Table of Contents",
|
||||
"title_sidebar": "Contents",
|
||||
"toc_cell": false,
|
||||
"toc_position": {},
|
||||
"toc_section_display": true,
|
||||
"toc_window_display": false
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user