Skip to content

Commit

Permalink
update the mathematica package for pcParallelize[]
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouhz1992 committed Apr 12, 2022
1 parent 262be9d commit 7cec76d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 34 deletions.
36 changes: 8 additions & 28 deletions doc/readthedocs/tutorials/mathematica/tutmathematica.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ Here you can find some tutorials on using Mathematica for post-processing.
Loading the package
===================

There are two ways of telling Mathematica where the package is:

1. Modifying ``init.m`` so that the path to the package is automatically added to the ``$Path`` variable in Mathematica.
We need to modify ``init.m`` so that the path to the package is automatically added to the ``$Path`` variable in Mathematica.
First, type

.. code::
Expand All @@ -32,33 +30,15 @@ Mathematica will not search in subdirectories, so make sure the package in right
After updating ``init.m``, restart the Mathematica kernel (``Evaluation`` -> ``Quit Kernel``).
To use the package, call ``Needs["pc`"]`` and then ``pcInitialize[]`` in a notebook or a script.

Each time you have updated the data, remember to do ``pcInitialize[]`` or ``pcReload[]`` again.
These two functions remove some persistent variables defined.

2. Alternatively, if you don't want to modify ``init.m``, you may also call

.. code::
Needs["pc`","path/to/this/package"]
To use the package on subkernels, call ``pcParallelize[n]``.
This will launch ``n`` subkernels and load the package on each of them.
Then you can do things like ``ParallelTable[readTS[...],...]``.
Only loading the package on the master kernel is not enough.
See the discussions `here <https://mathematica.stackexchange.com/questions/11595/package-found-with-needs-but-not-with-parallelneeds>`_, and the 'Possible issues' section `here <https://reference.wolfram.com/language/ref/ParallelNeeds.html>`_.

each time.

.. admonition:: Note:

To run the package on subkernels you may need to do something like:

.. code::
LaunchKernels[];
AppendTo[$Path, "your/pencil/home/mathematica"]//ParallelEvaluate;
Needs["pc`"]
ParallelNeeds["pc`"]
Then you can do things like ``ParallelTable[readTS[...],...]``.
Note that both ``Needs`` on the master kernel and ``ParallelNeeds`` on subkernels are needed.
See also the discussions `here <https://mathematica.stackexchange.com/questions/11595/package-found-with-needs-but-not-with-parallelneeds>`_, and the 'Possible issues' section
`here <https://reference.wolfram.com/language/ref/ParallelNeeds.html>`_.

Each time you have updated the data, remember to do ``pcInitialize[]`` and ``pcParallelize[n]`` again.
These two functions remove some persistent variables defined.


Pencil Code Commands in General
Expand Down
20 changes: 14 additions & 6 deletions mathematica/pc.wl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ BeginPackage["pc`","pcUtils`",


pcInitialize::usage="pcInitialize[] is equivalent to (pcReload[];pcPlotStyle[]).";
pcParallelize::usage="pcParallelize[] sets up the package for multi kernels.";
pcParallelize::usage="pcParallelize[n] sets up the package for multiple kernels.
Input:
n: Integer. Optional. Launches n subkernels. If not provided then launches all
configured subkernels.
";

pcFunctions::usage="pcFunctions[] returns a list of all available functions in this package."

Expand All @@ -41,15 +45,19 @@ Begin["`Private`"]
(*One-click initialization*)


pcInitialize[]:=(pcReload[];pcPlotStyle[];)
pcParallelize[]:=(
LaunchKernels[]//Quiet;
pcDirectory=$InputFileName//DirectoryName;
pcInitialize[]:=Module[{},
pcReload[];pcPlotStyle[];
]
pcParallelize[n_|PatternSequence[]]:=With[{dir=pcDirectory},
CloseKernels[];
LaunchKernels[n]//Quiet;
ParallelEvaluate[
AppendTo[$Path, "/Users/hzhou/Work/pencilCode/pencil-code/mathematica"];
AppendTo[$Path,dir];
Needs["pc`"];
pcInitialize[]
];
)
]


(* ::Section:: *)
Expand Down

0 comments on commit 7cec76d

Please sign in to comment.