Skip to content

Refactor logT precalculation#535

Merged
brittonsmith merged 21 commits into
grackle-project:newchem-cppfrom
mabruzzo:ncc/LnTPreparer
May 20, 2026
Merged

Refactor logT precalculation#535
brittonsmith merged 21 commits into
grackle-project:newchem-cppfrom
mabruzzo:ncc/LnTPreparer

Conversation

@mabruzzo

@mabruzzo mabruzzo commented Apr 20, 2026

Copy link
Copy Markdown
Collaborator

To be reviewed after #543 has been merged.


This PR refactors the calculations of ln(T). It factors out all of the logic so that it is now performed by the newly created LnTPreparer class1. I don't actually think that it's an ideal abstraction, but I think it's a huge improvement over what we had. Before we try to come up with a better abstraction, I think we need to decide how to resolve a bunch of inconsistencies that are described in the docstring of LnTPreparer (maybe that discussion should be moved to an issue?).

Hoisting logic out of cool1d_multi_g and lookup_cool_rates1d

As part of this PR, I pulled the logic pertaining to LnTPreparer class out of cool1d_multi_g and lookup_cool_rates1d. This does 2 things: (i) we can stop passing iteration count to cool1d_multi_g and (ii) it paves the way for us to directly reuse the computed ln(T) values for both cool1d_multi_g and lookup_cool_rates1d (note the values are still recomputed in this PR -- we start reusing values in PR #537).

At a glance, the logic might look a little more complex. However, this complexity has always been present; I've just made it more explicit. I think this is definitely a step in the right direction.

Gold Standard

In any case, I tried to take a lot of care to avoid breaking the gold-standard. At the end something changed, and I don't totally understand why. It's a small enough change that I'm content to probably just update the gold-standard.

I have deferred a small handful of (related) changes to PR #537, where there will be a significant change in the gold-standard.

Footnotes

  1. This was made as a slightly more traditional C++ class with a true constructor and attached methods to gauge how people feel about moving towards this sort of approach. I'm happy convert it to the more classic C-style logic on request.

@mabruzzo mabruzzo marked this pull request as ready for review April 23, 2026 12:13
@mabruzzo mabruzzo changed the title WIP: refactor logT precalculation Refactor logT precalculation Apr 23, 2026
@brittonsmith brittonsmith changed the base branch from newchem-cpp to main May 14, 2026 13:01
@brittonsmith brittonsmith changed the base branch from main to newchem-cpp May 14, 2026 13:01

@brittonsmith brittonsmith left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is great. I like it a lot. I only get one test failure locally and, as you would guess, it is minor. I'll update the gold standard and merge this.

Comment thread src/clib/lnT_prep.hpp
Comment on lines +71 to +77
logTlininterp_buf.t1[i] =
(logtem_start + (logTlininterp_buf.indixe[i] - 1) * dlogtem);
logTlininterp_buf.t2[i] =
(logtem_start + (logTlininterp_buf.indixe[i]) * dlogtem);
logTlininterp_buf.tdef[i] =
(logTlininterp_buf.logtem[i] - logTlininterp_buf.t1[i]) /
(logTlininterp_buf.t2[i] - logTlininterp_buf.t1[i]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You've probably thought about this, but I wonder if it's worth simplifying the logic here. I'd be curious to know if this makes any difference.

Suggested change
logTlininterp_buf.t1[i] =
(logtem_start + (logTlininterp_buf.indixe[i] - 1) * dlogtem);
logTlininterp_buf.t2[i] =
(logtem_start + (logTlininterp_buf.indixe[i]) * dlogtem);
logTlininterp_buf.tdef[i] =
(logTlininterp_buf.logtem[i] - logTlininterp_buf.t1[i]) /
(logTlininterp_buf.t2[i] - logTlininterp_buf.t1[i]);
logTlininterp_buf.t2[i] =
(logtem_start + (logTlininterp_buf.indixe[i]) * dlogtem);
logTlininterp_buf.t1[i] = logTlininterp_buf.t2[i] - dlogtem;
logTlininterp_buf.tdef[i] =
(logTlininterp_buf.logtem[i] - logTlininterp_buf.t1[i]) / dlogtem;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ok, I tested this and it took the test failures from 1 to 35! Let's pass on this for now, although maybe it's still something to think about for later if it improves performance.

@brittonsmith brittonsmith merged commit 724ef73 into grackle-project:newchem-cpp May 20, 2026
2 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants