Skip to content

Commit

Permalink
support for single-precision floating point for fields array functions (
Browse files Browse the repository at this point in the history
NanoComp#1833)

* switch dft-related functions to using realnum from double

* more fixes

* more type conversions from double to realnum

* adjust check tolerance of tests/integrate.cpp based on floating-point precision

* more fixes

* rebase from master from fix merge conflicts

* slight adjustment to tolerances in unit tests and update docs

* remove type check in test_adjoint_solver.py

* revert return types of integration functions to double

* revert return type of process_dft_component to double

* cleanup
  • Loading branch information
oskooi authored and Mo Chen committed Feb 16, 2022
1 parent 67cc1dc commit 8b28cf8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/meepgeom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2714,7 +2714,7 @@ std::complex<double> get_material_gradient(

for (int i=0;i<3;i++) dA_du[i] = (row_1[i] - row_2[i])/(2*du);
return dA_du[dir_idx] * fields_f * cond_cmp(forward_c,r,freq,geps);
}
}
}

/* A brute force approach to calculating Aᵤ using finite differences.
Expand Down Expand Up @@ -2933,6 +2933,7 @@ void material_grids_addgradient(double *v, size_t ng, std::complex<meep::realnum
size_t idx_fields = IVEC_LOOP_COUNTER;
meep::ivec ip = gv.iloc(adjoint_c,idx);
meep::vec p = gv.loc(adjoint_c,idx);
<<<<<<< HEAD
<<<<<<< HEAD
std::complex<double> adj = GET_FIELDS(fields_a, ci_adjoint, f_i, idx_fields);
material_type md;
Expand All @@ -2946,6 +2947,12 @@ void material_grids_addgradient(double *v, size_t ng, std::complex<meep::realnum
if (!md->trivial) adj *= cond_cmp(adjoint_c,p,frequencies[f_i], geps);

>>>>>>> Fix adjoint gradient with conductivities (#1830)
=======
std::complex<double> adj = GET_FIELDS(fields_a, ci_adjoint, f_i, idx_fields);
material_type md;
geps->get_material_pt(md, p);
if (!md->trivial) adj *= cond_cmp(adjoint_c,p,frequencies[f_i], geps);
>>>>>>> support for single-precision floating point for fields array functions (#1833)
double cyl_scale;
int ci_forward = 0;
FOR_MY_COMPONENTS(forward_c) {
Expand Down Expand Up @@ -2982,7 +2989,11 @@ void material_grids_addgradient(double *v, size_t ng, std::complex<meep::realnum
to the same two nodes (which requires another factor of 0.5).
Then we perform our inner product at these nodes.
*/
<<<<<<< HEAD
std::complex<double> fwd_avg, fwd1, fwd2;
=======
std::complex<double> fwd_avg, fwd1, fwd2, prod;
>>>>>>> support for single-precision floating point for fields array functions (#1833)
ptrdiff_t fwd1_idx, fwd2_idx;

//identify the first corner of the forward fields
Expand Down
9 changes: 9 additions & 0 deletions tests/ring-ll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ int main(int argc, char *argv[]) {
imag(amp[nb]), err[nb]);

// test comparison with expected values
<<<<<<< HEAD
double err_tol = 1.0e-5;
int ref_bands = 4;
double ref_freq_re[4] = {1.1807e-01, 1.4470e-01, 1.4715e-01, 1.7525e-01};
Expand All @@ -141,6 +142,14 @@ int main(int argc, char *argv[]) {
std::complex<double>(+3.99e-02,+4.09e-02),
std::complex<double>(-1.98e-03,-1.43e-02)};
if (bands != ref_bands) meep::abort("harminv found only %i/%i bands\n", bands, ref_bands);
=======
int ref_bands = 3;
double ref_freq_re[3] = {1.1807e-01, 1.4716e-01, 1.7525e-01};
double ref_freq_im[3] = {-7.6133e-04, -2.1156e-04, -5.2215e-05};
std::complex<double> ref_amp[3] = {std::complex<double>(-8.28e-04, -1.34e-03), std::complex<double>(1.23e-03, -1.25e-02),
std::complex<double>(2.83e-03, -6.52e-04)};
if (bands != 3) meep::abort("harminv found only %i/%i bands\n", bands, ref_bands);
>>>>>>> support for single-precision floating point for fields array functions (#1833)
for (int nb = 0; nb < bands; nb++)
if ((fabs(freq_re[nb] - ref_freq_re[nb]) > 1.0e-2 * fabs(ref_freq_re[nb]) ||
fabs(freq_im[nb] - ref_freq_im[nb]) > 1.0e-2 * fabs(ref_freq_im[nb]) ||
Expand Down

0 comments on commit 8b28cf8

Please sign in to comment.