Fix broken equation

This commit is contained in:
Paul Brinkmeier 2023-06-21 09:56:19 +02:00
parent 02629dc106
commit eeb83fe268
2 changed files with 128 additions and 64 deletions

File diff suppressed because one or more lines are too long

View File

@ -40,7 +40,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 33, "execution_count": 1,
"id": "05d4e22c-3d7d-4ac2-bf22-59b78aa40b84", "id": "05d4e22c-3d7d-4ac2-bf22-59b78aa40b84",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -64,7 +64,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 61, "execution_count": 18,
"id": "82bb4a02-1ecf-4c84-8b9d-b753dee75337", "id": "82bb4a02-1ecf-4c84-8b9d-b753dee75337",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -78,10 +78,10 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"-0.20000000000000007" "array([-0.14798801, -0.13453456])"
] ]
}, },
"execution_count": 61, "execution_count": 18,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -91,27 +91,32 @@
" displacement: np.ndarray,\n", " displacement: np.ndarray,\n",
" i: Tuple[int, int],\n", " i: Tuple[int, int],\n",
" j: Tuple[int, int]\n", " j: Tuple[int, int]\n",
") -> float:\n", ") -> np.ndarray:\n",
" u_ij = displacement[i] - displacement[j]\n", " u_ij = displacement[i] - displacement[j]\n",
" x_ij = np.array(i, dtype=float) - np.array(j, dtype=float)\n", " x_ij = np.array(i, dtype=float) - np.array(j, dtype=float)\n",
" K_ij = np.linalg.norm(x_ij) ** 2\n", " K_ij = np.linalg.norm(x_ij) ** 2\n",
" r_ij = x_ij + u_ij\n",
"\n", "\n",
" print(f\"{j=} {K_ij=} {u_ij=} {x_ij=}\")\n", " print(f\"{j=} {K_ij=} {u_ij=} {x_ij=}\")\n",
"\n", "\n",
" return -K_ij * np.dot(u_ij, x_ij)\n", " return -K_ij * np.dot(u_ij, x_ij) * r_ij / np.linalg.norm(r_ij)\n",
"\n", "\n",
"def calculate_force_bond(\n", "def calculate_force_bond(\n",
" displacement: np.ndarray,\n", " displacement: np.ndarray,\n",
" i: Tuple[int, int],\n", " i: Tuple[int, int],\n",
" j: Tuple[int, int]\n", " j: Tuple[int, int]\n",
") -> float:\n", ") -> np.ndarray:\n",
" return 0.0\n", " u_ij = displacement[i] - displacement[j]\n",
" x_ij = np.array(i, dtype=float) - np.array(j, dtype=float)\n",
" c_ij = 1.0\n",
"\n",
" return (-c_ij * u_ij) / (np.linalg.norm(x_ij) ** 2)\n",
"\n", "\n",
"def calculate_force(\n", "def calculate_force(\n",
" displacement: np.ndarray,\n", " displacement: np.ndarray,\n",
" i: Tuple[int, int],\n", " i: Tuple[int, int],\n",
" j: Tuple[int, int]\n", " j: Tuple[int, int]\n",
") -> float:\n", ") -> np.ndarray:\n",
" return calculate_force_elastic(displacement, i, j) + calculate_force_bond(displacement, i, j)\n", " return calculate_force_elastic(displacement, i, j) + calculate_force_bond(displacement, i, j)\n",
"\n", "\n",
"displacement = np.array([\n", "displacement = np.array([\n",
@ -124,19 +129,19 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 71, "execution_count": 19,
"id": "f7a019c2-8d20-4e84-9e0c-ef788781a07a", "id": "f7a019c2-8d20-4e84-9e0c-ef788781a07a",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "89618a87c5514c6698d62e3344092fdf", "model_id": "78145432b809422bb171f675b00d05a6",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
"text/plain": [ "text/plain": [
"interactive(children=(FloatSlider(value=0.0, description='dp_x', max=1.0, min=-1.0, step=0.05), FloatSlider(va…" "interactive(children=(FloatSlider(value=0.0, description='dp_x', max=1.5, min=-1.5, step=0.05), FloatSlider(va…"
] ]
}, },
"metadata": {}, "metadata": {},
@ -148,7 +153,7 @@
"<function __main__.do_plot(dp_x: float, dp_y: float) -> None>" "<function __main__.do_plot(dp_x: float, dp_y: float) -> None>"
] ]
}, },
"execution_count": 71, "execution_count": 19,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -172,16 +177,14 @@
" ys.append(y + displacement[j][1])\n", " ys.append(y + displacement[j][1])\n",
"\n", "\n",
" if j != i:\n", " if j != i:\n",
" F_ij = calculate_force_elastic(displacement, i, j)\n", " F_ij = calculate_force(displacement, i, j)\n",
" F_ij_vec = (\n", " print(f\"{F_ij=}\")\n",
" (i + displacement[i])\n", " elastic_xs.append(F_ij[0])\n",
" - (j + displacement[j])\n", " elastic_ys.append(F_ij[1])\n",
" ) * F_ij\n",
" print(f\"{F_ij_vec=}\")\n",
" elastic_xs.append(F_ij_vec[0])\n",
" elastic_ys.append(F_ij_vec[1])\n",
"\n", "\n",
" pi = i + displacement[i]\n", " pi = i + displacement[i]\n",
" plt.xlim(-1, 3)\n",
" plt.ylim(-1, 3)\n",
" plt.scatter(xs, ys)\n", " plt.scatter(xs, ys)\n",
" plt.quiver(\n", " plt.quiver(\n",
" [pi[0]] * len(elastic_xs),\n", " [pi[0]] * len(elastic_xs),\n",
@ -189,6 +192,7 @@
" elastic_xs,\n", " elastic_xs,\n",
" elastic_ys,\n", " elastic_ys,\n",
" scale = 5,\n", " scale = 5,\n",
" angles = \"xy\",\n",
" pivot = \"tip\",\n", " pivot = \"tip\",\n",
" width = 0.005\n", " width = 0.005\n",
" )\n", " )\n",
@ -198,14 +202,15 @@
" sum(elastic_ys),\n", " sum(elastic_ys),\n",
" pivot = \"tip\",\n", " pivot = \"tip\",\n",
" scale = 5,\n", " scale = 5,\n",
" angles = \"xy\",\n",
" color = \"red\"\n", " color = \"red\"\n",
" )\n", " )\n",
" \n", " \n",
"\n", "\n",
"interact(\n", "interact(\n",
" do_plot,\n", " do_plot,\n",
" dp_x = (-1.0, 1.0, 0.05),\n", " dp_x = (-1.5, 1.5, 0.05),\n",
" dp_y = (-1.0, 1.0, 0.05)\n", " dp_y = (-1.5, 1.5, 0.05)\n",
")" ")"
] ]
}, },
@ -216,6 +221,14 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [] "source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f283012a-6b7c-48c3-8bdc-f45f5b458edc",
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {