Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct the weights for dipole sources in cylindrical coordinates #2476

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/meep.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,10 @@ class fields_chunk {
}

double last_source_time();

// sources.cpp
std::complex<double> sum_sources(field_type ft);

// monitor.cpp
std::complex<double> get_field(component, const ivec &) const;

Expand Down Expand Up @@ -1761,6 +1765,9 @@ class fields {
void change_m(double new_m);
bool has_nonlinearities(bool parallel = true) const;

// sources.coo
std::complex<double> sum_sources(field_type ft);

// time.cpp
std::vector<double> time_spent_on(time_sink sink);
double mean_time_spent_on(time_sink);
Expand Down
44 changes: 41 additions & 3 deletions src/sources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,13 @@ static void src_vol_chunkloop(fields_chunk *fc, int ichunk, component c, ivec is
loc += shift * (0.5 * inva);

vec rel_loc = loc - data->center;
amps_array[idx_vol] = IVEC_LOOP_WEIGHT(s0, s1, e0, e1, 1) * amp * data->A(rel_loc);

// in cylindrical coordinates, we need to account for the r term in the integral.
if (fc->gv.dim == Dcyl)
amps_array[idx_vol] =
IVEC_LOOP_WEIGHT(s0, s1, e0, e1, dV0 + dV1 * loop_i2) * amp * data->A(rel_loc);
else
amps_array[idx_vol] = IVEC_LOOP_WEIGHT(s0, s1, e0, e1, 1) * amp * data->A(rel_loc);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can block can be more cleanly expressed as:

double interp_weight = fc->gv.dim == Dcyl ? dV0 + dV1 * loop_i2 : 1.0;
amps_array[idx_vol] = IVEC_LOOP_WEIGHT(s0, s1, e0, e1, interp_weight) * amp * data->A(rel_loc);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but let's make sure things work before we golf the code.


// check for invalid sources at r=0 in cylindrical coordinates
if (fc->gv.dim == Dcyl && loc.r() == 0 && amps_array[idx_vol] != 0.0) {
Expand Down Expand Up @@ -478,9 +484,16 @@ void fields::add_volume_source(component c, const src_time &src, const volume &w
src_vol_chunkloop_data data;
data.A = A ? A : one;
data.amp = amp;
// correct units for J delta-function amplitude
LOOP_OVER_DIRECTIONS(gv.dim, d) {
if (where.in_direction(d) == 0.0 && !nosize_direction(d)) // delta-fun
data.amp *= gv.a; // correct units for J delta-function amplitude
if (where.in_direction(d) == 0.0 && !nosize_direction(d)) { // delta-fun
if (d == R && where.center().in_direction(d) > 0)
// in cylindrical coordinates, we need to scale by $r$ for a dipole.
data.amp *= 1.0 / where.center().in_direction(d);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was the scaling by $\Delta r \times \Delta z$ removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the code comment below:

        // we handle the resolution scaling at the chunk level for cylindrical coordinates.

else if (gv.dim != Dcyl)
// we handle the resolution scaling at the chunk level for cylindrical coordinates.
data.amp *= gv.a;
}
}
sources = src.add_to(sources, &data.src);
data.center = (where.get_min_corner() + where.get_max_corner()) * 0.5;
Expand Down Expand Up @@ -755,4 +768,29 @@ diffractedplanewave::diffractedplanewave(int g_[3], double axis_[3], std::comple
p = p_;
};

/*
Occasionally, it's useful to check the sum of all the actual source amplitudes
(e.g. to ensure that the boundary weights are being computed properly).
`sum_sources()` provides a convenient interface to do just that.
*/
std::complex<double> fields::sum_sources(field_type ft) {
std::complex<double> total_sources = 0.0;

for (int i = 0; i < num_chunks; i++)
if (chunks[i]->is_mine()) total_sources += chunks[i]->sum_sources(ft);

return sum_to_all(total_sources);
}

std::complex<double> fields_chunk::sum_sources(field_type ft) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function just used for debugging and was forgotten to be removed from the commit?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I think we should keep it, as mentioned in the code comment above and the blurb above:

Finally, I added some convenience functions that sum up all the source amplitudes (i.e. perform a sort of "integration") to check if what we're doing is right.

std::complex<double> total_sources = 0.0;
for (const src_vol &sv : sources[ft]) {
for (size_t j = 0; j < sv.num_points(); j++) {
meep::master_printf("src %f\n", sv.amplitude_at(j));
total_sources += sv.amplitude_at(j);
}
}
return total_sources;
}

} // namespace meep