Skip to content

Commit 26d2a62

Browse files
committed
Add CLI function
1 parent 2b2c2d7 commit 26d2a62

1 file changed

Lines changed: 134 additions & 0 deletions

File tree

src/ParticlesMC.jl

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module ParticlesMC
22

33
using Arianna, StaticArrays
4+
using Comonicon, TOML
5+
using Comonicon: @main
46

57
export Particles
68
abstract type Particles <: AriannaSystem end
@@ -51,4 +53,136 @@ include("IO/IO.jl")
5153
using .IO: XYZ, EXYZ, LAMMPS, load_configuration, load_chains
5254
export XYZ, EXYZ, LAMMPS, load_configuration, load_chains
5355

56+
57+
"""
58+
ParticlesMC implemented in Comonicon.
59+
60+
# Arguments
61+
62+
- `params`: Path to the TOML parameter file.
63+
"""
64+
@main function particlesmc(params::String)
65+
if !isfile(params)
66+
error("Parameter file '$params' does not exist in the current path.")
67+
end
68+
params = TOML.parsefile(params)
69+
70+
# Extract system parameters
71+
system = params["system"]
72+
temperature = system["temperature"]
73+
density = system["density"]
74+
model = system["model"]
75+
config = system["config"]
76+
if !isfile(config)
77+
error("Configuration file '$config' does not exist in the current path.")
78+
end
79+
list_type = get(system, "list_type", "LinkedList") # optional field
80+
bonds = get(system, "bonds", nothing)
81+
82+
# Extract simulation parameters
83+
sim = params["simulation"]
84+
steps = sim["steps"]
85+
burn = get(sim, "burn", 0)
86+
seed = sim["seed"]
87+
parallel = sim["parallel"]
88+
output_path = sim["output_path"]
89+
90+
# Setup RNG and basic variables
91+
92+
# optional field
93+
94+
if bonds !== nothing
95+
chains = load_chains(config, args=Dict(
96+
"temperature" => [temperature],
97+
"density" => [density],
98+
"model" => [model],
99+
"list_type" => list_type,
100+
"bonds" => bonds,
101+
))
102+
else
103+
chains = load_chains(config, args=Dict(
104+
"temperature" => [temperature],
105+
"density" => [density],
106+
"model" => [model],
107+
"list_type" => list_type,
108+
))
109+
end
110+
algorithm_list = []
111+
# Setup moves
112+
pool = []
113+
for move in sim["move"]
114+
prob = move["probability"]
115+
policy = move["policy"]
116+
action = move["action"]
117+
parameters = get(move, "parameters", Dict())
118+
param_obj = ComponentArray()
119+
120+
# Create action object
121+
if action == "Displacement"
122+
action_obj = Displacement(0, zero(chains[1].box), 0.0)
123+
if "sigma" in keys(parameters)
124+
param_obj = ComponentArray= parameters["sigma"])
125+
else
126+
error("Missing parameter 'sigma' for action: $action")
127+
end
128+
if policy == "SimpleGaussian"
129+
policy_obj = SimpleGaussian()
130+
else
131+
error("Unsupported policy: $policy for action: $action")
132+
end
133+
else
134+
error("Unsupported action: $action")
135+
end
136+
137+
# Build move
138+
move_obj = Move(action_obj, policy_obj, param_obj, prob)
139+
push!(pool, move_obj)
140+
end
141+
push!(algorithm_list, (algorithm=Metropolis, pool=pool, seed=seed, parallel=parallel, sweepstep=length(chains[1])))
142+
143+
# Setup outputs
144+
for output in sim["output"]
145+
alg = output["algorithm"]
146+
scheduler_params = output["scheduler_params"]
147+
callbacks = get(output, "callbacks", [])
148+
fmt = get(output, "fmt", "XYZ")
149+
interval = scheduler_params["linear_interval"]
150+
if "log_base" in keys(scheduler_params)
151+
block = build_schedule(interval, 0, 2.0)
152+
sched = build_schedule(steps, burn, block)
153+
else
154+
sched = build_schedule(steps, burn, interval)
155+
end
156+
if alg == "StoreCallbacks"
157+
callbacks = map(c -> eval(Meta.parse("callback_$c")), callbacks)
158+
algorithm = (
159+
algorithm = eval(Meta.parse(alg)),
160+
callbacks = callbacks,
161+
scheduler = sched,
162+
)
163+
elseif alg == "StoreTrajectories" || alg == "StoreLastFrames"
164+
algorithm = (
165+
algorithm = eval(Meta.parse(alg)),
166+
scheduler = sched,
167+
fmt = eval(Meta.parse("$(fmt)()")),
168+
)
169+
elseif alg == "PrintTimeSteps"
170+
algorithm = (
171+
algorithm = eval(Meta.parse(alg)),
172+
scheduler = build_schedule(steps, burn, steps ÷ 10),
173+
)
174+
else
175+
error("Unsupported output algorithm: $alg")
176+
end
177+
push!(algorithm_list, algorithm)
178+
end
179+
M=1
180+
path = joinpath(output_path, "N$(chains[1].N)/T$(chains[1].temperature)/M$M")
181+
simulation = Simulation(chains, algorithm_list, steps; path=path, verbose=true)
182+
183+
# Run the simulation
184+
run!(simulation)
185+
186+
end
187+
54188
end

0 commit comments

Comments
 (0)