Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Persefone.jl
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Persefone
Persefone.jl
Commits
61f0ef71
Commit
61f0ef71
authored
9 months ago
by
xo30xoqa
Browse files
Options
Downloads
Patches
Plain Diff
Moved random functions to utils.jl, added `randn()`
parent
bd31a3e9
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/core/input.jl
+0
-33
0 additions, 33 deletions
src/core/input.jl
src/core/utils.jl
+62
-1
62 additions, 1 deletion
src/core/utils.jl
with
62 additions
and
34 deletions
src/core/input.jl
+
0
−
33
View file @
61f0ef71
...
...
@@ -125,39 +125,6 @@ function flattenTOML(tomldict)
flatdict
end
"""
@rand(args...)
Return a random number or element from the sample, using the model RNG.
This is a utility wrapper that can only be used a context where the
`model` object is available.
"""
macro
rand
(
args
...
)
:
(
$
(
esc
(
:
rand
))(
$
(
esc
(
:
model
))
.
rng
,
$
(
map
(
esc
,
args
)
...
)))
end
"""
@shuffle!(collection)
Shuffle the given collection in place, using the model RNG.
This is a utility wrapper that can only be used a context where the
`model` object is available.
"""
macro
shuffle!
(
collection
)
:
(
$
(
esc
(
:
shuffle!
))(
$
(
esc
(
:
model
))
.
rng
,
$
(
esc
(
collection
))))
end
"""
@chance(odds)
Return true if a random number is less than the odds (0.0 <= `odds` <= 1.0),
using the model RNG. This is a utility wrapper that can only be used a context
where the `model` object is available.
"""
macro
chance
(
odds
)
:
(
$
(
esc
(
:
rand
))(
$
(
esc
(
:
model
))
.
rng
)
<
$
(
esc
(
odds
)))
end
"""
parsecommandline()
...
...
This diff is collapsed.
Click to expand it.
src/core/utils.jl
+
62
−
1
View file @
61f0ef71
...
...
@@ -4,6 +4,8 @@
### units and dates.
###
### UNITS AND DIMENSIONS
## Import and define units and dimensions
import
Unitful
:
cm
,
m
,
km
,
ha
,
mg
,
g
,
kg
,
Length
,
Area
,
Mass
const
m²
=
m
^
2
...
...
@@ -14,7 +16,7 @@ Base.:(/)(x::S,y::T) where {S<:Length, T<:Length} = (upreferred(x)/m) / (uprefer
Base
.:
(
/
)(
x
::
S
,
y
::
T
)
where
{
S
<:
Area
,
T
<:
Area
}
=
(
upreferred
(
x
)
/
m²
)
/
(
upreferred
(
y
)
/
m²
)
Base
.:
(
/
)(
x
::
S
,
y
::
T
)
where
{
S
<:
Mass
,
T
<:
Mass
}
=
(
upreferred
(
x
)
/
g
)
/
(
upreferred
(
y
)
/
g
)
##
Utility type and function for working wth recurring dates
##
RECURRING DATES
"""
AnnualDate
...
...
@@ -87,3 +89,62 @@ Convert an AnnualDate to a Date, using the current/next/previous year of the sim
thisyear
(
ad
::
AnnualDate
,
model
::
SimulationModel
)
=
Date
(
year
(
model
.
date
),
ad
)
nextyear
(
ad
::
AnnualDate
,
model
::
SimulationModel
)
=
Date
(
year
(
model
.
date
)
+
1
,
ad
)
lastyear
(
ad
::
AnnualDate
,
model
::
SimulationModel
)
=
Date
(
year
(
model
.
date
)
-
1
,
ad
)
### RANDOM NUMBERS
"""
randn(vector)
Return a random element from the given vector, following a (mostly) normal distribution based on
index values (i.e. elements in the middle of the vector will be returned most frequently).
"""
function
randn
(
v
::
Vector
,
rng
=
default_rng
())
r
=
randn
(
rng
)
+
3
# normal distribution centered around 3, gives values from 0 to 6
step
=
6
/
length
(
v
)
i
=
Int
(
round
(
r
/
step
))
v
[
i
]
end
"""
@randn(args...)
Return a normally-distributed random number or element from the sample, using the model RNG.
This is a utility wrapper that can only be used a context where the
`model` object is available.
"""
macro
randn
(
args
...
)
:
(
$
(
esc
(
:
randn
))(
$
(
esc
(
:
model
))
.
rng
,
$
(
map
(
esc
,
args
)
...
)))
end
"""
@rand(args...)
Return a random number or element from the sample, using the model RNG.
This is a utility wrapper that can only be used a context where the
`model` object is available.
"""
macro
rand
(
args
...
)
:
(
$
(
esc
(
:
rand
))(
$
(
esc
(
:
model
))
.
rng
,
$
(
map
(
esc
,
args
)
...
)))
end
"""
@shuffle!(collection)
Shuffle the given collection in place, using the model RNG.
This is a utility wrapper that can only be used a context where the
`model` object is available.
"""
macro
shuffle!
(
collection
)
:
(
$
(
esc
(
:
shuffle!
))(
$
(
esc
(
:
model
))
.
rng
,
$
(
esc
(
collection
))))
end
"""
@chance(odds)
Return true if a random number is less than the odds (0.0 <= `odds` <= 1.0),
using the model RNG. This is a utility wrapper that can only be used a context
where the `model` object is available.
"""
macro
chance
(
odds
)
:
(
$
(
esc
(
:
rand
))(
$
(
esc
(
:
model
))
.
rng
)
<
$
(
esc
(
odds
)))
end
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment