Skip to content

Commit

Permalink
Merge pull request #101 from MineralsCloud/polynomial_least_square_fi…
Browse files Browse the repository at this point in the history
…tting

Add `@jit` to `polynomial_least_square_fitting`
  • Loading branch information
singularitti authored Dec 20, 2023
2 parents 690c106 + 4394248 commit 4a17bc6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/qha/fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
__all__ = ["polynomial_least_square_fitting", "apply_finite_strain_fitting"]


def polynomial_least_square_fitting(xs, ys, new_xs, order: Optional[int] = 3):
@jit(float64[:](float64[:], float64[:], float64[:], int64), nopython=True, cache=True)
def polynomial_least_square_fitting(
xs: Vector, ys: Vector, new_xs: Vector, order: Optional[int] = 3
):
"""
The algorithm is referenced from the
`Wolfram MathWorld <http://mathworld.wolfram.com/LeastSquaresFittingPolynomial.html>`_.
Expand All @@ -33,9 +36,9 @@ def polynomial_least_square_fitting(xs, ys, new_xs, order: Optional[int] = 3):
xx = np.vander(
xs, order, increasing=True
) # This will make a Vandermonde matrix that will be used in EoS fitting.
a, _, _, _ = np.linalg.lstsq(xx, ys, rcond=None)
a, _, _, _ = np.linalg.lstsq(xx, ys) # See https://stackoverflow.com/a/64224087
new_y = np.vander(new_xs, order, increasing=True) @ a
return a, new_y
return new_y


@jit(
Expand Down Expand Up @@ -69,8 +72,7 @@ def apply_finite_strain_fitting(
) # Initialize the F(T,V) array

for i in range(temperature_amount):
_, f_i = polynomial_least_square_fitting(
f_v_t[i] = polynomial_least_square_fitting(
strains_sparse, free_energies[i], strains_dense, order
)
f_v_t[i] = f_i
return f_v_t
2 changes: 1 addition & 1 deletion src/qha/grid_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def approach_to_best_ratio(
vr.interpolate_volumes()
strains, finer_volumes = vr.strains, vr.out_volumes
eulerian_strain = calculate_eulerian_strain(volumes[0], volumes)
_, f_v_tmax = polynomial_least_square_fitting(
f_v_tmax = polynomial_least_square_fitting(
eulerian_strain, free_energies, strains, self.option
)
p_v_tmax = -np.gradient(f_v_tmax) / np.gradient(finer_volumes)
Expand Down
2 changes: 1 addition & 1 deletion src/qha/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def calibrate_energy_on_reference(
strains_after_calibration = calculate_eulerian_strain(
volumes_before_calibration[i, 0], volumes_for_reference
)
_, energies_after_calibration[i, :] = polynomial_least_square_fitting(
energies_after_calibration[i, :] = polynomial_least_square_fitting(
strains_before_calibration,
energies_before_calibration[i],
strains_after_calibration,
Expand Down

0 comments on commit 4a17bc6

Please sign in to comment.