{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import astropy.units as u\n",
    "import astropy.constants as constants"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Name   = Gravitational constant\n",
      "  Value  = 6.6743e-11\n",
      "  Uncertainty  = 1.5e-15\n",
      "  Unit  = m3 / (kg s2)\n",
      "  Reference = CODATA 2018\n"
     ]
    }
   ],
   "source": [
    "G = constants.G\n",
    "print(G)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "球状星团sigma,tcross,trelax: 1.8549215120939821 km / s 0.016669441152887474 Gyr 25.67742257852871 Gyr\n",
      "星系sigma,tcross,trelax: 131.16275797704594 km / s 0.047148299511187326 Gyr 31897535.15296635 Gyr\n",
      "星系团sigma,tcross,trelax: 1311.6275797704593 km / s 0.04714829951118733 Gyr 24912148264.12652 Gyr\n"
     ]
    }
   ],
   "source": [
    "#作业1\n",
    "def get_sigma_tcross_trelax(N,Re,R):\n",
    "    M = N * 1 *u.Msun\n",
    "    sigma = np.sqrt(G*M/(5*Re)).to(u.km/u.s)\n",
    "    V = np.sqrt(G*M/R)\n",
    "    tcross = (R/V).to(u.Gyr)\n",
    "    trelax = (tcross*(N/(6*np.log(N/2)))).to(u.Gyr)\n",
    "\n",
    "    return sigma,tcross,trelax\n",
    "#球状星团\n",
    "#N 恒星数量 Re Mpc R Mpc\n",
    "N = 1e5\n",
    "Re = 0.025*u.kpc\n",
    "R = 0.05*u.kpc\n",
    "sigma,tcross,trelax = get_sigma_tcross_trelax(N,Re,R)\n",
    "print('球状星团sigma,tcross,trelax:',sigma,tcross,trelax)\n",
    "#星系\n",
    "#N 恒星数量 Re Mpc R Mpc\n",
    "N = 1e11\n",
    "Re = 5*u.kpc\n",
    "R = 10*u.kpc\n",
    "sigma,tcross,trelax = get_sigma_tcross_trelax(N,Re,R)\n",
    "print('星系sigma,tcross,trelax:',sigma,tcross,trelax)\n",
    "#星系团\n",
    "#N 恒星数量 Re Mpc R Mpc\n",
    "N = 1e14\n",
    "Re = 50*u.kpc\n",
    "R = 100*u.kpc\n",
    "sigma,tcross,trelax = get_sigma_tcross_trelax(N,Re,R)\n",
    "print('星系团sigma,tcross,trelax:',sigma,tcross,trelax)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 星系团的例子数目是星系的数目，1000个左右，半径是Mpc量级"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.039458007868944635\n"
     ]
    }
   ],
   "source": [
    "#作业4\n",
    "Vc = (200*u.km/u.s).to(u.m/u.s)#km s-1\n",
    "Rd = (3*u.kpc).to(u.m)\n",
    "Md = (1e11*u.Msun).to(u.kg)\n",
    "M = (1e12*u.Msun).to(u.kg)\n",
    "Jd = 2*Md*Rd*Vc\n",
    "J = 10*Jd\n",
    "E = -M*Vc*Vc/2\n",
    "lamada = J * np.sqrt(np.abs(E))*(1/G)*((M)**(-5/2))\n",
    "\n",
    "print(lamada)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "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.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
