{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 球体重磁异常正演计算\n",
    "\n",
    "本notebook实现了球体的重力异常和磁异常正演计算，包含理论公式、数值计算和可视化展示。\n",
    "\n",
    "## 1. 理论基础\n",
    "\n",
    "### 1.1 球体重力异常\n",
    "对于半径为 $R$、密度为 $\\rho$、中心埋深为 $h$ 的球体，其在地面 $(x,y)$ 处的重力异常为：\n",
    "\n",
    "$$\\Delta g = \\frac{4\\pi G \\rho R^3}{3} \\cdot \\frac{h}{(x^2 + y^2 + h^2)^{3/2}}$$\n",
    "\n",
    "其中：\n",
    "- $G = 6.67 \\times 10^{-11} \\text{ N·m}^2/\\text{kg}^2$ 为万有引力常数\n",
    "- $\\rho$ 为球体密度\n",
    "- $R$ 为球体半径\n",
    "- $h$ 为球体中心埋深\n",
    "\n",
    "### 1.2 球体磁异常\n",
    "对于磁化强度为 $M$、磁化倾角为 $I$、磁化偏角为 $D$ 的球体，其在地面 $(x,y)$ 处的磁异常为：\n",
    "\n",
    "$$\\Delta T = \\frac{\\mu_0 M V}{4\\pi} \\cdot \\frac{1}{(x^2 + y^2 + h^2)^{5/2}} \\cdot [(2h^2 - x^2 - y^2)\\sin I + 3hx\\cos I \\cos D + 3hy\\cos I \\sin D]$$\n",
    "\n",
    "其中：\n",
    "- $\\mu_0 = 4\\pi \\times 10^{-7} \\text{ H/m}$ 为真空磁导率\n",
    "- $M$ 为磁化强度\n",
    "- $V = \\frac{4}{3}\\pi R^3$ 为球体体积\n",
    "- $I$ 为磁化倾角\n",
    "- $D$ 为磁化偏角"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入必要的库\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from mpl_toolkits.mplot3d import Axes3D\n",
    "import ipywidgets as widgets\n",
    "from ipywidgets import interact, FloatSlider, IntSlider\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "# 设置中文字体\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans']\n",
    "plt.rcParams['axes.unicode_minus'] = False\n",
    "\n",
    "# 物理常数\n",
    "G = 6.67e-11  # 万有引力常数 (N·m²/kg²)\n",
    "mu_0 = 4 * np.pi * 1e-7  # 真空磁导率 (H/m)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 正演计算函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gravity_anomaly_sphere(x, y, R, rho, h):\n",
    "    \"\"\"\n",
    "    计算球体重力异常\n",
    "    \n",
    "    参数:\n",
    "    x, y: 观测点坐标 (m)\n",
    "    R: 球体半径 (m)\n",
    "    rho: 球体密度 (kg/m³)\n",
    "    h: 球体中心埋深 (m)\n",
    "    \n",
    "    返回:\n",
    "    重力异常值 (mGal)\n",
    "    \"\"\"\n",
    "    r_squared = x**2 + y**2 + h**2\n",
    "    r = np.sqrt(r_squared)\n",
    "    \n",
    "    # 重力异常公式 (单位: m/s²)\n",
    "    delta_g = (4 * np.pi * G * rho * R**3 / 3) * h / (r_squared)**(3/2)\n",
    "    \n",
    "    # 转换为 mGal (1 mGal = 1e-5 m/s²)\n",
    "    return delta_g * 1e5\n",
    "\n",
    "def magnetic_anomaly_sphere(x, y, R, M, h, I, D):\n",
    "    \"\"\"\n",
    "    计算球体磁异常\n",
    "    \n",
    "    参数:\n",
    "    x, y: 观测点坐标 (m)\n",
    "    R: 球体半径 (m)\n",
    "    M: 磁化强度 (A/m)\n",
    "    h: 球体中心埋深 (m)\n",
    "    I: 磁化倾角 (度)\n",
    "    D: 磁化偏角 (度)\n",
    "    \n",
    "    返回:\n",
    "    磁异常值 (nT)\n",
    "    \"\"\"\n",
    "    # 角度转换为弧度\n",
    "    I_rad = np.radians(I)\n",
    "    D_rad = np.radians(D)\n",
    "    \n",
    "    r_squared = x**2 + y**2 + h**2\n",
    "    r = np.sqrt(r_squared)\n",
    "    \n",
    "    # 球体体积\n",
    "    V = 4 * np.pi * R**3 / 3\n",
    "    \n",
    "    # 磁异常公式\n",
    "    factor = mu_0 * M * V / (4 * np.pi * r_squared**(5/2))\n",
    "    \n",
    "    # 磁异常分量\n",
    "    delta_T = factor * ((2*h**2 - x**2 - y**2) * np.sin(I_rad) + \n",
    "                       3*h*x * np.cos(I_rad) * np.cos(D_rad) + \n",
    "                       3*h*y * np.cos(I_rad) * np.sin(D_rad))\n",
    "    \n",
    "    # 转换为 nT (1 nT = 1e-9 T)\n",
    "    return delta_T * 1e9"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. 可视化函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_gravity_anomaly(X, Y, gravity_data, R, rho, h):\n",
    "    \"\"\"绘制重力异常等值线图和剖面图\"\"\"\n",
    "    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))\n",
    "    \n",
    "    # 等值线图\n",
    "    contour = ax1.contour(X, Y, gravity_data, levels=20, colors='black', linewidths=0.5)\n",
    "    ax1.clabel(contour, inline=True, fontsize=8)\n",
    "    im1 = ax1.contourf(X, Y, gravity_data, levels=20, cmap='RdBu_r', alpha=0.8)\n",
    "    ax1.set_xlabel('X (m)')\n",
    "    ax1.set_ylabel('Y (m)')\n",
    "    ax1.set_title(f'Gravity Anomaly (2D)\\nR={R}m, ρ={rho}kg/m³, h={h}m')\n",
    "    plt.colorbar(im1, ax=ax1, label='Gravity Anomaly (mGal)')\n",
    "    \n",
    "    # 剖面图 (y=0)\n",
    "    profile_y = 0\n",
    "    profile_idx = np.argmin(np.abs(Y[:, 0] - profile_y))\n",
    "    profile_x = X[profile_idx, :]\n",
    "    profile_gravity = gravity_data[profile_idx, :]\n",
    "    \n",
    "    ax2.plot(profile_x, profile_gravity, 'b-', linewidth=2, label='Gravity Anomaly (1D)')\n",
    "    ax2.set_xlabel('X (m)')\n",
    "    ax2.set_ylabel('Gravity Anomaly (mGal)')\n",
    "    ax2.set_title(f'Gravity Anomaly (1D) (Y={profile_y}m)')\n",
    "    ax2.grid(True, alpha=0.3)\n",
    "    ax2.legend()\n",
    "    \n",
    "    plt.tight_layout()\n",
    "    plt.show()\n",
    "\n",
    "def plot_magnetic_anomaly(X, Y, magnetic_data, R, M, h, I, D):\n",
    "    \"\"\"绘制磁异常等值线图和剖面图\"\"\"\n",
    "    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))\n",
    "    \n",
    "    # 等值线图\n",
    "    contour = ax1.contour(X, Y, magnetic_data, levels=20, colors='black', linewidths=0.5)\n",
    "    ax1.clabel(contour, inline=True, fontsize=8)\n",
    "    im1 = ax1.contourf(X, Y, magnetic_data, levels=20, cmap='RdBu_r', alpha=0.8)\n",
    "    ax1.set_xlabel('X (m)')\n",
    "    ax1.set_ylabel('Y (m)')\n",
    "    ax1.set_title(f'磁异常等值线图\\nR={R}m, M={M}A/m, h={h}m, I={I}°, D={D}°')\n",
    "    plt.colorbar(im1, ax=ax1, label='磁异常 (nT)')\n",
    "    \n",
    "    # 剖面图 (y=0)\n",
    "    profile_y = 0\n",
    "    profile_idx = np.argmin(np.abs(Y[:, 0] - profile_y))\n",
    "    profile_x = X[profile_idx, :]\n",
    "    profile_magnetic = magnetic_data[profile_idx, :]\n",
    "    \n",
    "    ax2.plot(profile_x, profile_magnetic, 'r-', linewidth=2, label='磁异常')\n",
    "    ax2.set_xlabel('X (m)')\n",
    "    ax2.set_ylabel('磁异常 (nT)')\n",
    "    ax2.set_title(f'磁异常剖面图 (Y={profile_y}m)')\n",
    "    ax2.grid(True, alpha=0.3)\n",
    "    ax2.legend()\n",
    "    \n",
    "    plt.tight_layout()\n",
    "    plt.show()\n",
    "\n",
    "def plot_3d_anomaly(X, Y, data, title, zlabel):\n",
    "    \"\"\"绘制3D异常图\"\"\"\n",
    "    fig = plt.figure(figsize=(12, 8))\n",
    "    ax = fig.add_subplot(111, projection='3d')\n",
    "    \n",
    "    surf = ax.plot_surface(X, Y, data, cmap='RdBu_r', alpha=0.8)\n",
    "    ax.set_xlabel('X (m)')\n",
    "    ax.set_ylabel('Y (m)')\n",
    "    ax.set_zlabel(zlabel)\n",
    "    ax.set_title(title)\n",
    "    \n",
    "    plt.colorbar(surf, ax=ax, label=zlabel)\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. 交互式参数调整"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def interactive_gravity_anomaly():\n",
    "    \"\"\"交互式重力异常计算\"\"\"\n",
    "    \n",
    "    @interact\n",
    "    def gravity_plot(R=FloatSlider(min=10, max=200, step=10, value=50, description='半径 (m)'),\n",
    "                    rho=FloatSlider(min=1000, max=5000, step=100, value=3000, description='密度 (kg/m³)'),\n",
    "                    h=FloatSlider(min=20, max=200, step=10, value=100, description='埋深 (m)'),\n",
    "                    x_range=FloatSlider(min=100, max=1000, step=50, value=400, description='X范围 (m)'),\n",
    "                    y_range=FloatSlider(min=100, max=1000, step=50, value=400, description='Y范围 (m)')):\n",
    "        \n",
    "        # 创建网格\n",
    "        x = np.linspace(-x_range, x_range, 100)\n",
    "        y = np.linspace(-y_range, y_range, 100)\n",
    "        X, Y = np.meshgrid(x, y)\n",
    "        \n",
    "        # 计算重力异常\n",
    "        gravity_data = gravity_anomaly_sphere(X, Y, R, rho, h)\n",
    "        \n",
    "        # 绘制结果\n",
    "        plot_gravity_anomaly(X, Y, gravity_data, R, rho, h)\n",
    "        \n",
    "        # 显示参数信息\n",
    "        print(f\"球体参数:\")\n",
    "        print(f\"半径: {R} m\")\n",
    "        print(f\"密度: {rho} kg/m³\")\n",
    "        print(f\"埋深: {h} m\")\n",
    "        print(f\"最大重力异常: {np.max(gravity_data):.2f} mGal\")\n",
    "        print(f\"最小重力异常: {np.min(gravity_data):.2f} mGal\")\n",
    "\n",
    "def interactive_magnetic_anomaly():\n",
    "    \"\"\"交互式磁异常计算\"\"\"\n",
    "    \n",
    "    @interact\n",
    "    def magnetic_plot(R=FloatSlider(min=10, max=200, step=10, value=50, description='半径 (m)'),\n",
    "                     M=FloatSlider(min=100, max=5000, step=100, value=1000, description='磁化强度 (A/m)'),\n",
    "                     h=FloatSlider(min=20, max=200, step=10, value=100, description='埋深 (m)'),\n",
    "                     I=FloatSlider(min=-90, max=90, step=5, value=60, description='磁化倾角 (°)'),\n",
    "                     D=FloatSlider(min=0, max=360, step=10, value=0, description='磁化偏角 (°)'),\n",
    "                     x_range=FloatSlider(min=100, max=1000, step=50, value=400, description='X范围 (m)'),\n",
    "                     y_range=FloatSlider(min=100, max=1000, step=50, value=400, description='Y范围 (m)')):\n",
    "        \n",
    "        # 创建网格\n",
    "        x = np.linspace(-x_range, x_range, 100)\n",
    "        y = np.linspace(-y_range, y_range, 100)\n",
    "        X, Y = np.meshgrid(x, y)\n",
    "        \n",
    "        # 计算磁异常\n",
    "        magnetic_data = magnetic_anomaly_sphere(X, Y, R, M, h, I, D)\n",
    "        \n",
    "        # 绘制结果\n",
    "        plot_magnetic_anomaly(X, Y, magnetic_data, R, M, h, I, D)\n",
    "        \n",
    "        # 显示参数信息\n",
    "        print(f\"球体参数:\")\n",
    "        print(f\"半径: {R} m\")\n",
    "        print(f\"磁化强度: {M} A/m\")\n",
    "        print(f\"埋深: {h} m\")\n",
    "        print(f\"磁化倾角: {I}°\")\n",
    "        print(f\"磁化偏角: {D}°\")\n",
    "        print(f\"最大磁异常: {np.max(magnetic_data):.2f} nT\")\n",
    "        print(f\"最小磁异常: {np.min(magnetic_data):.2f} nT\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. 使用示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== 示例1: 固定参数重力异常 ===\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 183652 characters]",
      "text/plain": [
       "<Figure size 1500x600 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最大重力异常: 1.04 mGal\n",
      "最小重力异常: 0.01 mGal\n"
     ]
    }
   ],
   "source": [
    "# 示例1: 固定参数的重力异常计算\n",
    "print(\"=== 示例1: 固定参数重力异常 ===\")\n",
    "\n",
    "# 参数设置\n",
    "R = 50  # 半径 (m)\n",
    "rho = 3000  # 密度 (kg/m³)\n",
    "h = 100  # 埋深 (m)\n",
    "x_range = 400  # X范围 (m)\n",
    "y_range = 400  # Y范围 (m)\n",
    "\n",
    "# 创建网格\n",
    "x = np.linspace(-x_range, x_range, 100)\n",
    "y = np.linspace(-y_range, y_range, 100)\n",
    "X, Y = np.meshgrid(x, y)\n",
    "\n",
    "# 计算重力异常\n",
    "gravity_data = gravity_anomaly_sphere(X, Y, R, rho, h)\n",
    "\n",
    "# 绘制结果\n",
    "plot_gravity_anomaly(X, Y, gravity_data, R, rho, h)\n",
    "\n",
    "print(f\"最大重力异常: {np.max(gravity_data):.2f} mGal\")\n",
    "print(f\"最小重力异常: {np.min(gravity_data):.2f} mGal\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== 示例2: 固定参数磁异常 ===\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 134236 characters]",
      "text/plain": [
       "<Figure size 1500x600 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最大磁异常: 95523.32 nT\n",
      "最小磁异常: -7447.79 nT\n"
     ]
    }
   ],
   "source": [
    "# 示例2: 固定参数的磁异常计算\n",
    "print(\"=== 示例2: 固定参数磁异常 ===\")\n",
    "\n",
    "# 参数设置\n",
    "R = 50  # 半径 (m)\n",
    "M = 1000  # 磁化强度 (A/m)\n",
    "h = 100  # 埋深 (m)\n",
    "I = 60  # 磁化倾角 (°)\n",
    "D = 0  # 磁化偏角 (°)\n",
    "x_range = 400  # X范围 (m)\n",
    "y_range = 400  # Y范围 (m)\n",
    "\n",
    "# 创建网格\n",
    "x = np.linspace(-x_range, x_range, 100)\n",
    "y = np.linspace(-y_range, y_range, 100)\n",
    "X, Y = np.meshgrid(x, y)\n",
    "\n",
    "# 计算磁异常\n",
    "magnetic_data = magnetic_anomaly_sphere(X, Y, R, M, h, I, D)\n",
    "\n",
    "# 绘制结果\n",
    "plot_magnetic_anomaly(X, Y, magnetic_data, R, M, h, I, D)\n",
    "\n",
    "print(f\"最大磁异常: {np.max(magnetic_data):.2f} nT\")\n",
    "print(f\"最小磁异常: {np.min(magnetic_data):.2f} nT\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== 示例3: 3D重力异常可视化 ===\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 289000 characters]",
      "text/plain": [
       "<Figure size 1200x800 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 示例3: 3D可视化\n",
    "print(\"=== 示例3: 3D重力异常可视化 ===\")\n",
    "\n",
    "# 使用较小的网格以提高性能\n",
    "x = np.linspace(-200, 200, 50)\n",
    "y = np.linspace(-200, 200, 50)\n",
    "X, Y = np.meshgrid(x, y)\n",
    "\n",
    "# 计算重力异常\n",
    "gravity_data = gravity_anomaly_sphere(X, Y, R=50, rho=3000, h=100)\n",
    "\n",
    "# 3D可视化\n",
    "plot_3d_anomaly(X, Y, gravity_data, '球体重力异常3D图', '重力异常 (mGal)')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. 交互式计算\n",
    "\n",
    "运行下面的代码块，使用滑块调整参数来实时查看重磁异常的变化："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== 交互式重力异常计算 ===\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0f183af415864da1af4c7d3abaf4bfc1",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(FloatSlider(value=50.0, description='半径 (m)', max=200.0, min=10.0, step=10.0), FloatSlid…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 交互式重力异常计算\n",
    "print(\"=== 交互式重力异常计算 ===\")\n",
    "interactive_gravity_anomaly()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== 交互式磁异常计算 ===\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ff8fcbe1939e4af5899f75843faa2090",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(FloatSlider(value=50.0, description='半径 (m)', max=200.0, min=10.0, step=10.0), FloatSlid…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 交互式磁异常计算\n",
    "print(\"=== 交互式磁异常计算 ===\")\n",
    "interactive_magnetic_anomaly()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. 参数敏感性分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== 重力异常参数敏感性分析 ===\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 255432 characters]",
      "text/plain": [
       "<Figure size 1500x1200 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 重力异常参数敏感性分析\n",
    "print(\"=== 重力异常参数敏感性分析 ===\")\n",
    "\n",
    "# 基准参数\n",
    "base_R, base_rho, base_h = 50, 3000, 100\n",
    "x_profile = np.linspace(-200, 200, 100)\n",
    "y_profile = 0\n",
    "\n",
    "fig, axes = plt.subplots(2, 2, figsize=(15, 12))\n",
    "\n",
    "# 半径变化的影响\n",
    "radii = [30, 50, 70, 100]\n",
    "for R in radii:\n",
    "    gravity = gravity_anomaly_sphere(x_profile, y_profile, R, base_rho, base_h)\n",
    "    axes[0,0].plot(x_profile, gravity, label=f'R={R}m')\n",
    "axes[0,0].set_xlabel('X (m)')\n",
    "axes[0,0].set_ylabel('重力异常 (mGal)')\n",
    "axes[0,0].set_title('半径变化对重力异常的影响')\n",
    "axes[0,0].legend()\n",
    "axes[0,0].grid(True, alpha=0.3)\n",
    "\n",
    "# 密度变化的影响\n",
    "densities = [2000, 3000, 4000, 5000]\n",
    "for rho in densities:\n",
    "    gravity = gravity_anomaly_sphere(x_profile, y_profile, base_R, rho, base_h)\n",
    "    axes[0,1].plot(x_profile, gravity, label=f'ρ={rho}kg/m³')\n",
    "axes[0,1].set_xlabel('X (m)')\n",
    "axes[0,1].set_ylabel('重力异常 (mGal)')\n",
    "axes[0,1].set_title('密度变化对重力异常的影响')\n",
    "axes[0,1].legend()\n",
    "axes[0,1].grid(True, alpha=0.3)\n",
    "\n",
    "# 埋深变化的影响\n",
    "depths = [50, 100, 150, 200]\n",
    "for h in depths:\n",
    "    gravity = gravity_anomaly_sphere(x_profile, y_profile, base_R, base_rho, h)\n",
    "    axes[1,0].plot(x_profile, gravity, label=f'h={h}m')\n",
    "axes[1,0].set_xlabel('X (m)')\n",
    "axes[1,0].set_ylabel('重力异常 (mGal)')\n",
    "axes[1,0].set_title('埋深变化对重力异常的影响')\n",
    "axes[1,0].legend()\n",
    "axes[1,0].grid(True, alpha=0.3)\n",
    "\n",
    "# 综合对比\n",
    "gravity_base = gravity_anomaly_sphere(x_profile, y_profile, base_R, base_rho, base_h)\n",
    "axes[1,1].plot(x_profile, gravity_base, 'k-', linewidth=2, label='基准参数')\n",
    "axes[1,1].set_xlabel('X (m)')\n",
    "axes[1,1].set_ylabel('重力异常 (mGal)')\n",
    "axes[1,1].set_title('基准参数重力异常剖面')\n",
    "axes[1,1].legend()\n",
    "axes[1,1].grid(True, alpha=0.3)\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== 磁异常参数敏感性分析 ===\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 169820 characters]",
      "text/plain": [
       "<Figure size 1500x600 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 磁异常参数敏感性分析\n",
    "print(\"=== 磁异常参数敏感性分析 ===\")\n",
    "\n",
    "# 基准参数\n",
    "base_R, base_M, base_h, base_I, base_D = 50, 1000, 100, 60, 0\n",
    "\n",
    "fig, axes = plt.subplots(2, 3, figsize=(18, 12))\n",
    "\n",
    "# 磁化强度变化的影响\n",
    "magnetizations = [500, 1000, 2000, 3000]\n",
    "for M in magnetizations:\n",
    "    magnetic = magnetic_anomaly_sphere(x_profile, y_profile, base_R, M, base_h, base_I, base_D)\n",
    "    axes[0,0].plot(x_profile, magnetic, label=f'M={M}A/m')\n",
    "axes[0,0].set_xlabel('X (m)')\n",
    "axes[0,0].set_ylabel('磁异常 (nT)')\n",
    "axes[0,0].set_title('磁化强度对磁异常的影响')\n",
    "axes[0,0].legend()\n",
    "axes[0,0].grid(True, alpha=0.3)\n",
    "\n",
    "# 磁化倾角变化的影响\n",
    "inclinations = [0, 30, 60, 90]\n",
    "for I in inclinations:\n",
    "    magnetic = magnetic_anomaly_sphere(x_profile, y_profile, base_R, base_M, base_h, I, base_D)\n",
    "    axes[0,1].plot(x_profile, magnetic, label=f'I={I}°')\n",
    "axes[0,1].set_xlabel('X (m)')\n",
    "axes[0,1].set_ylabel('磁异常 (nT)')\n",
    "axes[0,1].set_title('磁化倾角对磁异常的影响')\n",
    "axes[0,1].legend()\n",
    "axes[0,1].grid(True, alpha=0.3)\n",
    "\n",
    "# 磁化偏角变化的影响\n",
    "declinations = [0, 45, 90, 135]\n",
    "for D in declinations:\n",
    "    magnetic = magnetic_anomaly_sphere(x_profile, y_profile, base_R, base_M, base_h, base_I, D)\n",
    "    axes[0,2].plot(x_profile, magnetic, label=f'D={D}°')\n",
    "axes[0,2].set_xlabel('X (m)')\n",
    "axes[0,2].set_ylabel('磁异常 (nT)')\n",
    "axes[0,2].set_title('磁化偏角对磁异常的影响')\n",
    "axes[0,2].legend()\n",
    "axes[0,2].grid(True, alpha=0.3)\n",
    "\n",
    "# 半径变化的影响\n",
    "radii = [30, 50, 70, 100]\n",
    "for R in radii:\n",
    "    magnetic = magnetic_anomaly_sphere(x_profile, y_profile, R, base_M, base_h, base_I, base_D)\n",
    "    axes[1,0].plot(x_profile, magnetic, label=f'R={R}m')\n",
    "axes[1,0].set_xlabel('X (m)')\n",
    "axes[1,0].set_ylabel('磁异常 (nT)')\n",
    "axes[1,0].set_title('半径对磁异常的影响')\n",
    "axes[1,0].legend()\n",
    "axes[1,0].grid(True, alpha=0.3)\n",
    "\n",
    "# 埋深变化的影响\n",
    "depths = [50, 100, 150, 200]\n",
    "for h in depths:\n",
    "    magnetic = magnetic_anomaly_sphere(x_profile, y_profile, base_R, base_M, h, base_I, base_D)\n",
    "    axes[1,1].plot(x_profile, magnetic, label=f'h={h}m')\n",
    "axes[1,1].set_xlabel('X (m)')\n",
    "axes[1,1].set_ylabel('磁异常 (nT)')\n",
    "axes[1,1].set_title('埋深对磁异常的影响')\n",
    "axes[1,1].legend()\n",
    "axes[1,1].grid(True, alpha=0.3)\n",
    "\n",
    "# 综合对比\n",
    "magnetic_base = magnetic_anomaly_sphere(x_profile, y_profile, base_R, base_M, base_h, base_I, base_D)\n",
    "axes[1,2].plot(x_profile, magnetic_base, 'k-', linewidth=2, label='基准参数')\n",
    "axes[1,2].set_xlabel('X (m)')\n",
    "axes[1,2].set_ylabel('磁异常 (nT)')\n",
    "axes[1,2].set_title('基准参数磁异常剖面')\n",
    "axes[1,2].legend()\n",
    "axes[1,2].grid(True, alpha=0.3)\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8. 总结\n",
    "\n",
    "本notebook实现了球体的重磁异常正演计算，主要特点包括：\n",
    "\n",
    "1. **理论基础完整**：包含了重磁异常正演的理论公式和参数说明\n",
    "2. **计算功能完善**：实现了球体重力异常和磁异常的数值计算\n",
    "3. **可视化丰富**：提供了等值线图、剖面图和3D图等多种可视化方式\n",
    "4. **交互性强**：支持通过滑块实时调整参数并查看结果变化\n",
    "5. **分析深入**：包含了参数敏感性分析，帮助理解各参数对异常的影响\n",
    "\n",
    "### 使用建议：\n",
    "- 对于重力异常，主要关注球体的密度、半径和埋深参数\n",
    "- 对于磁异常，除了几何参数外，磁化强度和方向参数也很重要\n",
    "- 可以通过交互式界面快速探索不同参数组合的效果\n",
    "- 参数敏感性分析有助于理解各参数的相对重要性\n",
    "\n",
    "### 注意事项：\n",
    "- 计算结果基于理论公式，实际应用中需要考虑地质复杂性\n",
    "- 网格分辨率会影响计算精度和显示效果\n",
    "- 3D可视化对计算资源要求较高，建议使用较小的网格"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (forward_modeling)",
   "language": "python",
   "name": "forward_modeling"
  },
  "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.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
