Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR to update Julia folder #41

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
16 changes: 4 additions & 12 deletions Languages/Julia/Example1/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
### Purpose:

This example illustrates the use of Julia on the Harvard University FAS cluster. Specifically, it evaluates PI via a Monte-Carlo method.
This example illustrates the use of Julia on the Harvard University
FAS cluster. Specifically, it evaluates PI via a Monte-Carlo method.


### Contents:

* <code>pi\_monte\_carlo.jl</code>: Julia source code
* <code>run.sbatch</code>: Batch-job submission script

### Julia code:

```julia
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Program: pi_monte_carlo.jl
Expand Down Expand Up @@ -39,7 +37,6 @@ end
```

### Example Batch-Job Submission Script:

```bash
#!/bin/bash
#SBATCH -J pi_monte_carlo
Expand All @@ -51,21 +48,16 @@ end
#SBATCH -t 0-00:30
#SBATCH --mem=2G

# Set up Julia and run the program
export PATH=$PATH:/n/holylabs/LABS/jharvard_lab/Users/jharvard/software/julia-1.9.3/bin
srun -n 1 -c 1 julia pi_monte_carlo.jl
# Run the program using Julia
julia pi_monte_carlo.jl
```

**NOTE:** Please remember to point the `PATH` environmental variable to the actual location of your Julia installation.

### Example Usage:

```bash
sbatch run.sbatch
```

### Example Output:

```bash
$ cat pi_monte_carlo.out
N = 1000: PI = 3.228
Expand Down
3 changes: 2 additions & 1 deletion Languages/Julia/Example1/pi_monte_carlo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ function montepi(n::Int)
end

# Main program
for n in 10 .^(3: 8)
for i in 3: 8
n = 10^i
p = montepi(n)
println("N = $n: PI = $p")
end
6 changes: 0 additions & 6 deletions Languages/Julia/Example1/pi_monte_carlo.out

This file was deleted.

6 changes: 2 additions & 4 deletions Languages/Julia/Example1/run.sbatch
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@
#SBATCH -t 0-00:30
#SBATCH --mem=2G

# Set up Julia and run the program
export PATH=$PATH:/n/holylabs/LABS/jharvard_lab/Users/jharvard/software/julia-1.9.3/bin
srun -n 1 -c 1 julia pi_monte_carlo.jl

# Run the program using Julia
julia pi_monte_carlo.jl
125 changes: 73 additions & 52 deletions Languages/Julia/Example2/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
### Purpose:

This example illustrates solving differential equations numerically in Julia. Specifically, it solves an ODE after an example taken from [this](https://sam-dolan.sites.sheffield.ac.uk/mas212-course/sample-notebooks/ode_example) Python notebook, using [DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/)
This example illustrates solving differential equations numerically in
Julia. Specifically, it solves an ODE after an example taken from
[this](https://sam-dolan.sites.sheffield.ac.uk/mas212-course/sample-notebooks/ode_example)
Python notebook, using
[DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/)


### Contents:

* <code>ode\_test.jl</code>: Julia source code
* <code>figure.py</code>: Python script for generating the figure
* <code>run.sbatch</code>: Batch-job submission script
* <code>results.dat</code>: Numeric results
* <code>figure.png</code>: Figure of ODE's solution
* <code>figure.py</code>: Python script for generating the figure

### Julia code:

```julia
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Program: ode_test.jl
Expand All @@ -22,6 +22,7 @@ using SimpleDiffEq
using Printf
using Plots
using Plots.PlotMeasures
using PyCall

# --- Define the problem ---
f(u, p, t) = t - u
Expand Down Expand Up @@ -52,17 +53,41 @@ for i = 1: size(sol.t)[1]
@printf(fo, "%8.4f %10.6f %10.6f\n", x, y, y_exact)
end
close(fo)

pyimport("numpy")

# --- Run the Python script to plot the result ---
@pyinclude("figure.py")
```

> **Note:** You may need to install the `DifferentialEquations`, `SimpleDiffEq` and `Plots` packages. Use the following command inside the Julia REPL:
> **Note:** You may need to install the `DifferentialEquations`,
`SimpleDiffEq`, `Plots`, `PyCall`, `Conda`, and `matplotlib`
packages. Use the following command inside Julia's full-featured
interactive command-line REPL (read-eval-print loop) to adding these packages.

```julia
julia> using Pkg
julia> Pkg.add("DifferentialEquations")
julia> Pkg.add("SimpleDiffEq")
julia> Pkg.add("Plots")
julia> Pkg.add("Conda")
julia> ENV["PYTHON"]=""
julia> Pkg.build("PyCall")
julia> using Conda
julia> Conda.add("matplotlib")
```

> **Note:** The `PYTHON` environmental variable above has been set to
`blank` prior to building the `PyCall` package. This is to
override the default behavior of `PyCall`, which is to use the
system's default Python environment in Linux, and instead install
Julia's "private" version of Python. You can find more details on
how to install `PyCall` at a desired location and call Python
functions from a Julia program on
[Github](https://github.com/JuliaPy/PyCall.jl) and
[Stackoverflow](
https://stackoverflow.com/questions/66648841/how-to-call-a-python-function-from-a-julia-program)

### Example Batch-Job Submission Script:

```bash
Expand All @@ -76,56 +101,15 @@ julia> Pkg.add("Plots")
#SBATCH -t 0-00:30
#SBATCH --mem=4G

