{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c6805951",
   "metadata": {},
   "source": [
    "# Multidimensional differential equations\n",
    "\n",
    "- Börge Göbel "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "921259c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt \n",
    "from scipy import integrate"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29eb17ec",
   "metadata": {},
   "source": [
    "# 3-body problem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "eb2d6604",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Constants in SI units\n",
    "\n",
    "# Gravitational constant\n",
    "G = 6.67430*10**(-11) # m^3 / ( kg * s^2 )\n",
    "\n",
    "# Masses\n",
    "msun = 1.9884 * 10**30 # kg\n",
    "mearth = 5.9723 * 10**24 # kg\n",
    "mmoon = 7.349 * 10**22 # kg\n",
    "\n",
    "# Distances (average)\n",
    "rSunEarth = 1.4960 * 10**11 # m\n",
    "rEarthMoon = 3.850 * 10**8 # m\n",
    "\n",
    "# Velocities (average)\n",
    "vEarth = 29780 # m/s (Trajectory around sun)\n",
    "vMoon = 1022 # m/s (Trajectory around earth)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd46d2f2",
   "metadata": {},
   "source": [
    "## A) Sun, Earth & Moon"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b461fb52",
   "metadata": {},
   "source": [
    "Variables for each of the 3 bodies: \\\\(x, y, z, v_x, v_y, v_z\\\\)\n",
    "\n",
    "Force: Newton's law of gravitation \n",
    "\n",
    "\\\\( \\vec{F} =G\\frac{m_1m_2}{|\\vec{r}_0-\\vec{r}|^3}(\\vec{r}_0-\\vec{r}) = -G\\frac{m_1m_2}{r^2}\\vec{e}_r\\\\) for \\\\( \\vec{r}_0=0\\\\)\n",
    "\n",
    "For the three bodies\n",
    "\n",
    "\\\\( m_1\\ddot{\\vec{r}_1}=\n",
    "G\\frac{m_1m_2}{|\\vec{r}_2-\\vec{r}_1|^3}(\\vec{r}_2-\\vec{r}_1) + G\\frac{m_1m_3}{|\\vec{r}_3-\\vec{r}_1|^3}(\\vec{r}_3-\\vec{r}_1)\\\\\n",
    "m_2\\ddot{\\vec{r}_2}=\n",
    "G\\frac{m_2m_1}{|\\vec{r}_1-\\vec{r}_2|^3}(\\vec{r}_1-\\vec{r}_2) + G\\frac{m_2m_3}{|\\vec{r}_3-\\vec{r}_2|^3}(\\vec{r}_3-\\vec{r}_2)\\\\\n",
    "m_3\\ddot{\\vec{r}_3}=\n",
    "G\\frac{m_3m_1}{|\\vec{r}_1-\\vec{r}_3|^3}(\\vec{r}_1-\\vec{r}_3) + G\\frac{m_3m_2}{|\\vec{r}_2-\\vec{r}_3|^3}(\\vec{r}_2-\\vec{r}_3)\n",
    "\\\\)\n",
    "\n",
    "We have the solve the following differential equations\n",
    "\n",
    "\\\\( \\ddot{\\vec{r}_1}=\n",
    "-G\\left(\\frac{m_2}{|\\vec{r}_2-\\vec{r}_1|^3}+\\frac{m_3}{|\\vec{r}_3-\\vec{r}_1|^3}\\right)\\vec{r}_1 + G\\frac{m_2}{|\\vec{r}_2-\\vec{r}_1|^3}\\vec{r}_2 + G\\frac{m_3}{|\\vec{r}_3-\\vec{r}_1|^3}\\vec{r}_3\\\\\n",
    "\\ddot{\\vec{r}_2}=\n",
    "G\\frac{m_1}{|\\vec{r}_1-\\vec{r}_2|^3}\\vec{r}_1 -G\\left(\\frac{m_1}{|\\vec{r}_1-\\vec{r}_2|^3}+\\frac{m_3}{|\\vec{r}_3-\\vec{r}_2|^3}\\right)\\vec{r}_2 + G\\frac{m_3}{|\\vec{r}_3-\\vec{r}_2|^3}\\vec{r}_3\\\\\n",
    "\\ddot{\\vec{r}_3}=\n",
    "G\\frac{m_1}{|\\vec{r}_1-\\vec{r}_3|^3}\\vec{r}_1 + G\\frac{m_2}{|\\vec{r}_2-\\vec{r}_3|^3}\\vec{r}_2 -G\\left(\\frac{m_1}{|\\vec{r}_1-\\vec{r}_3|^3}+\\frac{m_2}{|\\vec{r}_2-\\vec{r}_3|^3}\\right)\\vec{r}_3\n",
    "\\\\)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d61fc1d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "dd22d0b9",
   "metadata": {},
   "source": [
    "### Sun"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec73dcfb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "e2c8d852",
   "metadata": {},
   "source": [
    "### Earth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0cbda8e7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "5625ab12",
   "metadata": {},
   "source": [
    "### Moon"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8212f5c",
   "metadata": {},
   "source": [
    "### Moon orbit around earth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "846c8b99",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "2ebde7b4",
   "metadata": {},
   "source": [
    "### Exaggerate moon orbit radius"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "825f08c0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "f729b6b5",
   "metadata": {},
   "source": [
    "## B) Add a fourth body: Spaceship"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b47fdbf3",
   "metadata": {},
   "source": [
    "Since the mass of the spaceship is so small compared to the other masses, we will disregard the effect on sun, earth and moon. \n",
    "The 3 differential equation remain. \n",
    "\n",
    "The following equation describes the spaceship\n",
    "\n",
    "\\\\(\n",
    "m_4\\ddot{\\vec{r}_4}=\n",
    "G\\frac{m_4m_1}{|\\vec{r}_1-\\vec{r}_4|^3}(\\vec{r}_1-\\vec{r}_4) + \n",
    "G\\frac{m_4m_2}{|\\vec{r}_2-\\vec{r}_4|^3}(\\vec{r}_2-\\vec{r}_4) + \n",
    "G\\frac{m_4m_3}{|\\vec{r}_3-\\vec{r}_4|^3}(\\vec{r}_3-\\vec{r}_4)\n",
    "\\\\)\n",
    "\n",
    "We add to our systems of differential equations:\n",
    "\n",
    "\\\\(\n",
    "\\ddot{\\vec{r}_4}=\n",
    "G\\frac{m_1}{|\\vec{r}_1-\\vec{r}_4|^3}\\vec{r}_1 + G\\frac{m_2}{|\\vec{r}_2-\\vec{r}_4|^3}\\vec{r}_2+ G\\frac{m_3}{|\\vec{r}_3-\\vec{r}_4|^3}\\vec{r}_3 -G\\left(\\frac{m_1}{|\\vec{r}_1-\\vec{r}_4|^3}+\\frac{m_2}{|\\vec{r}_2-\\vec{r}_4|^3}+\\frac{m_3}{|\\vec{r}_3-\\vec{r}_4|^3}\\right)\\vec{r}_4\n",
    "\\\\)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90c9056f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "716a2393",
   "metadata": {},
   "source": [
    "## Analyze trajectory with respect to earth and moon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99a7271f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "f216edb7",
   "metadata": {},
   "source": [
    "### a) Elliptical orbit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a74b5d75",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a2f9abf9",
   "metadata": {},
   "source": [
    "### b) Direct earth escape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c94f001a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "bdadf27f",
   "metadata": {},
   "source": [
    "### c) Earth espace via moon encounter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09b58f60",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "e6a0fe1c",
   "metadata": {},
   "source": [
    "### d) Moon orbit"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d05d6cf",
   "metadata": {},
   "source": [
    "We have already achieved a trajectory that aproached the moon. In order to reach a stable orbit, we must decrease the relative velocity of the spaceship once it is close to the moon. We will temporarily apply a force that breaks the spaceship relative to the moon.\n",
    "\n",
    "The fourth differential equation changes to\n",
    "\n",
    "\\\\(\n",
    "\\ddot{\\vec{r}_4}=\n",
    "G\\frac{m_1}{|\\vec{r}_1-\\vec{r}_4|^3}\\vec{r}_1 + G\\frac{m_2}{|\\vec{r}_2-\\vec{r}_4|^3}\\vec{r}_2+ G\\frac{m_3}{|\\vec{r}_3-\\vec{r}_4|^3}\\vec{r}_3 -G\\left(\\frac{m_1}{|\\vec{r}_1-\\vec{r}_4|^3}+\\frac{m_2}{|\\vec{r}_2-\\vec{r}_4|^3}+\\frac{m_3}{|\\vec{r}_3-\\vec{r}_4|^3}\\right)\\vec{r}_4 - \\frac{F(t)}{m_4}\\vec{e_{v_\\mathrm{rel}}}\n",
    "\\\\)\n",
    "\n",
    "with \n",
    "\n",
    "\\\\(\n",
    "\\vec{e_{v_\\mathrm{rel}}}=\\frac{\\dot{\\vec{r}}_4-\\dot{\\vec{r}}_3}{\\left|\\dot{\\vec{r}}_4-\\dot{\\vec{r}}_3\\right|}\n",
    "\\\\)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d8d0d24",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
