wmwv / cosmo

Go cosmology library
MIT License
7 stars 2 forks source link

Fix broken `WCDM.ComovingDistance` for Nonflat LCDM cases #28

Closed wmwv closed 6 years ago

wmwv commented 6 years ago

The WCDM.ComovingDistance is currently wrong for Nonflat LCDM cases.

  1. [x] Add test for WCDMComovingDistanceNonflatLCDM
  2. [x] Fix wcdm.go
  3. [x] Check lambdacdm.go, wacdm.go as well.
wmwv commented 6 years ago

Wasn't handling negative curvature correctly. Switch to use complex cmplx.Sqrt, cmplx.Sinh.

wmwv commented 6 years ago

Performance:

goos: darwin
goarch: amd64
pkg: github.com/wmwv/cosmo
BenchmarkMathSqrt-16            2000000000           0.27 ns/op
BenchmarkMathSinh-16             200000000           8.48 ns/op
BenchmarkMathSinhSqrt-16         200000000           8.55 ns/op
BenchmarkComplexSqrt-16          500000000           3.79 ns/op
BenchmarkComplexSinh-16          100000000          12.2 ns/op
BenchmarkComplexSinhSqrt-16       50000000          26.6 ns/op

The code SinhSqrt tests are mocking up the code in question, which compares a (1/Sqrt(x))*Sinh(Sqrt(x)). The performance difference is substantial in a relative sense: 8.55 ns/op vs. 26.6 ns/op.

But in an absolute sense, the LambdaComovingDistance calculation takes ~750 ns, and the WCDMComovingdistance and WACDMComovingDistance calculations take ~200 microseconds. The 18 ns taken by a final wrapping with complex numbers of the distance calculation is negligible.

wmwv commented 6 years ago

Went with the cases written out separately for Ok0<0; Ok0==0; Ok0>0, but the performance gains are trivial.

wmwv commented 6 years ago

Merged to master.