Skip to content

Commit

Permalink
Merge pull request #6 from celestiary/dev
Browse files Browse the repository at this point in the history
Updates to Time
  • Loading branch information
pablo-mayrgundter authored Mar 4, 2024
2 parents e60f46e + 51582da commit 0198cf8
Show file tree
Hide file tree
Showing 20 changed files with 323 additions and 203 deletions.
13 changes: 12 additions & 1 deletion docs/index.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/index.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/App.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useEffect, useRef, useState} from 'react'
import React, {ReactElement, useEffect, useRef, useState} from 'react'
import {Route, useLocation} from 'wouter'
import Box from '@mui/material/Box'
import ScopedCssBaseline from '@mui/material/ScopedCssBaseline'
Expand All @@ -17,7 +17,7 @@ import StarSelectIcon from '@mui/icons-material/SavedSearch'
import './index.css'


/** @returns {React.ReactElement} */
/** @returns {ReactElement} */
export default function App() {
const toggleIsStarsSelectActive = useStore((state) => state.toggleIsStarsSelectActive)
const [celestiary, setCelestiary] = useState(null)
Expand Down
18 changes: 18 additions & 0 deletions js/AsterismsCatalog.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {readFileSync} from 'fs'

import AsterismsCatalog from './AsterismsCatalog.js'
import StarsCatalog from './StarsCatalog.js'
import {toArrayBuffer} from './utils.js'


describe('AsterismsCatalog', () => {
it('constructor loads data', () => {
const starsCatalog = new StarsCatalog()
starsCatalog.read(toArrayBuffer(readFileSync('./public/data/stars.dat')))
expect(starsCatalog.numStars).toEqual(106747)
const asterisms = new AsterismsCatalog(starsCatalog)
asterisms.read(readFileSync('./public/data/asterisms.dat', 'utf-8'))
expect(asterisms.byName.size).toEqual(89)
})
})

22 changes: 0 additions & 22 deletions js/AsterismsCatalog_test.js

This file was deleted.

6 changes: 3 additions & 3 deletions js/Celestiary.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class Celestiary {
this.controlPanel = new ControlPanel(navElt, this.loader)
this.load()
this.setupPathListeners()
this.setupKeyListeners()
this.setupKeyListeners(useStore)
document.body.addEventListener('mousedown', (e) => e.preventDefault())
this.navVisible = true
// these are here for convenience debugging from jsconsole.
Expand Down Expand Up @@ -138,8 +138,8 @@ export default class Celestiary {
}


setupKeyListeners() {
const k = new Keys()
setupKeyListeners(useStore) {
const k = new Keys(useStore)
k.map(' ', () => {
this.setIsPaused(this.time.togglePause())
},
Expand Down
11 changes: 7 additions & 4 deletions js/Keys.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
/** */
export default class Keys {
/** */
constructor() {
constructor(useStore) {
this.keymap = {}
this.msgs = {}
this.bindToWindow()
this.bindToWindow(useStore)
}


/** */
bindToWindow() {
bindToWindow(useStore) {
window.addEventListener('keydown', (e) => {
this.onKeyDown(e)
const is = useStore.getState().isTimeDialogVisible
if (!is) {
this.onKeyDown(e)
}
})
}

Expand Down
55 changes: 55 additions & 0 deletions js/StarsCatalog.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {readFileSync} from 'fs'

import StarsCatalog from './StarsCatalog.js'
import {toArrayBuffer} from './utils.js'


const STARS_DAT = './public/data/stars.dat'
const STAR_NAMES_DAT = './public/data/starnames.dat'

const TEST_STAR = {
x: 0,
y: 0,
z: 0,
hipId: 0,
absMag: 1,
kind: 'M',
spectralType: '0',
sub: '0',
lumClass: 1,
lumRelSun: 1,
radius: 1,
}


describe('StarsCatalog', () => {
it('minimal catalog loads', () => {
const starByHip = {0: TEST_STAR}
const hipByName = {Sun: 0}
const namesByHip = {0: ['Sun']}
const minMag = 10
const maxMag = 1
const catalog = new StarsCatalog(1, starByHip, hipByName, namesByHip, minMag, maxMag)
expect(catalog.numStars).toEqual(1)
})

it('StarsCatalog#read', () => {
const catalog = new StarsCatalog()
catalog.read(toArrayBuffer(readFileSync(STARS_DAT)))
expect(catalog.numStars).toEqual(106747)
})

it('StarsCatalog#downsample', () => {
let catalog = new StarsCatalog()
catalog = catalog.read(toArrayBuffer(readFileSync(STARS_DAT))).downsample(1000)
expect(catalog.numStars).toEqual(1000)
})

it('StarsCatalog#readNames', () => {
const catalog = new StarsCatalog()
catalog.read(toArrayBuffer(readFileSync(STARS_DAT)))
catalog.readNames(readFileSync(STAR_NAMES_DAT, 'utf-8'))
// TODO(pablo): was 5699
expect(catalog.hipByName.size).toEqual(5672)
})
})
60 changes: 0 additions & 60 deletions js/StarsCatalog_test.js

This file was deleted.

3 changes: 2 additions & 1 deletion js/Style.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ const ColorModeContext = createContext({toggleColorMode: () => {}})
const dark = {
mode: 'dark',
primary: {
main: '#f00',
// TODO(pablo): this is a light teal for text in dark mode. not sure why main key is used
main: '#869fdb',
light: '#0f0',
dark: '#00f',
contrastText: '#fff',
Expand Down
26 changes: 21 additions & 5 deletions js/Time.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export default class Time {
}


/** @param {number} unixTime In seconds */
setTime(unixTime) {
this.simTime = unixTime
}


/** */
setTimeToNow() {
this.timeScale = 1.0
Expand Down Expand Up @@ -150,16 +156,26 @@ export function toJulianDay(t) {


/**
* @param {number} UNIX Epoch milliseconds
* @param {number} unixTime UNIX Epoch milliseconds
* @returns {string}
*/
function timeToDateStr(time) {
return new Date(time).toLocaleDateString(undefined, {
year: 'numeric',
month: 'long',
export function timeToDateStr(unixTime) {
const date = new Date(unixTime)

// Assuming the month and day are provided in a modern context
// Adjust the formatting as needed
const dateWithoutYear = date.toLocaleDateString(undefined, {
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
timezone: 'long',
})

// Use commas for large-looking years
const year = date.getUTCFullYear()
const yearStr = Math.abs(year) < 10000 ? (year).toString() : (year).toLocaleString()

return `${yearStr} ${dateWithoutYear}`
}
19 changes: 19 additions & 0 deletions js/Time.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {timeToDateStr} from './Time'


describe('Time', () => {
describe('timeToDateStr', () => {
it('handles start of unix epoch', () => {
expect(timeToDateStr(0).toString()).toEqual('1970 Jan 1 at 12:00:00 AM')
})

it('handles future', () => {
expect(timeToDateStr(1000000000000000).toString()).toEqual('33,658 Sep 27 at 1:46:40 AM')
})

it('handles past', () => {
expect(timeToDateStr(-1000000000000000).toString()).toEqual('-29,719 Apr 5 at 10:13:20 PM')
})
})
})

81 changes: 81 additions & 0 deletions js/gravity.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import {
G,
step,
// updateAccelerations,
} from './gravity.js'


describe('gravity', () => {
it('particle at rest', () => {
const pos = [0, 0, 0]
const vel = [0, 0, 0]
const acc = [null, null, null]
const mass = [1]

const zeros = [0, 0, 0]
applyAndAssert(
pos, vel, acc, mass,
zeros, pos,
zeros, vel,
zeros, acc)
expect(mass[0]).toEqual(1)
})


it('particle in motion', () => {
const pos = [0, 0, 0]
const vel = [1, 2, 3]
const acc = [null, null, null]
const mass = [1]
applyAndAssert(
pos, vel, acc, mass,
[1, 2, 3], pos,
[1, 2, 3], vel,
[0, 0, 0], acc)
})


it('two particles', () => {
const pos = [0, 0, 0, 1, 0, 0]
const vel = [0, 0, 0, 0, 0, 0]
const acc = [null, null, null, null, null, null]
const mass = [1, 1]
applyAndAssert(
pos, vel, acc, mass,
[0, 0, 0, 1, 0, 0], pos,
[G / 2, 0, 0, -G / 2, 0, 0], vel,
[G, 0, 0, -G, 0, 0], acc,
'on first step')
/* tests.applyAndAssert(pos, vel, acc, mass,
[G, 0, 0, 1-G, 0, 0], pos,
[G, 0, 0, -G, 0, 0], vel,
[2*G, 0, 0, -2*G, 0, 0], acc,
'on second step')*/
})
})


/** Expect vectors and components to be equal */
function assertVectorsEqual(vec1, vec2, appendMsg = '') {
if (vec1.length !== vec2.length) {
throw new Error('vector lengths must be equal')
}
for (let i = 0; i < vec1.length; i++) {
expect(vec1[i]).toEqual(vec2[i])
}
}


/** Apply step and test vectors */
function applyAndAssert(
pos, vel, acc, mass,
expPos, actPos,
expVel, actVel,
expAcc, actAcc,
appendMsg = '') {
const dt = 1
step(pos, vel, acc, mass, dt)
assertVectorsEqual(expPos, actPos, `for pos ${appendMsg}`)
assertVectorsEqual(expVel, actVel, `for vel ${appendMsg}`)
assertVectorsEqual(expAcc, actAcc, `for acclerations ${appendMsg}`)
}
Loading

0 comments on commit 0198cf8

Please sign in to comment.