Mathematica
In this section:
Description
According to the page of Mathematica, Mathematica defines the state of the art in technical computing, providing the leading computation environment for millions of innovators, educators, and students worldwide.
Available Versions
mathematica/9.0
mathematica/10.4 (default)
Serial Job Submission
submit_mathematica_serial.sh
#!/bin/bash
#SBATCH -J mathematica_serial
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 05:00:00
#SBATCH --mem=8G
export INPUT="script.m"
export OUTPUT="script.out"
module load mathematica
job-nanny math -script script.m > script.out
Job Submission with MathKernel
submit_mathematica_kernel.sh
#!/bin/bash
#SBATCH -J mathematica_kernel
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 12:00:00
#SBATCH --mem=16G
export INPUT="calculation.m"
export OUTPUT="calculation.out"
module load mathematica
# Start kernel and execute
job-nanny math -noprompt -run "<<calculation.m" > calculation.out
Job Submission with Parallelism
submit_mathematica_parallel.sh
#!/bin/bash
#SBATCH -J mathematica_parallel
#SBATCH -N 1
#SBATCH -c 8
#SBATCH -t 24:00:00
#SBATCH --mem=32G
export INPUT="parallel.m"
export OUTPUT="parallel.out"
module load mathematica
# Create script with parallel configuration
cat > run_parallel.m << EOF
LaunchKernels[$SLURM_CPUS_PER_TASK]
Get["parallel.m"]
CloseKernels[]
EOF
job-nanny math -noprompt -run "<<run_parallel.m" > parallel.out
Mathematica Script Example
calculation.m
(* Intensive numerical calculation *)
n = 1000;
matrix = RandomReal[{0,1}, {n, n}];
eigenvalues = Eigenvalues[matrix];
Print["Largest eigenvalue: ", Max[eigenvalues]];
Print["Smallest eigenvalue: ", Min[eigenvalues]];
(* Numerical integration *)
f[x_] := Sin[x]^2 * Exp[-x/10];
integral = NIntegrate[f[x], {x, 0, 100}];
Print["Integral: ", integral];
(* Curve fitting *)
data = Table[{x, 2*Sin[x] + RandomReal[{-0.1,0.1}]}, {x, 0, 2Pi, 0.1}];
fit = NonlinearModelFit[data, a*Sin[b*x + c], {a, b, c}, x];
Print["Fit parameters: ", fit["BestFitParameters"]];
Export["results.txt", {eigenvalues[[1;;10]], integral, fit["BestFit"]}];
Job Array for Multiple Parameters
submit_mathematica_array.sh
#!/bin/bash
#SBATCH -J mathematica_array
#SBATCH --array=1-10
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 12:00:00
#SBATCH --mem=8G
PARAM=$SLURM_ARRAY_TASK_ID
export INPUT="template.m"
export OUTPUT="run_${PARAM}/"
module load mathematica
mkdir -p run_${PARAM}
cd run_${PARAM}
# Create script with specific parameter
cat > run.m << EOF
parameter = $PARAM;
Print["Running with parameter = ", parameter];
(* Parameter-dependent calculation *)
result = NIntegrate[Sin[parameter*x]^2, {x, 0, 10}];
Print["Result: ", result];
Export["result_${PARAM}.txt", {parameter, result}];
EOF
job-nanny math -noprompt -run "<<run.m" > output_${PARAM}.log
Batch Data Processing
submit_mathematica_batch.sh
#!/bin/bash
#SBATCH -J mathematica_batch
#SBATCH -N 1
#SBATCH -c 4
#SBATCH -t 24:00:00
#SBATCH --mem=16G
export INPUT="data_*.csv"
export OUTPUT="analysis/"
module load mathematica
mkdir -p analysis
# Create script to process all files
cat > batch_process.m << EOF
files = FileNames["data_*.csv"];
results = {};
Do[
Print["Processing ", file];
data = Import[file, "CSV"];
(* Analysis *)
means = Mean[data];
stds = StandardDeviation[data];
AppendTo[results, {file, means, stds}];
(* Save individual results *)
Export["analysis/" <> FileBaseName[file] <> "_stats.txt",
{means, stds}, "Table"],
{file, files}];
(* General summary *)
Export["analysis/summary.txt", results, "Table"];
EOF
job-nanny math -noprompt -run "<<batch_process.m"
Data Visualization
submit_mathematica_plot.sh
#!/bin/bash
#SBATCH -J mathematica_plot
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 02:00:00
#SBATCH --mem=4G
export INPUT="data.txt"
export OUTPUT="plot.pdf"
module load mathematica
cat > plot.m << EOF
data = Import["data.txt", "Table"];
(* 2D plot *)
plot1 = ListPlot[data, PlotStyle -> PointSize[0.015],
AxesLabel -> {"X", "Y"},
PlotLabel -> "Experimental data"];
(* Histogram *)
plot2 = Histogram[Flatten[data], 30,
AxesLabel -> {"Value", "Frequency"}];
(* Export *)
Export["plot.pdf", GraphicsGrid[{{plot1}, {plot2}}]];
EOF
job-nanny math -noprompt -run "<<plot.m"
References
Documentation: https://reference.wolfram.com/language/
Tutorials: https://www.wolfram.com/support/learn/
Resources Center: https://www.wolfram.com/support/
See also
MATLAB - Numerical computing environment
R - Statistical computing
Running Simulations - How to submit jobs