Skip to content

Commit

Permalink
Merge pull request #26 from AFD-Illinois/feature/adaptive-res
Browse files Browse the repository at this point in the history
Preliminary adaptive resolution
  • Loading branch information
Ben Prather authored May 15, 2020
2 parents 0d61df0 + 03a6518 commit 9436c6f
Show file tree
Hide file tree
Showing 17 changed files with 723 additions and 316 deletions.
30 changes: 30 additions & 0 deletions example_refine.par
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ipole parameter file demonstrating refinement

# Max refinement level pixels in x,y dimension
nx 1280
ny 1280
# Min refinement level pixels in x,y
nx_min 160
ny_min 160
# Both the following must be met to refine:
# Relative difference must be greater than
refine_rel 0.1
# Flux of the pixel must be greater than this portion of average
refine_cut 0.001

dump tests/test-resources/sample_dump_SANE_a+0.94_0900.h5

rcam 1.e4
thetacam 163
phicam 0

fovx_dsource 160
fovy_dsource 160

freqcgs 230.e9
trat_small 3
trat_large 3

MBH 6.2e9
M_unit 3.25e26
outfile image.h5
11 changes: 11 additions & 0 deletions model/iharm/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,18 @@ void output_hdf5()
hdf5_write_single_val(&beta_crit, "beta_crit", H5T_IEEE_F64LE);
}
hdf5_write_single_val(&ELECTRONS, "type", H5T_STD_I32LE);

hdf5_set_directory("/header/");
hdf5_make_directory("units");
hdf5_set_directory("/header/units/");
hdf5_write_single_val(&L_unit, "L_unit", H5T_IEEE_F64LE);
hdf5_write_single_val(&M_unit, "M_unit", H5T_IEEE_F64LE);
hdf5_write_single_val(&T_unit, "T_unit", H5T_IEEE_F64LE);
hdf5_write_single_val(&Te_unit, "Thetae_unit", H5T_IEEE_F64LE);

hdf5_set_directory("/");

//fprintf(stderr, "Wrote model header\n");
}

void load_iharm_data(int n, char *fnam, int dumpidx, int verbose)
Expand Down
2 changes: 1 addition & 1 deletion scripts/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def dssim(imageI, imageK):
print("Diff EVPA [deg]: {:g}".format(evpatot2 - evpatot1))

# Return code for automated testing. Adjust stringency to taste
if rdiffI > 0.01 or rdiffQ > 0.01 or rdiffU > 0.01 or rdiffV > 0.01:
if mseI > 0.01 or mseQ > 0.01 or mseU > 0.01 or mseV > 0.01:
exit(1)
else:
exit(0)
1 change: 1 addition & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define EV (1.60217653e-12 ) /* electron volt in erg */
#define SIGMA_THOMSON (0.665245873e-24 ) /* Thomson cross section in cm^2 */
#define JY (1.e-23 ) /* Jansky (flux/freq. unit) in cgs */
#define MUAS_PER_RAD (2.06265e11 ) /* Micro-arcseconds in 1 radian */

#define ALPHAF (7.29735e-3 ) /* (fine-structure constant)^-1 */
#define KEV (1.602e-9 ) /*kiloelectronovolt*/
Expand Down
25 changes: 19 additions & 6 deletions src/coordinates.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,21 @@ void vec_from_ks(double X[NDIM], double v_ks[NDIM], double v_nat[NDIM]) {
MUNULOOP v_nat[mu] += dXdx[mu][nu] * v_ks[nu];
}

// Root-find the camera x2 location in native coordinates
double root_find(double X[NDIM], double startx2, double stopx2)
/*
* Translate the input camera angles into a canonical Xcam in native coordinates
*/
void native_coord(double r, double th, double phi, double X[NDIM]) {
double x[NDIM] = {0., r, th/180.*M_PI, phi/180.*M_PI};
X[0] = 0.0;
X[1] = log(r);
X[2] = root_find(x);
X[3] = phi/180.*M_PI;
}