# Set up Julia and run the program
export PATH=$PATH:/n/holylabs/LABS/jharvard_lab/Users/jharvard/software/julia-1.9.3/bin
srun -n 1 -c 1 julia ode_test.jl
# Run the program using Julia
julia ode_test.jl
```
**NOTE:** Please remember to point the `PATH` environmental variable to the actual location of your Julia installation.

### Example Usage:

```bash
sbatch run.sbatch
```

### Example Output:

```bash
$ cat results.dat
Time Numeric Exact
------------------------------
0.0000 1.000000 1.000000
0.2000 0.837462 0.837462
0.4000 0.740639 0.740640
0.6000 0.697624 0.697623
0.8000 0.698660 0.698658
1.0000 0.735763 0.735759
1.2000 0.802393 0.802388
1.4000 0.893198 0.893194
1.6000 1.003802 1.003793
1.8000 1.130613 1.130598
2.0000 1.270681 1.270671
2.2000 1.421625 1.421606
2.4000 1.581455 1.581436
2.6000 1.748569 1.748547
2.8000 1.921645 1.921620
3.0000 2.099603 2.099574
3.2000 2.281551 2.281524
3.4000 2.466785 2.466747
3.6000 2.654677 2.654647
3.8000 2.844781 2.844742
4.0000 3.036672 3.036631
4.2000 3.230027 3.229991
4.4000 3.424601 3.424555
4.6000 3.620150 3.620104
4.8000 3.816498 3.816459
5.0000 4.013510 4.013476
```

### Figure of Solution:

<img src="figure.png" alt="solution" width="500"/>

### Python script for generating the figure:

```python
Expand All @@ -136,7 +120,6 @@ import os
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

def rc_params():
Expand Down Expand Up @@ -195,6 +178,44 @@ plt.legend(fontsize=15, loc="upper left", shadow=True, fancybox=True)
plt.savefig(fig_path, format='png', dpi=100, bbox_inches='tight')
```

### Example Output:
```bash
$ cat results.dat
Time Numeric Exact
------------------------------
0.0000 1.000000 1.000000
0.2000 0.837462 0.837462
0.4000 0.740639 0.740640
0.6000 0.697624 0.697623
0.8000 0.698660 0.698658
1.0000 0.735763 0.735759
1.2000 0.802393 0.802388
1.4000 0.893198 0.893194
1.6000 1.003802 1.003793
1.8000 1.130613 1.130598
2.0000 1.270681 1.270671
2.2000 1.421625 1.421606
2.4000 1.581455 1.581436
2.6000 1.748569 1.748547
2.8000 1.921645 1.921620
3.0000 2.099603 2.099574
3.2000 2.281551 2.281524
3.4000 2.466785 2.466747
3.6000 2.654677 2.654647
3.8000 2.844781 2.844742
4.0000 3.036672 3.036631
4.2000 3.230027 3.229991
4.4000 3.424601 3.424555
4.6000 3.620150 3.620104
4.8000 3.816498 3.816459
5.0000 4.013510 4.013476
```

### Figure of Solution:
<img src="figure.png" alt="solution" width="500"/>

### References:

* [Official **DifferentialEquations.jl** Documentation](https://docs.sciml.ai/DiffEqDocs/stable/)
* [Official **DifferentialEquations.jl** Documentation](https://docs.sciml.ai/DiffEqDocs/stable/)
* [PyCall](https://github.com/JuliaPy/PyCall.jl)
* [Pycall to call Python code](https://stackoverflow.com/questions/66648841/how-to-call-a-python-function-from-a-julia-program)
1 change: 0 additions & 1 deletion Languages/Julia/Example2/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

def rc_params():
Expand Down
5 changes: 5 additions & 0 deletions Languages/Julia/Example2/ode_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using SimpleDiffEq
using Printf
using Plots
using Plots.PlotMeasures
using PyCall

# --- Define the problem ---
f(u, p, t) = t - u
Expand Down Expand Up @@ -37,3 +38,7 @@ for i = 1: size(sol.t)[1]
end
close(fo)

pyimport("numpy")

# --- Run the Python script to plot the result ---
@pyinclude("figure.py")
28 changes: 0 additions & 28 deletions Languages/Julia/Example2/results.dat

This file was deleted.

6 changes: 2 additions & 4 deletions Languages/Julia/Example2/run.sbatch
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@
#SBATCH -t 0-00:30
#SBATCH --mem=4G

# Set up Julia and run the program
#export PATH=$PATH:/n/holylabs/LABS/jharvard_lab/Users/jharvard/software/julia-1.9.3/bin
export PATH=$PATH:/n/home06/pkrastev/holyscratch01/Work/Julia/julia-1.9.3/bin
srun -n 1 -c 1 julia ode_test.jl
# Run the program using Julia
julia ode_test.jl
Loading