Skip to content

Commit

Permalink
Traits and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
islent committed Dec 19, 2023
1 parent 49fc25b commit 40497b4
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: CI

on: [push, pull_request, workflow_dispatch]

# 64-bit Julia only
jobs:
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }}
runs-on: ${{ matrix.os }}
if: "!contains(github.event.head_commit.message, '[skip ci]') && !contains(github.event.head_commit.message, 'CompatHelper')"
strategy:
matrix:
version:
- '1.9.4'
os:
- ubuntu-latest
- macOS-latest
- windows-latest
arch:
- x64
steps:
- run: echo ACTIONS_RUNNER_DEBUG true
- uses: actions/[email protected]
- name: "Set up Julia"
uses: julia-actions/setup-julia@latest
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
#- uses: julia-actions/julia-buildpkg@latest
- uses: julia-actions/julia-runtest@latest
#env:
# ACTIONS_RUNNER_DEBUG: true
- uses: julia-actions/julia-uploadcodecov@latest
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
15 changes: 15 additions & 0 deletions .github/workflows/TagBot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: TagBot
on:
issue_comment:
types:
- created
workflow_dispatch:
jobs:
TagBot:
if: github.event_name == 'workflow_dispatch' || github.actor == 'JuliaTagBot'
runs-on: ubuntu-latest
steps:
- uses: JuliaRegistries/TagBot@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
ssh: ${{ secrets.DOCUMENTER_KEY }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test/output
Manifest.toml
12 changes: 12 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name = "AstroSimBase"
uuid = "c6a34d98-f626-4d8d-b450-a3f124eaada6"
authors = ["islent <[email protected]>"]
version = "0.1.0"

[deps]
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
PrecompileTools = "1"
julia = "1.6"
87 changes: 87 additions & 0 deletions src/AstroSimBase.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
module AstroSimBase

using PrecompileTools

export LoggingMode, NormalMode, ProgressMode, SilentMode
export GravityModel, Newton, MOND1983Milgrom, QUMOND
export DeviceType, CPU, GPU
export TimeIntegration, Euler, Leapfrog, RK4
export BoundaryCondition, Periodic, Dirichlet, Vacuum, Newman, Reflective

export traitstring, emptyfunction
export mkpathIfNotExist
export need_to_interrupt

# Traits
abstract type LoggingMode end
struct NormalMode <: LoggingMode end
"Logging with progress bar"
struct ProgressMode <: LoggingMode end
struct SilentMode <: LoggingMode end

"Gravity model. Supported: `Newton`, `MOND1983Milgrom`, `QUMOND`"
abstract type GravityModel end
"Traditional Newtonian gravity"
struct Newton <: GravityModel end
"Milgrom 1983 formula of MOND"
struct MOND1983Milgrom <: GravityModel end
"QUasi-linear MOdified Newtonian Dynamics"
struct QUMOND <: GravityModel end

abstract type DeviceType end
struct CPU <: DeviceType end
struct GPU <: DeviceType end

abstract type TimeIntegration end
"1st-order explicit Euler time integration"
struct Euler <: TimeIntegration end
"Leapfrog time integration"
struct Leapfrog <: TimeIntegration end
"4-th order Runge-Kutta time integration"
struct RK4 <: TimeIntegration end
#"Hierarchical time integration"
#struct Hierarchical <: TimeIntegration end

abstract type BoundaryCondition end
struct Periodic <: BoundaryCondition end
struct Dirichlet <: BoundaryCondition end
struct Vacuum <: BoundaryCondition end
struct Newman <: BoundaryCondition end
struct Reflective <: BoundaryCondition end


# Functions
"Better printing of trait types"
traitstring(t) = string(t)[1:end-2]

"""
function emptyfunction(args...) end
Accept any outputs but doing nothing.
It is designed for initializing callback functions
"""
function emptyfunction(args...) end

function mkpathIfNotExist(dir)
if !isdir(dir)
mkpath(dir)
end
end

"""
function need_to_interrupt(OutputDir::String)
If there is a file named `stop` in folder `OutputDir`, return true; else, return `false`.
"""
function need_to_interrupt(OutputDir::String)
if isfile(joinpath(OutputDir, "stop"))
return true
else
return false
end
end

include("precompile.jl")

end # module AstroSimBase
29 changes: 29 additions & 0 deletions src/precompile.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@setup_workload begin
@compile_workload begin
# Traits
NormalMode()
ProgressMode()
SilentMode()

Newton()
MOND1983Milgrom()
QUMOND()

CPU()
GPU()

Euler()
Leapfrog()
RK4()

Periodic()
Dirichlet()
Vacuum()
Newman()
Reflective()

# Functions
emptyfunction()
traitstring(CPU)
end
end
42 changes: 42 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Test
using AstroSimBase

@testset "Traits" begin
@test NormalMode() isa LoggingMode
@test ProgressMode() isa LoggingMode
@test SilentMode() isa LoggingMode

@test Newton() isa GravityModel
@test MOND1983Milgrom() isa GravityModel
@test QUMOND() isa GravityModel

@test CPU() isa DeviceType
@test GPU() isa DeviceType

@test Euler() isa TimeIntegration
@test Leapfrog() isa TimeIntegration
@test RK4() isa TimeIntegration

@test Periodic() isa BoundaryCondition
@test Dirichlet() isa BoundaryCondition
@test Vacuum() isa BoundaryCondition
@test Newman() isa BoundaryCondition
@test Reflective() isa BoundaryCondition
end

@testset "Functions" begin
@test isnothing(emptyfunction("Input anything"))
@test traitstring(CPU()) == "CPU"

folder = "output"
if isdir(folder)
rm(folder, force=true, recursive=true)
end
mkpathIfNotExist(folder)
mkpathIfNotExist(folder)

@test_broken need_to_interrupt("output")
f = open("output/stop", "w")
close(f)
@test need_to_interrupt("output")
end

0 comments on commit 40497b4

Please sign in to comment.