Repository navigation
Conversation
MOSAIC chooses between its sulfate-poor and sulfate-rich treatments with XT >= 2 (MESA, compute_activities, ions_to_electrolytes, MESA_estimate_eleliquid, form_electrolytes, conform_electrolytes, and the ASTEM routines that use the XT these return), and treats 0 < XT < 1 as excess sulfate (MESA, ASTEM_update_phase_eqblm). An exactly neutral particle (NH4 = 2 SO4) whose amounts have been through a mass <-> mole conversion, as in PartMC at every step, has XT a few units of roundoff above or below 2, so it takes one branch or the other at random. XT is computed from the ions in four places (calculate_XT, and inline in ions_to_electrolytes, form_electrolytes and conform_electrolytes, all with the same expression). Each now calls the new snap_XT, which sets XT to exactly 2 (or 1) when it is within a relative xt_snap_rtol = 1e-9 of it, so that every routine puts such a particle in the same regime, the one it is in exactly at the boundary (in line with the 2014 change from XT >= 1.9999 to XT >= 2.0 for consistency between routines). Not changed: the ratio that ions_to_electrolytes recomputes in its sulfate-rich branch (thresholds 1.0 and 1.5) and the ratios of the sulfate-rich branch of form_electrolytes and conform_electrolytes (XT_prime, XNa_prime, XNH4_prime) only split the sulfate among the electrolytes, and the split is continuous across those thresholds; those of MESA_estimate_eleliquid (XNa_d, XNH4_d) only set the starting estimate of MESA. xt_snap_rtol = 0 gives back the previous behaviour. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Snap the sulfate ratio XT to 2 or 1 when it is within roundoff of it
Branch
xt-roundoff-tolerance(one commit onpartmc-libraryat 2c5e006, i.e. after PR #2).Files:
MOSAIC_v2/aerosol/module_mosaic_ext.f90(+32: a new subroutinesnap_XTand one call after each of the four computations of XT),MOSAIC_v2/datamodules/module_data_mosaic_aero.f90(+13: the parameterxt_snap_rtol = 1.0e-9_r8and its comment).Problem
MOSAIC chooses between its sulfate-poor and sulfate-rich treatments with
the sulfate ratio XT = (NH4 + Na + 2 Ca) / (SO4 + MSA/2):
MESA(MDRH fromjsulf_poororjsulf_rich)XT < 1 .and. XT > 0(excess sulfate, always liquid),XT >= 2ASTEM_update_phase_eqblm(the same decision)XT < 1 .and. XT > 0,XT >= 2compute_activities(fully dissociated vs. HSO4- equilibrium)XT >= 2ions_to_electrolytesXT >= 2MESA_estimate_eleliquidXT >= 2;XT_d >= XNa_d,XT_d > 1form_electrolytes,conform_electrolytesXT >= 2ASTEM_flux_wet_case*users of the XT returned byions_to_electrolytes(module_mosaic_astem.f90 ~1266, ~2858) andASTEM_flux_dry(~2094)XT < 2 .and. XT >= 0XT is computed in four places with the same expression:
calculate_XT,and inline in
ions_to_electrolytes,form_electrolytesandconform_electrolytes(each has the call tocalculate_XTcommented outwith
! EFFI).A particle that is exactly neutral, e.g. NH4 = 2 SO4 as
electrolytes_to_ionsmakes it from (NH4)2SO4, does not stay exactly atXT = 2 in PartMC: PartMC stores volumes and converts them to moles at every
step, and after that round trip XT is a few units of roundoff above or
below 2. Such a particle then takes the sulfate-poor or the sulfate-rich
branch depending on the last bits, which change from step to step, so it is
misclassified at random. In the urban plume scenario this is a large share
of the particles: in the 25 hourly outputs of the run below (10000
particles), 99327 of 381844 particle-outputs (26%) have an XT within a
relative 1e-12 of 2, and the count is the same within 1e-9 and 1e-6:
nothing lies between roundoff and 1e-6. (XT computed from PartMC's output
masses; with flat_source weighting, 56128 of 283055, 20%.) None is near 1
in this scenario.
History
The 2014 changes show that the boundary and the consistency between
routines were deliberate:
ions_to_electrolyteshasXT.ge.1.9999commented out by "RAZ 4/16/2014" and replaced byXT.ge.2.0("Slightly different logic, consistent with that incompute_activities subr.");
compute_activities"changed .gt. to .ge.RAZ 4/16/2014";
form_electrolytesandconform_electrolytesreplacedXT .ge. 1.9999byXT .ge. 2.0on "RAZ 11/10/2014", and the ASTEM testsXT.lt.2.0carry the same date. So a neutral particle is meant to besulfate-poor, in every routine. With roundoff that holds only for the
particles that land at or above 2.
Measurements
PartMC (
update_mosaic, 1716875) linked against MOSAIC built ascompile.shdoes for gfortran (-O2 -g -fallow-argument-mismatch,gfortran 14.3, Perlmutter CPU node): this branch against
partmc-libraryas it is (2c5e006). Urban plume scenario, 24 h, 10000 particles, del_t 60
s, hourly output,
rand_init 7, coagulation off, flat weighting. Withcoagulation off and flat weighting no random decision of PartMC depends on
the particles' composition, so every run has the same particles (checked:
the particle ids agree in every output) and the differences between runs
come from the chemistry alone. Numbers: largest relative difference over
the 24 hourly outputs.
Effect of the change (
partmc-libraryagainst this branch):The same with
flat_sourceweighting: gas NH3 8.9%, bulk NO3 1.0%, Cl1.2%, H2O 1.0%. The current code treats a random part of the neutral
particles as sulfate-rich (more acidic: less nitrate and chloride taken up
early, a different MDRH and water); this branch treats all of them as
neutral.
Run-to-run "order noise": PartMC hands its particles to MOSAIC as bins
1..n. A run in which the particles are handed over in another order, a new
random one at every step (a test-only change of PartMC's
mosaic_from_partmc/mosaic_to_partmc, below), should differ only byroundoff:
partmc-library, shuffled vs. not (seed 1; seed 2)So nearly all of the order noise of the current code (1e-3 to 1e-2 in
bulk species, a few percent of a particle's dry mass) is this switch; with
neutral particles treated as neutral it falls to the 1e-7 level.
Sensitivity to the tolerance: the same run with
xt_snap_rtol = 1e-5instead of 1e-9 differs from it by at most 4e-7 in bulk NO3, 1.2e-6 in gas
NH3 and 4.5e-6 per particle (p99), i.e. by the order noise: the result
does not depend on the value between these bounds.
Cost: none measurable (wall time of the 24 h run 334 s with this branch,
332 s without; runs side by side on one node).
Test-only PartMC change used for the shuffled runs (src/mosaic.F90)
A module array
mosaic_bin_of_part, the identity unless the environmentvariable
PMC_MOSAIC_SHUFFLEis set to a seed; then a new randompermutation (Fisher-Yates, MINSTD generator) at every call of
mosaic_from_partmc. Bothmosaic_from_partmcandmosaic_to_partmcuseoffset = ngas_max + (mosaic_bin_of_part(i_part) - 1)*naer_totinstead of(i_part - 1)*naer_tot. Not proposed for PartMC.The change
snap_XT(XT)sets XT to exactly 2 when |XT - 2| <= 2 * xt_snap_rtol, andto exactly 1 when |XT - 1| <= xt_snap_rtol; otherwise it leaves XT as it
is (the sentinel -1 for no sulfate is untouched). It is called right after
each of the four computations of XT, so every routine that tests XT sees
the same value for the same particle and makes the same decision: the
regime the particle is in exactly at the boundary (sulfate-poor at 2, not
"excess sulfate" at 1).
xt_snap_rtol = 0gives back the currentbehaviour.
Why 1e-9: the roundoff of XT after a volume-mole round trip is a few units
of 1.1e-16; 1e-9 is about 1e7 times that, so every roundoff case is caught,
also after longer chains of operations. It is far below any physically
meaningful excess of cations or sulfate (a relative excess of 1e-9 of NH4
over 2 SO4 changes nothing measurable), and far below MOSAIC's own
tolerances (
rtol_mesa0.01,ptol_mol_astem0.01%). The census shows noparticle between 1e-12 and 1e-6 of the boundary, and 1e-5 gives the same
results (above). It is not large enough for single-precision storage of
the amounts (relative roundoff 6e-8); a model that stores floats would
need about 1e-6.
What is not changed, and why:
ions_to_electrolytesrecomputes XT as(remaining NH4 + Na) / SO4 and tests
<= 1.0and<= 1.5. Thesethresholds only split the sulfate among H2SO4, bisulfate, letovicite and
sulfate, and the split is continuous across them (at 1.0 both sides give
all bisulfate, at 1.5 all letovicite).
form_electrolytesandconform_electrolytestestsXT_prime >= XNa_prime,XNH4_prime >= 1.5and
XT_prime >= 1. These too only choose the order in which theform_*routines are called, and the electrolytes are continuous acrossthe thresholds.
MESA_estimate_eleliquidtestsXNH4_dagainst 5/3, 1.5 and 1.0 andXT_d(= the snapped XT) againstXNa_dand 1. These choose thestarting estimate of the liquid electrolytes for MESA, not the result;
they are discontinuous, but only in the starting point of an iteration.
We did not snap them; see "left to judge".
What was checked
for 24 h with it, with flat and with
flat_sourceweighting (runsabove).
crustal-threshold-particle-resolved(companion PR); the merge was run as well (see there).
Left for the reviewer to judge
variable that a host model can set.
only the recomputed sulfate-rich ratio is, where the split is
continuous.
MESA_estimate_eleliquid(XNH4_d) should besnapped too. They only set the starting point of MESA.
neutral particles; we read them as removing a random misclassification,
in line with the 2014 intent.
Relation to other PRs
partmc-libraryafter PR Compile with -O2 and avoid per-bin work-array allocation #2 (-O2 under gfortran, per-bin workarrays; merged), which changed no result.
crustal-threshold-particle-resolved(no crustal-fractionlimit on the upper hysteresis curve in the particle-resolved framework):
independent; the two touch different lines.
🤖 Generated with Claude Code