# Best practices for single script files

**URL:** https://discourse.julialang.org/t/best-practices-for-single-script-files/110258
**Category:** Performance
**Created:** [February 15, 2024, 3:43pm UTC](https://discourse.julialang.org/t/best-practices-for-single-script-files/110258 "2024-02-15T15:43:25Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Ved\_Mahajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ved_mahajan/32/21838_2.png) [@Ved\_Mahajan](https://discourse.julialang.org/u/Ved_Mahajan)
#### Post date: [February 15, 2024, 3:43pm UTC](https://discourse.julialang.org/t/best-practices-for-single-script-files/110258/1 "2024-02-15T15:43:25Z")

</div>

Hey all 👋

In the future I see myself writing a lot of single script files. They will be, almost exclusively, for analysing my molecular dynamics simulations. I imagine a rough skeleton of the script to be:

```julia
define some constants # May vary from script to script

read_trajectory_file()

loop through frames and atoms
compute some stuff
log the answers to some text file

```

Adding to it, I can imagine running the code like: `julia code.jl` on a HPC cluster.

How would the expirienced julia programmers code such a thing in the most efficient manner?  
I am excited to read the responses 🙂

Thank you in advanced.

Regards,  
Ved

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [February 15, 2024, 3:55pm UTC](https://discourse.julialang.org/t/best-practices-for-single-script-files/110258/2 "2024-02-15T15:55:21Z")

</div>

Follow the [performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/). Particularly, enclose your code inside functions and do not use (any, for simplicity) global variable.

Also, I recommend using environments. This is the workflow I use: [Development workflow · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/stable/workflow/#Environments).

By the way, since you are planing analyze MD simulations, take a look at MolSimToolkit.jl, particularly to the `Simulation` object described [here](https://m3g.github.io/MolSimToolkit.jl/stable/Developer/#MolSimToolkit.Simulation). This is a framework we built exactly to make MD simulation analysis easy.

(Also look at the [JuliaMolSim](https://github.com/JuliaMolSim) organization and, why not, the [other packages](https://github.com/m3g) we develop).

---

<div class="post-metadata">

### Author: ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)
#### Post date: [February 15, 2024, 5:36pm UTC](https://discourse.julialang.org/t/best-practices-for-single-script-files/110258/3 "2024-02-15T17:36:44Z")

</div>

You might be interested in DrWatson.jl

Docs: [Introduction · DrWatson.jl (juliadynamics.github.io)](https://juliadynamics.github.io/DrWatson.jl/stable/)

Introduction video: [DrWatson: The Perfect Sidekick to Your Scientific Inquiries | George Datseris | JuliaCon 2020 (youtube.com)](https://www.youtube.com/watch?v=jKATlEAu8eE)

---

<div class="post-metadata">

### Author: ![Ved\_Mahajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ved_mahajan/32/21838_2.png) [@Ved\_Mahajan](https://discourse.julialang.org/u/Ved_Mahajan)
#### Post date: [February 15, 2024, 5:53pm UTC](https://discourse.julialang.org/t/best-practices-for-single-script-files/110258/4 "2024-02-15T17:53:33Z")

</div>

Hey, thanks a lot for your reply. I will take a look at the links you have shared.

I mostly use GROMACS and LAMMPS for my work. I have used [Chemfiles.jl](https://chemfiles.org/Chemfiles.jl/latest/index.html) in past to read `.xtc` files and I used to parse `lammpstrj` file manually by code. Your package looks quite neat too. I would live to talk about it, but this is not a good place for that 🙂

I have preferred writing my own analysis code and use `packages` only to parse the compressed input files and I think I am going to keep writing my own analysis code.

Thanks again for your reply.

---

<div class="post-metadata">

### Author: ![Ved\_Mahajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ved_mahajan/32/21838_2.png) [@Ved\_Mahajan](https://discourse.julialang.org/u/Ved_Mahajan)
#### Post date: [February 15, 2024, 5:54pm UTC](https://discourse.julialang.org/t/best-practices-for-single-script-files/110258/5 "2024-02-15T17:54:41Z")

</div>

Thanks for your message. I have come across this package, but it looks like this package is very overkill for what I forsee. Nonetheless, this is a super cool package and maybe I will use it sometime later in my research xD.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [February 15, 2024, 5:57pm UTC](https://discourse.julialang.org/t/best-practices-for-single-script-files/110258/6 "2024-02-15T17:57:51Z")

</div>

> [@Ved\_Mahajan](#):
>
> I have used [Chemfiles.jl](https://chemfiles.org/Chemfiles.jl/latest/index.html) in past to read `.xtc` files

Oh yes, MolSimToolkit.jl uses Chemfiles.jl as well. It just makes the interface a little bit simpler. The idea is exactly making writing custom analysis scripts easy.

For example, this is how the distance between two groups of atoms is computed along the simulation:

```julia
using MolSimToolkit
using PDBTools

function distances(
    simulation::Simulation,
    indexes1::AbstractVector{Int},
    indexes2::AbstractVector{Int};
)
    distances = zeros(length(simulation))
    for (iframe, frame) in enumerate(simulation)
        coor = positions(frame)
        cm1 = center_of_mass(indexes1, simulation, coor)
        cm2 = center_of_mass(indexes2, simulation, coor)
        cm2_wrapped = wrap(cm2, cm1, unitcell(frame))
        d = norm(cm2_wrapped - cm1)
        distances[iframe] = d
    end
    return distances
end

simulation = Simulation(pdb_file, trajectory_file)
pdb = readPDB(pdb_file)
distances(
    simulation, # Simulation object
    findall(Select("resnum 1"), pdb), # indexes of atoms of residue 1
    findall(Select("resnum 10"), pdb) # indexes of atoms of residue 10
)

```

---

<div class="post-metadata">

### Author: ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)
#### Post date: [February 15, 2024, 6:43pm UTC](https://discourse.julialang.org/t/best-practices-for-single-script-files/110258/7 "2024-02-15T18:43:19Z")

</div>

> [@Ved\_Mahajan](#):
>
> In the future I see myself writing a lot of single script files.

Even with “time to first X” having gotten a lot better in recent version of Julia, I wouldn’t generally recommend this. It’s a type of workflow that works with compiled languages like Fortran, or even with Python, which has a small “startup cost”. Julia benefits from long-running processes, so adjust your workflows to that.

> I can imagine running the code like: `julia code.jl` on a HPC cluster.

That’s fine as long as `code.jl` has a runtime of minutes at the very least (better hours or days – whatever the runtime of a typical HPC job). That is, don’t submit a job file that has a hundred different `julia code.jl` calls.

When working locally, I would recommend a (Jupyter) notebook workflow, where you have a long-running kernel. Or using the REPL. Even if you have a lot of `code.jl` file for small calculations or to produce plots, it’s much better to do `include("code.jl")` in a long-running REPL than to run `julia code.jl` in your shell.

Similarly, for an HPC run: have a single `job.jl` file which can still `include` smaller “script” files, but uses a single long-running Julia process. You can still use Julia’s excellent multi-threading support to parallelize things and to best utilize your HPC node.

---

<div class="post-metadata">

### Author: ![Ved\_Mahajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ved_mahajan/32/21838_2.png) [@Ved\_Mahajan](https://discourse.julialang.org/u/Ved_Mahajan)
#### Post date: [February 16, 2024, 1:31pm UTC](https://discourse.julialang.org/t/best-practices-for-single-script-files/110258/8 "2024-02-16T13:31:11Z")

</div>

Thank you so much for your message. This is exacly what I waned to know. I have been using FORTRAN for some time now, but I can see that my codes are becoming more complex and I would spend a lot of time coding things up. Julia would make coding things up quite fast.

Most of the codes would be mostly have runtime of more than a few minutes for sure.

> but uses a single long-running Julia process.

I would have to see how to do this in my HPC. If you have any idea, then please do let me know.

Thank you once again, this was really helpful.

---

<div class="post-metadata">

### Author: ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)
#### Post date: [February 16, 2024, 8:25pm UTC](https://discourse.julialang.org/t/best-practices-for-single-script-files/110258/9 "2024-02-16T20:25:04Z")

</div>

If you search this forum, you will find previous threads on using Julia on a cluster, e.g.,

> [@Julia on a cluster using SLURM, dependencies](https://discourse.julialang.org/t/julia-on-a-cluster-using-slurm-dependencies/89737/10):
>
> Do I need my script to call the project module? That is do I need it to be of the form module TestProject greet() = print(“Hello World!”) using Statistics using Distributions open(“./testing\_write\_proj.txt”, “w”) do file write(file, “worked 1:03 edit”) end end # module TestProject

> [@How to run Julia on Cluster?](https://discourse.julialang.org/t/how-to-run-julia-on-cluster/56551/6):
>
> This is what the last two lines in my example accomplish: I upload the code to the project directory with rsync. That way, I have a Project.toml and Manifest.toml in place that match my (tested) local version of the code. All the jl file needs to do then is using Pkg Pkg.instantiate() # in case some package is missing on the remote using MyPackage command\_I\_want\_to\_run()

You will also find general documentation on the web, e.g.,

> **[SLURM Job Array Julia Example | RCpedia](https://rcpedia.stanford.edu/topicGuides/jobArrayJuliaExample.html)**
>
> Site / page description

> **[Julia on the HPC Clusters](https://researchcomputing.princeton.edu/support/knowledge-base/julia)**
>
> This page describes getting started with running Julia on the HPC clusters.

> **[Using Julia](https://www.carc.usc.edu/user-information/user-guides/software-and-programming/julia/)**
>
> USC's Center for Advanced Research Computing supports computational research and data-driven solutions.

Lastly, the HPC staff of the cluster you’re planning to use should be able to give you advice for their specific system.

My general recommendation was that your job script should only call `julia` once. Beyond that, calling `julia` on the cluster isn’t that much different from running it on your workstation. Except maybe that you should be more aware of the exact resources you’re going to use, and carefully manage the number of processes/threads. The `JULIA_EXCLUSIVE` environment variable might be useful on a cluster.

---

<div class="post-metadata">

### Author: ![Ved\_Mahajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ved_mahajan/32/21838_2.png) [@Ved\_Mahajan](https://discourse.julialang.org/u/Ved_Mahajan)
#### Post date: [February 17, 2024, 4:38pm UTC](https://discourse.julialang.org/t/best-practices-for-single-script-files/110258/10 "2024-02-17T16:38:49Z")

</div>

Thank you so much for such a detailed answer. This is super helpful.
