{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"\n",
"# Tutorial-IllinoisGRMHD: add_fluxes_and_source_terms_to_hydro_rhss.C\n",
"\n",
"## Authors: Leo Werneck & Zach Etienne\n",
"\n",
"**This module is currently under development**\n",
"\n",
"## This tutorial module demonstrates the addition of the flux and source terms to the right-hand side of the hydrodynamic variables. \n",
"\n",
"### Required and recommended citations:\n",
"\n",
"* **(Required)** Etienne, Z. B., Paschalidis, V., Haas R., Mösta P., and Shapiro, S. L. IllinoisGRMHD: an open-source, user-friendly GRMHD code for dynamical spacetimes. Class. Quantum Grav. 32 (2015) 175009. ([arxiv:1501.07276](http://arxiv.org/abs/1501.07276)).\n",
"* **(Required)** Noble, S. C., Gammie, C. F., McKinney, J. C., Del Zanna, L. Primitive Variable Solvers for Conservative General Relativistic Magnetohydrodynamics. Astrophysical Journal, 641, 626 (2006) ([astro-ph/0512420](https://arxiv.org/abs/astro-ph/0512420)).\n",
"* **(Recommended)** Del Zanna, L., Bucciantini N., Londrillo, P. An efficient shock-capturing central-type scheme for multidimensional relativistic flows - II. Magnetohydrodynamics. A&A 400 (2) 397-413 (2003). DOI: 10.1051/0004-6361:20021641 ([astro-ph/0210618](https://arxiv.org/abs/astro-ph/0210618))."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"# Table of Contents\n",
"$$\\label{toc}$$\n",
"\n",
"This module is organized as follows\n",
"\n",
"0. [Step 0](#src_dir): **Source directory creation**\n",
"1. [Step 1](#introduction): **Introduction**\n",
"1. [Step 2](#add_fluxes_and_source_terms_to_hydro_rhss__c): **`add_fluxes_and_source_terms_to_hydro_rhss.C`**\n",
"1. [Step n-1](#code_validation): **Code validation**\n",
"1. [Step n](#latex_pdf_output): **Output this notebook to $\\LaTeX$-formatted PDF file**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"# Step 0: Source directory creation \\[Back to [top](#toc)\\]\n",
"$$\\label{src_dir}$$\n",
"\n",
"We will now use the [cmdline_helper.py NRPy+ module](Tutorial-Tutorial-cmdline_helper.ipynb) to create the source directory within the `IllinoisGRMHD` NRPy+ directory, if it does not exist yet."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Step 0: Creation of the IllinoisGRMHD source directory\n",
"# Step 0a: Add NRPy's directory to the path\n",
"# https://stackoverflow.com/questions/16780014/import-file-from-parent-directory\n",
"import os,sys\n",
"nrpy_dir_path = os.path.join(\"..\",\"..\")\n",
"if nrpy_dir_path not in sys.path:\n",
" sys.path.append(nrpy_dir_path)\n",
"\n",
"# Step 0b: Load up cmdline_helper and create the directory\n",
"import cmdline_helper as cmd\n",
"IGM_src_dir_path = os.path.join(\"..\",\"src\")\n",
"cmd.mkdir(IGM_src_dir_path)\n",
"\n",
"# Step 0c: Create the output file path\n",
"outfile_path__add_fluxes_and_source_terms_to_hydro_rhss__C = os.path.join(IGM_src_dir_path,\"add_fluxes_and_source_terms_to_hydro_rhss.C\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"# Step 1: Introduction \\[Back to [top](#toc)\\]\n",
"$$\\label{introduction}$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"# Step 2: `add_fluxes_and_source_terms_to_hydro_rhss.C` \\[Back to [top](#toc)\\]\n",
"$$\\label{add_fluxes_and_source_terms_to_hydro_rhss__c}$$"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing ../src/add_fluxes_and_source_terms_to_hydro_rhss.C\n"
]
}
],
"source": [
"%%writefile $outfile_path__add_fluxes_and_source_terms_to_hydro_rhss__C\n",
"// Side note: the following values could be used for cell averaged gfs:\n",
"// am2=-1.0/12.0, am1=7.0/12.0, a0=7.0/12.0, a1=-1.0/12.0\n",
"// However, since the metric gfs store the grid point values instead of the cell average,\n",
"// the following coefficients should be used:\n",
"// am2 = -1/16, am1 = 9/16, a0 = 9/16, a1 = -1/16\n",
"// This will yield the third-order-accurate face values at m-1/2,\n",
"// using values specified at {m-2,m-1,m,m+1}\n",
"#define AM2 -0.0625\n",
"#define AM1 0.5625\n",
"#define A0 0.5625\n",
"#define A1 -0.0625\n",
"#define COMPUTE_FCVAL(METRICm2,METRICm1,METRIC,METRICp1) (AM2*(METRICm2) + AM1*(METRICm1) + A0*(METRIC) + A1*(METRICp1))\n",
"\n",
"static inline void mhdflux(int i,int j,int k,const int flux_dirn,CCTK_REAL *Ul,CCTK_REAL *Ur, CCTK_REAL *FACEVAL,CCTK_REAL *FACEVAL_LAPSE_PSI4,eos_struct &eos, CCTK_REAL Gamma_th,\n",
" CCTK_REAL &cmax,CCTK_REAL &cmin,\n",
" CCTK_REAL &rho_star_flux,CCTK_REAL &tau_flux,CCTK_REAL &st_x_flux,CCTK_REAL &st_y_flux,CCTK_REAL &st_z_flux);\n",
"\n",
"#define COMPUTE_FOURMETRIC(g4tt,g4tx,g4ty,g4tz,g4xx,g4xy,g4xz,g4yy,g4yz,g4zz,METRIC,METRIC_AUX) ( { \\\n",
" /* g_{0i} = beta_i */ \\\n",
" g4tx = METRIC_AUX[PSI4]*(METRIC[GXX]*METRIC[SHIFTX] + METRIC[GXY]*METRIC[SHIFTY] + METRIC[GXZ]*METRIC[SHIFTZ]); \\\n",
" g4ty = METRIC_AUX[PSI4]*(METRIC[GXY]*METRIC[SHIFTX] + METRIC[GYY]*METRIC[SHIFTY] + METRIC[GYZ]*METRIC[SHIFTZ]); \\\n",
" g4tz = METRIC_AUX[PSI4]*(METRIC[GXZ]*METRIC[SHIFTX] + METRIC[GYZ]*METRIC[SHIFTY] + METRIC[GZZ]*METRIC[SHIFTZ]); \\\n",
" /* g_{00} = -alpha^2 + beta^i beta^j gamma_{ij} = -alpha^2 + beta^i beta_i = -alpha^2 + beta^i g_{0i} */ \\\n",
" g4tt = -SQR(METRIC_AUX[LAPSE]) + g4tx*METRIC[SHIFTX] + g4ty*METRIC[SHIFTY] + g4tz*METRIC[SHIFTZ]; \\\n",
" g4xx = METRIC_AUX[PSI4]*METRIC[GXX]; \\\n",
" g4xy = METRIC_AUX[PSI4]*METRIC[GXY]; \\\n",
" g4xz = METRIC_AUX[PSI4]*METRIC[GXZ]; \\\n",
" g4yy = METRIC_AUX[PSI4]*METRIC[GYY]; \\\n",
" g4yz = METRIC_AUX[PSI4]*METRIC[GYZ]; \\\n",
" g4zz = METRIC_AUX[PSI4]*METRIC[GZZ]; \\\n",
" } )\n",
"\n",
"\n",
"static void add_fluxes_and_source_terms_to_hydro_rhss(const int flux_dirn,const cGH *cctkGH,const int *cctk_lsh,const int *cctk_nghostzones,CCTK_REAL *dX,\n",
" CCTK_REAL **metric,gf_and_gz_struct *in_prims,CCTK_REAL **TUPmunu,\n",
" int numvars_reconstructed,gf_and_gz_struct *out_prims_r,gf_and_gz_struct *out_prims_l,eos_struct &eos,\n",
" CCTK_REAL *cmax,CCTK_REAL *cmin,\n",
" CCTK_REAL *rho_star_flux,CCTK_REAL *tau_flux,CCTK_REAL *st_x_flux,CCTK_REAL *st_y_flux,CCTK_REAL *st_z_flux,\n",
" CCTK_REAL *rho_star_rhs,CCTK_REAL *tau_rhs,CCTK_REAL *st_x_rhs,CCTK_REAL *st_y_rhs,CCTK_REAL *st_z_rhs) {\n",
"\n",
" DECLARE_CCTK_PARAMETERS;\n",
"\n",
" CCTK_REAL dxi[4] = { 1e100,1.0/dX[0],1.0/dX[1],1.0/dX[2] };\n",
"\n",
" // Notice in the loop below that we go from 3 to cctk_lsh-2 for i, j, AND k, even though\n",
" // we are only computing the flux in one direction at a time. This is because in the end,\n",
" // we only need the rhs's from 3 to cctk_lsh-3 for i, j, and k.\n",
"#pragma omp parallel for\n",
" for(int k=cctk_nghostzones[2];k\n",
"\n",
"# Step n-1: Code validation \\[Back to [top](#toc)\\]\n",
"$$\\label{code_validation}$$\n",
"\n",
"First, we download the original `IllinoisGRMHD` source code and then compare it to the source code generated by this tutorial notebook."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Validation test for add_fluxes_and_source_terms_to_hydro_rhss.C: FAILED!\n",
"Diff:\n",
"1c1\n",
"< // Side note: the following values could be used for cell averaged gfs: \n",
"---\n",
"> // Side note: the following values could be used for cell averaged gfs:\n",
"3,4c3,4\n",
"< // However, since the metric gfs store the grid point values instead of the cell average, \n",
"< // the following coefficients should be used: \n",
"---\n",
"> // However, since the metric gfs store the grid point values instead of the cell average,\n",
"> // the following coefficients should be used:\n",
"6c6\n",
"< // This will yield the third-order-accurate face values at m-1/2, \n",
"---\n",
"> // This will yield the third-order-accurate face values at m-1/2,\n",
"14c14\n",
"< static inline void mhdflux(int i,int j,int k,const int flux_dirn,CCTK_REAL *Ul,CCTK_REAL *Ur, CCTK_REAL *FACEVAL,CCTK_REAL *FACEVAL_LAPSE_PSI4,eos_struct &eos,\n",
"---\n",
"> static inline void mhdflux(int i,int j,int k,const int flux_dirn,CCTK_REAL *Ul,CCTK_REAL *Ur, CCTK_REAL *FACEVAL,CCTK_REAL *FACEVAL_LAPSE_PSI4,eos_struct &eos, CCTK_REAL Gamma_th,\n",
"85c85\n",
"< \tmhdflux(i,j,k,flux_dirn,Ul ,Ur ,FACEVAL ,FACEVAL_LAPSE_PSI4 ,eos, cmax[index],cmin[index],\n",
"---\n",
"> \tmhdflux(i,j,k,flux_dirn,Ul ,Ur ,FACEVAL ,FACEVAL_LAPSE_PSI4 ,eos, Gamma_th, cmax[index],cmin[index],\n",
"88c88\n",
"< \n",
"---\n",
"> \n",
"96c96\n",
"< \t CCTK_REAL half_alpha_sqrtgamma = 0.5*METRIC_LAP_PSI4[LAPSE]*Psi6; \n",
"---\n",
"> \t CCTK_REAL half_alpha_sqrtgamma = 0.5*METRIC_LAP_PSI4[LAPSE]*Psi6;\n",
"101c101\n",
"< \n",
"---\n",
"> \n",
"126,127c126,127\n",
"< \t st_i_curvature_terms[flux_dirn] = half_alpha_sqrtgamma * ( TUP[0][0]*partial_i_gmunu[0][0] + \n",
"< \t\t\t\t\t\t\t\t TUP[1][1]*partial_i_gmunu[1][1] + \n",
"---\n",
"> \t st_i_curvature_terms[flux_dirn] = half_alpha_sqrtgamma * ( TUP[0][0]*partial_i_gmunu[0][0] +\n",
"> \t\t\t\t\t\t\t\t TUP[1][1]*partial_i_gmunu[1][1] +\n",
"130,131c130,131\n",
"< \t\t\t\t\t\t\t\t 2.0*(TUP[0][1]*partial_i_gmunu[0][1] + \n",
"< \t\t\t\t\t\t\t\t\t TUP[0][2]*partial_i_gmunu[0][2] + \n",
"---\n",
"> \t\t\t\t\t\t\t\t 2.0*(TUP[0][1]*partial_i_gmunu[0][1] +\n",
"> \t\t\t\t\t\t\t\t\t TUP[0][2]*partial_i_gmunu[0][2] +\n",
"133,134c133,134\n",
"< \t\t\t\t\t\t\t\t\t TUP[1][2]*partial_i_gmunu[1][2] + \n",
"< \t\t\t\t\t\t\t\t\t TUP[1][3]*partial_i_gmunu[1][3] + \n",
"---\n",
"> \t\t\t\t\t\t\t\t\t TUP[1][2]*partial_i_gmunu[1][2] +\n",
"> \t\t\t\t\t\t\t\t\t TUP[1][3]*partial_i_gmunu[1][3] +\n",
"140c140\n",
"< \t tau_rhs[index] += alpha_sqrtgamma*(-(TUP[0][0]*METRIC[SHIFTX+(flux_dirn-1)] + TUP[0][flux_dirn])*lapse_deriv[flux_dirn]); \n",
"---\n",
"> \t tau_rhs[index] += alpha_sqrtgamma*(-(TUP[0][0]*METRIC[SHIFTX+(flux_dirn-1)] + TUP[0][flux_dirn])*lapse_deriv[flux_dirn]);\n",
"166a167\n",
"> \n"
]
}
],
"source": [
"# Verify if the code generated by this tutorial module\n",
"# matches the original IllinoisGRMHD source code\n",
"\n",
"# First download the original IllinoisGRMHD source code\n",
"import urllib\n",
"from os import path\n",
"\n",
"original_IGM_file_url = \"https://bitbucket.org/zach_etienne/wvuthorns/raw/5611b2f0b17135538c9d9d17c7da062abe0401b6/IllinoisGRMHD/src/add_fluxes_and_source_terms_to_hydro_rhss.C\"\n",
"original_IGM_file_name = \"add_fluxes_and_source_terms_to_hydro_rhss-original.C\"\n",
"original_IGM_file_path = os.path.join(IGM_src_dir_path,original_IGM_file_name)\n",
"\n",
"# Then download the original IllinoisGRMHD source code\n",
"# We try it here in a couple of ways in an attempt to keep\n",
"# the code more portable\n",
"try:\n",
" original_IGM_file_code = urllib.request.urlopen(original_IGM_file_url).read().decode(\"utf-8\")\n",
" # Write down the file the original IllinoisGRMHD source code\n",
" with open(original_IGM_file_path,\"w\") as file:\n",
" file.write(original_IGM_file_code)\n",
"except:\n",
" try:\n",
" original_IGM_file_code = urllib.urlopen(original_IGM_file_url).read().decode(\"utf-8\")\n",
" # Write down the file the original IllinoisGRMHD source code\n",
" with open(original_IGM_file_path,\"w\") as file:\n",
" file.write(original_IGM_file_code)\n",
" except:\n",
" # If all else fails, hope wget does the job\n",
" !wget -O $original_IGM_file_path $original_IGM_file_url\n",
"\n",
"# Perform validation\n",
"Validation__add_fluxes_and_source_terms_to_hydro_rhss__C = !diff $original_IGM_file_path $outfile_path__add_fluxes_and_source_terms_to_hydro_rhss__C\n",
"\n",
"if Validation__add_fluxes_and_source_terms_to_hydro_rhss__C == []:\n",
" # If the validation passes, we do not need to store the original IGM source code file\n",
" !rm $original_IGM_file_path\n",
" print(\"Validation test for add_fluxes_and_source_terms_to_hydro_rhss.C: PASSED!\")\n",
"else:\n",
" # If the validation fails, we keep the original IGM source code file\n",
" print(\"Validation test for add_fluxes_and_source_terms_to_hydro_rhss.C: FAILED!\")\n",
" # We also print out the difference between the code generated\n",
" # in this tutorial module and the original IGM source code\n",
" print(\"Diff:\")\n",
" for diff_line in Validation__add_fluxes_and_source_terms_to_hydro_rhss__C:\n",
" print(diff_line)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"# Step n: Output this notebook to $\\LaTeX$-formatted PDF file \\[Back to [top](#toc)\\]\n",
"$$\\label{latex_pdf_output}$$\n",
"\n",
"The following code cell converts this Jupyter notebook into a proper, clickable $\\LaTeX$-formatted PDF file. After the cell is successfully run, the generated PDF may be found in the root NRPy+ tutorial directory, with filename\n",
"[Tutorial-IllinoisGRMHD__add_fluxes_and_source_terms_to_hydro_rhss.pdf](Tutorial-IllinoisGRMHD__add_fluxes_and_source_terms_to_hydro_rhss.pdf) (Note that clicking on this link may not work; you may need to open the PDF file through another means)."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"latex_nrpy_style_path = os.path.join(nrpy_dir_path,\"latex_nrpy_style.tplx\")\n",
"#!jupyter nbconvert --to latex --template $latex_nrpy_style_path --log-level='WARN' Tutorial-IllinoisGRMHD__add_fluxes_and_source_terms_to_hydro_rhss.ipynb\n",
"#!pdflatex -interaction=batchmode Tutorial-IllinoisGRMHD__add_fluxes_and_source_terms_to_hydro_rhss.tex\n",
"#!pdflatex -interaction=batchmode Tutorial-IllinoisGRMHD__add_fluxes_and_source_terms_to_hydro_rhss.tex\n",
"#!pdflatex -interaction=batchmode Tutorial-IllinoisGRMHD__add_fluxes_and_source_terms_to_hydro_rhss.tex\n",
"!rm -f Tut*.out Tut*.aux Tut*.log"
]
}
],
"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.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 4
}