/*
* Root-find the camera theta in native coordinates
*/
double root_find(double X[NDIM])
{
double th = X[2];
double tha, thb, thc;
Expand All @@ -285,11 +298,11 @@ double root_find(double X[NDIM], double startx2, double stopx2)
Xc[3] = Xa[3];

if (X[2] < M_PI / 2.) {
Xa[2] = startx2;
Xb[2] = (stopx2 - startx2)/2 + SMALL;
Xa[2] = startx[2];
Xb[2] = (stopx[2] - startx[2])/2 + SMALL;
} else {
Xa[2] = (stopx2 - startx2)/2 - SMALL;
Xb[2] = stopx2;
Xa[2] = (stopx[2] - startx[2])/2 - SMALL;
Xb[2] = stopx[2];
}

double tol = 1.e-9;
Expand Down
4 changes: 3 additions & 1 deletion src/coordinates.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ void set_dXdx(double X[NDIM], double dXdx[NDIM][NDIM]);
void vec_to_ks(double X[NDIM], double v_nat[NDIM], double v_ks[NDIM]);
void vec_from_ks(double X[NDIM], double v_ks[NDIM], double v_nat[NDIM]);

double root_find(double X[NDIM], double startx2, double stopx2);
// Translation to native coords
void native_coord(double r, double th, double phi, double X[NDIM]);
double root_find(double X[NDIM]);

#endif // COORDINATES_H
8 changes: 1 addition & 7 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,7 @@ void write_header(double scale, double cam[NDIM],
hsize_t vec_dim[1] = {NDIM};
hdf5_write_full_array(cam, "x", 1, vec_dim, H5T_IEEE_F64LE);

hdf5_set_directory("/header/");
hdf5_make_directory("units");
hdf5_set_directory("/header/units/");
hdf5_write_single_val(&L_unit, "L_unit", H5T_IEEE_F64LE);
hdf5_write_single_val(&M_unit, "M_unit", H5T_IEEE_F64LE);
hdf5_write_single_val(&T_unit, "T_unit", H5T_IEEE_F64LE);
hdf5_write_single_val(&Te_unit, "Thetae_unit", H5T_IEEE_F64LE);
//fprintf(stderr, "Wrote header\n");

// allow model to output its parameters
output_hdf5();
Expand Down
119 changes: 107 additions & 12 deletions src/ipolarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
/****************and then rewritten by C.Gammie ***********/
/**********************************************************/

#include "ipolarray.h"

#include "decs.h"
#include "coordinates.h"
#include "geometry.h"
Expand All @@ -17,7 +19,7 @@
#include "debug_tools.h"
#include <complex.h>

/* tensor tools*/
/* tensor tools */
void complex_lower(double complex N[NDIM][NDIM], double gcov[NDIM][NDIM],
int low1, int low2, double complex Nl[NDIM][NDIM]);
void stokes_to_tensor(double fI, double fQ, double fU, double fV,
Expand All @@ -31,9 +33,75 @@ void complex_tetrad_to_coord_rank2(double complex T_tetrad[NDIM][NDIM],
double Econ[NDIM][NDIM],
double complex T_coord[NDIM][NDIM]);



/************************PRIMARY FUNCTION*******************************/
void integrate_emission(struct of_traj *traj, int nstep, int only_unpolarized,
double *Intensity, double *Tau, double *tauF,
double complex N_coord[NDIM][NDIM]) {
double Xi[NDIM], Xhalf[NDIM], Xf[NDIM];
double Kconi[NDIM], Kconhalf[NDIM], Kconf[NDIM];

// Initialize location
MULOOP {
Xi[mu] = traj[nstep].X[mu];
Kconi[mu] = traj[nstep].Kcon[mu];
}
// Initialize emission
init_N(Xi, Kconi, N_coord);
*Intensity = 0.;
*Tau = 0.;
*tauF = 0.;
double ji, ki;
get_jkinv(Xi, Kconi, &ji, &ki);

// integrate forwards along trajectory, including radiative transfer equation
// initialize X, K
while (nstep > 1) {
// initialize X,K
MULOOP {
Xi[mu] = traj[nstep].X[mu];
Kconi[mu] = traj[nstep].Kcon[mu];
Xhalf[mu] = traj[nstep].Xhalf[mu];
Kconhalf[mu] = traj[nstep].Kconhalf[mu];
Xf[mu] = traj[nstep - 1].X[mu];
Kconf[mu] = traj[nstep - 1].Kcon[mu];
}

#if THIN_DISK
if (thindisk_region(Xi, Xf)) {
get_model_i(Xf, Kconf, Intensity);
} else {
*Intensity = 0.;
}
#else
// solve total intensity equation alone
double jf, kf;
get_jkinv(Xf, Kconf, &jf, &kf);
*Intensity = approximate_solve(*Intensity, ji, ki, jf, kf, traj[nstep].dl, Tau);
// swap start and finish
ji = jf;
ki = kf;
#endif

// solve polarized transport
if (!only_unpolarized) {
evolve_N(Xi, Kconi, Xhalf, Kconhalf, Xf, Kconf, traj[nstep].dl, N_coord, tauF);
if (isnan(creal(N_coord[0][0]))) {
printf("NaN in N00!\n");
exit(-3);
}
}

nstep--;
}
}

/***************************MAIN FUNCTIONS******************************/
/* initialize tensor N in the coordinate frame at the beginning of the *
geodesics integration = it is zero */
/*
* initialize tensor N in the coordinate frame at the beginning of the
* geodesics integration = it is zero
*/
void init_N(double X[NDIM], double Kcon[NDIM],
double complex N_coord[NDIM][NDIM])
{
Expand All @@ -42,9 +110,7 @@ void init_N(double X[NDIM], double Kcon[NDIM],
}

/*
parallel transport N over dl
* parallel transport N over dl
*/
void push_polar(double Xi[NDIM], double Xm[NDIM], double Xf[NDIM],
double Ki[NDIM], double Km[NDIM], double Kf[NDIM],
Expand Down Expand Up @@ -74,11 +140,12 @@ void push_polar(double Xi[NDIM], double Xm[NDIM], double Xf[NDIM],
return;
}

/* updates N for one step on geodesics, using the previous step N*/
/* here we compute new right-hand side of the equation */
/* and somehow rotate this along the geodesics knowing */
/* first point and last point X and K*/

/*
* Updates N for one step on geodesics, using the previous step N
* here we compute new right-hand side of the equation
* and somehow rotate this along the geodesics knowing
* first point and last point X and K
*/
void evolve_N(double Xi[NDIM], double Kconi[NDIM],
double Xhalf[NDIM], double Kconhalf[NDIM],
double Xf[NDIM], double Kconf[NDIM],
Expand Down Expand Up @@ -315,7 +382,35 @@ void project_N(double X[NDIM], double Kcon[NDIM],

}

/***************************END MAIN FUNCTIONS******************************/
/*
* must be a stable, approximate solution to radiative transfer
* that runs between points w/ initial intensity I, emissivity
* ji, opacity ki, and ends with emissivity jf, opacity kf.
*
* return final intensity
*/
double approximate_solve(double Ii, double ji, double ki, double jf,
double kf, double dl, double *tau)
{
double efac, If, javg, kavg, dtau;

javg = (ji + jf) / 2.;
kavg = (ki + kf) / 2.;

dtau = dl * kavg;
*tau += dtau;

if (dtau < 1.e-3) {
If = Ii + (javg - Ii * kavg) * dl * (1. -
(dtau / 2.) * (1. -
dtau / 3.));
} else {
efac = exp(-dtau);
If = Ii * efac + (javg / kavg) * (1. - efac);
}

return If;
}

/*************************SUPPORTING FUNCTIONS******************************/

Expand Down
7 changes: 7 additions & 0 deletions src/ipolarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

#include <complex.h>

// Top-level functions for solving emission
void integrate_emission(struct of_traj *traj, int nstep, int only_unpolarized,
double *Intensity, double *Tau, double *tauF,
double complex N_coord[NDIM][NDIM]);
double approximate_solve (double Ii, double ji, double ki, double jf, double kf,
double dl, double *tau);

void init_N(double Xi[NDIM],double Kconi[NDIM],double complex Ncon[NDIM][NDIM]);
void evolve_N(double Xi[NDIM],double Kconi[NDIM],
double Xf[NDIM],double Kconf[NDIM],
Expand Down
Loading

0 comments on commit 9436c6f

Please sign in to comment.