Skip to content

Instantly share code, notes, and snippets.

@MiCurry
Last active March 26, 2026 19:35
Show Gist options
  • Select an option

  • Save MiCurry/caa73aca50a681bbf28eab30a534780f to your computer and use it in GitHub Desktop.

Select an option

Save MiCurry/caa73aca50a681bbf28eab30a534780f to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "fbb0df54-4f36-451a-8eb2-11e86f8dd539",
"metadata": {},
"source": [
"# ESoln Manager Savings\n",
"\n",
"The ESolnManager essentially saves us one `solnVectorMTX` type in ModEM.\n",
"\n",
"Below we have functions to calculate the total number of savings in bytes. We\n",
"first calulate the size of the `cVector`, then the `solnVector`, and finally\n",
"the `solnVectorMTX`.\n"
]
},
{
"cell_type": "markdown",
"id": "645968e2-38dd-42ec-a603-f386a8157043",
"metadata": {},
"source": [
"# cvector Size\n",
"The [cvector](https://github.com/magnetotellurics/ModEM/blob/225e798bab967e0dbd8ab306159160484f8fe9af/f90/FIELDS/FiniteDiff3D/sg_vector.f90#L174-L206) is defined in sg_vector and is mostly defined by three complex 3-Dimensional arrays: `X`, `Y`, `Z`.\n",
"\n",
"For `EDGE` grids, the dimension of X, Y and Z are:\n",
"\n",
"```\n",
"x : nx, ny + 1, nz + 1\n",
"y : nx + 1, ny, nz + 1\n",
"z : nx + 1, ny + 1, nz\n",
"```\n",
"\n",
"ModEM uses 8 byte size reals, because this is a complex variable, there will be\n",
"two 8 byte real values, so a single element of the array is 16 bytes in total.\n",
"\n",
"Thus, the total size of a `cVector` is: \n",
"\n",
"```\n",
"cvector_bytes = (len(x) + len(y) + len(z)) * (real_size_bytes * nComplex)\n",
"```\n",
"\n",
"We perform the calulation below:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9375aa0e-f9d1-4f25-9efe-bfd093ae666a",
"metadata": {},
"outputs": [],
"source": [
"# Calulate the size in \n",
"def cvector_size_bytes(nx: int, ny: int, nz: int,\n",
" nComplex=2,\n",
" real_size_bytes=8) -> int:\n",
" x = nx * (ny + 1) * (nz + 1)\n",
" y = (nx + 1) * ny * (nz + 1)\n",
" z = (nx + 1) * (ny + 1) * nz\n",
" \n",
" xBytes = x * (real_size_bytes * nComplex)\n",
" yBytes = y * (real_size_bytes * nComplex)\n",
" zBytes = z * (real_size_bytes * nComplex)\n",
"\n",
" cVectorBytes = (xBytes + yBytes + zBytes)\n",
" return cVectorBytes\n"
]
},
{
"cell_type": "markdown",
"id": "dde17305-4e7e-420d-91d2-9624f69a1d63",
"metadata": {},
"source": [
"## SolnVector Size\n",
"\n",
"The [solnVector](https://github.com/magnetotellurics/ModEM/blob/225e798bab967e0dbd8ab306159160484f8fe9af/f90/3D_MT/SolnSpace.f90#L49-L84) is deinfed in SolnSpace.f90 and is comprised of `nPol` `cvectors` called `Pol`, one for each polarity of a transmitter.\n",
"\n",
"Thus, it's size in bytes is: `cvector_size_bytes * nPol`. We calculate this below:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "cf7c63e0-86bd-438e-805f-90c9ee2a92f4",
"metadata": {},
"outputs": [],
"source": [
"def solnVector_size_bytes(nx, ny, nz,\n",
" nPol=2,\n",
" nComplex=2,\n",
" real_size_bytes=8):\n",
"\n",
" cVectorBytes = cvector_size_bytes(nx, ny, nz, nComplex=nComplex, real_size_bytes=real_size_bytes)\n",
" solnVectorBytes = cVectorBytes * nPol\n",
" return solnVectorBytes, cVectorBytes"
]
},
{
"cell_type": "markdown",
"id": "fa21958a-13b8-48b6-9202-e7a462b3b3b5",
"metadata": {},
"source": [
"## SolnVectorMTX\n",
"\n",
"Lastly, we have the\n",
"[SolnVectorMTX](https://github.com/magnetotellurics/ModEM/blob/225e798bab967e0dbd8ab306159160484f8fe9af/f90/3D_MT/SolnSpace.f90#L86-L92),\n",
"also defined in SolnSpace.f90. The SolnVectorMTX defines the collection of `nTx`\n",
"SolnVectors, one for each period/transmitter. \n",
"\n",
"Thus, the total size of a `SolnVectorMTX` is: `solnVector_size_bytes * nTx`. We\n",
"calculate this below:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "af9996db-103f-4162-be0a-6de7c5a4b23f",
"metadata": {},
"outputs": [],
"source": [
"def solnVectorMTX_size_bytes(nx, ny, nz, nTx, nPol=2, nComplex=2, real_size_bytes=8):\n",
" solnVectorBytes, cVectorBytes = solnVector_size_bytes(nx, ny, nz, \n",
" nPol=nPol, \n",
" nComplex=nComplex,\n",
" real_size_bytes=real_size_bytes)\n",
"\n",
" solnVectorMTXBytes = solnVectorBytes * nTx\n",
" return solnVectorMTXBytes, solnVectorBytes, cVectorBytes"
]
},
{
"cell_type": "markdown",
"id": "55f2ba38-1945-4680-abe2-1399dc443f3c",
"metadata": {},
"source": [
"We also define some helpful functions below:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "e6fc4a4b-db3c-45e6-9f1a-d821c473fb65",
"metadata": {},
"outputs": [],
"source": [
"def bytes_to_mb(bytes) -> float:\n",
" return (float(bytes) / (1024**2))\n",
"\n",
"def formated_str(label, val):\n",
" return f\"{label:<30} {val:>10} MB\"\n",
"\n",
"def determine_and_print_savings(nx, ny, nz, nTx, nPol=2, nComplex=2, real_size_bytes=8): \n",
" width=30\n",
" solnVectorMTXBytes, solnVectorBytes, cVectorBytes = solnVectorMTX_size_bytes(nx, ny, nz, nTx, nPol)\n",
" label = \"- solnVector Size (MB):\"\n",
" val = f\"{bytes_to_mb(solnVectorBytes):,.2f}\"\n",
" print(formated_str(label, val))\n",
"\n",
" label = \"- cVector Size (MB):\"\n",
" val = f\"{bytes_to_mb(cVectorBytes):,.2f}\"\n",
" print(formated_str(label, val))\n",
" \n",
" label = \"Total Savings (MB):\"\n",
" val = f\"{bytes_to_mb(solnVectorMTXBytes):,.2f}\"\n",
" print(formated_str(label, val))"
]
},
{
"cell_type": "markdown",
"id": "04f65b79-02d7-4e11-b504-0856b51d89ae",
"metadata": {},
"source": [
"# Cases\n",
"\n",
"## Cascadia Case\n",
"\n",
"Now lets check some real world problems and see their sizes. The Cascadia case\n",
"is defined and the total size saved is shown below."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c6692209-fc8a-4594-aca3-43f0fa3b0661",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Cascadia Case - Nx: 48 - Ny: 46 - Nz: 34 - nTx: 20 - nPol: 2\n",
"- solnVector Size (MB): 7.21 MB\n",
"- cVector Size (MB): 3.60 MB\n",
"Total Savings (MB): 144.14 MB\n"
]
}
],
"source": [
"nx = 48\n",
"ny = 46\n",
"nz = 34\n",
"nTx = 20\n",
"nPol = 2\n",
"\n",
"print(f\"Cascadia Case - Nx: {nx} - Ny: {ny} - Nz: {nz} - nTx: {nTx} - nPol: {nPol}\")\n",
"determine_and_print_savings(nx, ny, nz, nTx, nPol)"
]
},
{
"cell_type": "markdown",
"id": "fe3fb8c5-6bab-4dee-9700-116b575ccbff",
"metadata": {},
"source": [
"## USMT Array - 0.25x0.25\n",
"\n",
"The first grid of the USMT array grid is a 0.25 degree by 0.25 degree grid. The\n",
"USMT case has two datafiles, one with 2 total periods, and one with 15 total\n",
"periods.\n",
"\n",
"We calulate the total size of the USMT 0.25x0.25 array for both data files (2 periods and 15 periods):"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "5311c7a6-64b0-4141-a292-a1b160895e69",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"USMT Array Case 0.25x0.25 - Nx: 136 - Ny: 284 - Nz: 46 - nTx: 2 - nPol: 2\n",
"- solnVector Size (MB): 166.21 MB\n",
"- cVector Size (MB): 83.11 MB\n",
"Total Savings (MB): 332.43 MB\n",
"\n",
"USMT Array Case 0.25x0.25 - Nx: 136 - Ny: 284 - Nz: 46 - nTx: 15 - nPol: 2\n",
"- solnVector Size (MB): 166.21 MB\n",
"- cVector Size (MB): 83.11 MB\n",
"Total Savings (MB): 2,493.19 MB\n"
]
}
],
"source": [
"# USMT Array Case 0.25 x 0.25\n",
"nx = 136\n",
"ny = 284\n",
"nz = 46\n",
"nTx = 2\n",
"nPol = 2\n",
"\n",
"print(f\"USMT Array Case 0.25x0.25 - Nx: {nx} - Ny: {ny} - Nz: {nz} - nTx: {nTx} - nPol: {nPol}\")\n",
"determine_and_print_savings(nx, ny, nz, nTx, nPol)\n",
"print(\"\")\n",
"\n",
"nTx = 15 \n",
"print(f\"USMT Array Case 0.25x0.25 - Nx: {nx} - Ny: {ny} - Nz: {nz} - nTx: {nTx} - nPol: {nPol}\")\n",
"determine_and_print_savings(nx, ny, nz, nTx, nPol)"
]
},
{
"cell_type": "markdown",
"id": "f64e3cb3-b451-4362-8bf5-01bffc4c502f",
"metadata": {},
"source": [
"## USMT Array Case 0.1x0.1\n",
"\n",
"The USMT array has a 0.1 degree by 0.1 degree and is defined by the dimension below. Similar to the 0.25x0.25 case we calulate the total size saved for 2 periods and 15 periods:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "a0203448-f1e4-4f6b-a85f-3dd931a69eca",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"USMT Array Case 0.1x0.1 - Nx: 304 - Ny: 674 - Nz: 47 - nTx: 2 - nPol: 2\n",
"- solnVector Size (MB): 897.01 MB\n",
"- cVector Size (MB): 448.50 MB\n",
"Total Savings (MB): 1,794.01 MB\n",
"\n",
"USMT Array Case 0.1x0.1 - Nx: 304 - Ny: 674 - Nz: 47 - nTx: 15 - nPol: 2\n",
"- solnVector Size (MB): 897.01 MB\n",
"- cVector Size (MB): 448.50 MB\n",
"Total Savings (MB): 13,455.09 MB\n"
]
}
],
"source": [
"# USMT Array Case 0.1 x 0.1\n",
"nx = 304\n",
"ny = 674\n",
"nz = 47\n",
"nTx = 2\n",
"nPol = 2\n",
"\n",
"print(f\"USMT Array Case 0.1x0.1 - Nx: {nx} - Ny: {ny} - Nz: {nz} - nTx: {nTx} - nPol: {nPol}\")\n",
"determine_and_print_savings(nx, ny, nz, nTx, nPol)\n",
"print(\"\")\n",
"\n",
"nTx = 15\n",
"print(f\"USMT Array Case 0.1x0.1 - Nx: {nx} - Ny: {ny} - Nz: {nz} - nTx: {nTx} - nPol: {nPol}\")\n",
"determine_and_print_savings(nx, ny, nz, nTx, nPol)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "06d8ca88-af8a-4163-953e-33c03ce3e0ca",
"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.14.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment