Skip to content

Commit 03e521e

Browse files
committed
BUG: Gravity & b-field on dipole, fixed
1 parent 0a3e91f commit 03e521e

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

src/fill_grid.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,18 @@ void Grid::fill_grid_bfield(Planets planet) {
170170

171171
bfield_info = get_bfield(lon, lat, alt, DoDebug,
172172
planet);
173+
173174
// Magnetic coordinates:
174175
// init_mag grid already initializes magLon & magInvLat
175-
// #TODO: make sure the bfield is correct for the dipole
176-
// - maybe Dot product the B_vec with (ijk)_vec?
177-
if (IsGeoGrid){
176+
if (IsGeoGrid) {
178177
magInvLat_scgc(iLon, iLat, iAlt) = bfield_info.lat;
179178
magLon_scgc(iLon, iLat, iAlt) = bfield_info.lon;
180179
}
181-
bfield_mag_scgc(iLon, iLat, iAlt) = 0.0;
182180

183181
for (iDim = 0; iDim < 3; iDim++) {
184182
bfield_vcgc[iDim](iLon, iLat, iAlt) = bfield_info.b[iDim] * cNTtoT;
185-
bfield_mag_scgc(iLon, iLat, iAlt) =
186-
bfield_mag_scgc(iLon, iLat, iAlt) +
187-
bfield_vcgc[iDim](iLon, iLat, iAlt) * bfield_vcgc[iDim](iLon, iLat, iAlt);
183+
bfield_mag_scgc(iLon, iLat, iAlt) += pow(bfield_vcgc[iDim](iLon, iLat, iAlt),
184+
2);
188185
}
189186

190187
bfield_mag_scgc(iLon, iLat, iAlt) =
@@ -193,8 +190,23 @@ void Grid::fill_grid_bfield(Planets planet) {
193190
}
194191
}
195192

196-
for (iDim = 0; iDim < 3; iDim++)
197-
bfield_unit_vcgc[iDim] = bfield_vcgc[iDim] / (bfield_mag_scgc + 1e-32);
193+
// Now we modify the dipole's magnetic field to account for any imprecision.
194+
// Take the bfield_mag and put it into the third component (b-hat = k-hat)
195+
if (IsDipole) {
196+
bfield_vcgc[2] = bfield_mag_scgc;
197+
bfield_vcgc[1].zeros();
198+
bfield_vcgc[0].zeros();
199+
200+
bfield_unit_vcgc[0].zeros();
201+
bfield_unit_vcgc[1].zeros();
202+
bfield_unit_vcgc[2].ones();
203+
204+
bfield_unit_vcgc[2] % sign(magInvLat_scgc * -1.0);
205+
206+
// slight complication -
207+
} else
208+
for (iDim = 0; iDim < 3; iDim++)
209+
bfield_unit_vcgc[iDim] = bfield_vcgc[iDim] / (bfield_mag_scgc + 1e-32);
198210

199211
int IsNorth = 1, IsSouth = 0;
200212
mag_pole_north_ll = get_magnetic_pole(IsNorth, planet);

src/init_mag_grid.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,8 @@ bool Grid::init_dipole_grid(Quadtree quadtree_ion, Planets planet) {
447447
k_corner_scgc *= planetRadius;
448448

449449
// Convert to geographic, rotating and (maybe) shifting the dipole grid.
450-
std::vector <arma_cube> llr = mag_to_geo(magLon_scgc, magLat_scgc, magAlt_scgc * planetRadius,
450+
std::vector <arma_cube> llr = mag_to_geo(magLon_scgc, magLat_scgc,
451+
magAlt_scgc * planetRadius,
451452
planet);
452453

453454
geoLon_scgc = llr[0];
@@ -473,6 +474,7 @@ bool Grid::init_dipole_grid(Quadtree quadtree_ion, Planets planet) {
473474
radius2i_scgc = 1.0 / radius2_scgc;
474475

475476
// Figure out what direction is radial:
477+
// This is all in the dipole's i,j,k coordinate system...
476478
rad_unit_vcgc = make_cube_vector(nLons, nLats, nAlts, 3);
477479
gravity_vcgc = make_cube_vector(nLons, nLats, nAlts, 3);
478480

@@ -482,11 +484,12 @@ bool Grid::init_dipole_grid(Quadtree quadtree_ion, Planets planet) {
482484
}
483485

484486
rad_unit_vcgc[1] = cos(magLat_scgc) / pow(abs(1 + 3 * sin(magLat_scgc)), 0.5);
485-
rad_unit_vcgc[2] = -2 * sin(magLat_scgc) / pow(abs(1 + 3 * sin(magLat_scgc)), 0.5);
487+
rad_unit_vcgc[2] = -2 * sin(magLat_scgc) / pow(abs(1 + 3 * sin(magLat_scgc)),
488+
0.5);
486489

487490
precision_t mu = planet.get_mu();
488-
gravity_vcgc[1] = mu * rad_unit_vcgc[1] % radius2i_scgc;
489-
gravity_vcgc[2] = mu * rad_unit_vcgc[2] % radius2i_scgc;
491+
gravity_vcgc[1] = - mu * rad_unit_vcgc[1] % radius2i_scgc;
492+
gravity_vcgc[2] = - mu * rad_unit_vcgc[2] % radius2i_scgc;
490493
gravity_potential_scgc.set_size(nX, nY, nAlts);
491494
gravity_potential_scgc.zeros();
492495
gravity_mag_scgc = sqrt(
@@ -499,11 +502,14 @@ bool Grid::init_dipole_grid(Quadtree quadtree_ion, Planets planet) {
499502
calc_dipole_grid_spacing(planet);
500503

501504

502-
// Generate mask for physicsl cells
505+
//////////////////////////////////////
506+
// Generate mask for physicsl cells //
507+
//////////////////////////////////////
508+
503509
isTooLowCell = find(geoAlt_scgc <= 0.0);
504510
isPhysicalCell = find(geoAlt_scgc > 0.0);
505511
UseThisCell.elem(isTooLowCell).fill(false);
506-
512+
507513
report.print(4, "Done altitude spacing for the dipole grid.");
508514

509515
// Calculate magnetic field and magnetic coordinates:

0 commit comments

Comments
 (0)