Skip to content

Commit

Permalink
mathematica package
Browse files Browse the repository at this point in the history
changed default trimming to False in readVARN; improved some help messages
  • Loading branch information
zhouhz1992 committed Aug 17, 2021
1 parent 1ce9fdf commit 64ccc36
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
2 changes: 1 addition & 1 deletion doc/readthedocs/tutorials/mathematica/tutmathematica.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ For example,
{t,urms} = readTS[sim,"t","urms"]
Make sure that ``Length`` of the left side is equal to the number of ``var``s; otherwise Mathematica will complain.
Make sure that ``Length`` of the left side is equal to the number of ``var``; otherwise Mathematica will complain.

To plot the data, you can say

Expand Down
30 changes: 22 additions & 8 deletions mathematica/pcDerivative.wl
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,75 @@ BeginPackage["pcDerivative`"]
(*Usage messages*)


der::usage="der[f,mx,my,mz,di,i,n] returns the nth derivative w.r.t. i of f.
der::usage="der[f,mx,my,mz,ghi,di,i,n] returns the nth derivative w.r.t. i of f.
Input:
f: 1D List of length mx*my*mz. Must be sorted by coordiantes
ghi: number of ghost cells
di: grid spacing
i: 1, 2, or 3 as x, y, or z derivative
Output:
\!\(\*SuperscriptBox[\(d\), \(n\)]\)f/\!\(\*SuperscriptBox[\(di\), \(n\)]\) as a mx*my*mz length List"

der2::usage="der2[f,mx,my,mz,{di,dj},{i,j}] returns d^2f/didj.
der2::usage="der2[f,mx,my,mz,{ghi,ghj},{di,dj},{i,j}] returns d^2f/didj.
Input:
f: 1D List of length mx*my*mz. Must be sorted by coordiantes
ghi,ghij: numbers of ghost cells
di,dj: grid spacing
i,j: 1, 2, or 3 as x, y, or z derivative
Output:
d^2f/didj as a mx*my*mz length List"

grad::usage="grad[f,mx,my,mz,dx,dy,dz] returns the gradient of f.
grad::usage="grad[f,mx,my,mz,ghx,ghy,ghz,dx,dy,dz] returns the gradient of f.
Input:
f: 1D List of length mx*my*mz. Must be sorted by coordiantes
ghx,ghy,ghz: numbers of ghost cells
dx,dy,dz: grid spacings
Output:
{df/dx,df/dy,df/dz}, each as an mx*my*mz length List"

div::usage="div[{fx,fy,fz},mx,my,mz,dx,dy,dz] returns the divergence of f.
div::usage="div[{fx,fy,fz},mx,my,mz,ghx,ghy,ghz,dx,dy,dz] returns the divergence of f.
Input:
fx,fy,fz: three components of f, each as a 1D List of length mx*my*mz.
Must be sorted by coordiantes
ghx,ghy,ghz: numbers of ghost cells
dx,dy,dz: grid spacings
Output:
div(f), as an mx*my*mz length List"

curl::usage="curl[{fx,fy,fz},mx,my,mz,dx,dy,dz] returns the curl of f.
curl::usage="curl[{fx,fy,fz},mx,my,mz,ghx,ghy,ghz,dx,dy,dz] returns the curl of f.
Input:
fx,fy,fz: three components of f, each as a 1D List of length mx*my*mz.
Must be sorted by coordiantes
ghx,ghy,ghz: numbers of ghost cells
dx,dy,dz: grid spacings
Output:
curl(f), as three mx*my*mz length Lists"

curl2::usage="curl2[{fx,fy,fz},mx,my,mz,dx,dy,dz] returns the curl of curl of f.
curl2::usage="curl2[{fx,fy,fz},mx,my,mz,ghx,ghy,ghz,dx,dy,dz] returns the curl of curl of f.
Input:
fx,fy,fz: three components of f, each as a 1D List of length mx*my*mz.
Must be sorted by coordiantes
ghx,ghy,ghz: numbers of ghost cells
dx,dy,dz: grid spacings
Output:
curl(curl(f)), as three mx*my*mz length Lists"

laplacian::usage="laplacian[f,mx,my,mz,dx,dy,dz] returns the laplacian of f.
laplacian::usage="laplacian[f,mx,my,mz,ghx,ghy,ghz,dx,dy,dz] returns the laplacian of f.
Input:
f: 1D List of length mx*my*mz. Must be sorted by coordiantes
ghx,ghy,ghz: numbers of ghost cells
dx,dy,dz: grid spacings
Output:
laplacian(f), as three mx*my*mz length Lists"

gradDiv;
gradDiv::usage="gradDiv[{fx,fy,fz},mx,my,mz,ghx,ghy,ghz,dx,dy,dz] returns grad(div f).
Input:
fx,fy,fz: three components of f, each as a 1D List of length mx*my*mz.
Must be sorted by coordiantes
ghx,ghy,ghz: numbers of ghost cells
dx,dy,dz: grid spacings
Output:
grad(div f), as three mx*my*mz length Lists"


Begin["`Private`"]
Expand Down
33 changes: 18 additions & 15 deletions mathematica/pcReadVAR.wl
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ readVARN::usage="readVARN[sim,iVAR,addons,\"ltrim\"->True] reads the iVARth VARN
from all processors.
Input:
sim: String. Directory of the run
iVAR: index of the VARN file, starting from 0
addons: List. Optional. Specifies variables need to be computed; e.g., {\"oo\",\"bb\"}
\"ltrim\": Options. Default value =True. If =True then trim ghost zones.
iVAR: Integer. Index of the VARN file, starting from 0
addons: List. Optional. Specifies magical variables need to be computed; e.g., {\"oo\",\"bb\"}.
Magical variables in the result are assigned with long names, e.g., \"ooo1\" and \"bbb1\"
to avoid shadowing the written auxiliaries.
\"ltrim\": Option. Default value ->False.
If ->True then trim ghost zones.
If ->\"More\" then trim again nghost number of cells at the boundaries; i.e., 2*nghost cells are
removed at each boundary
Output:
An Association. Use readVARN[sim,iVAR]//Keys to extract its keys"

Expand Down Expand Up @@ -164,22 +169,20 @@ readVARNRaw[sim_,iVAR_]:=With[{
Table[var->Flatten[data[#][var]&/@Range[0,nproc-1]],{var,allvars}]//Association
]]

Options[readVARN]={"ltrim"->True}
Options[readVARN]={"ltrim"->False}
readVARN[sim_,iVAR_,addOn_List:{},OptionsPattern[]]:=With[{
vars=varName[sim]//Keys,
raw=readVARNRaw[sim,iVAR],
ltrim=OptionValue["ltrim"]
},
Module[{grid,values,merge,tmp,mx,my,mz,dx,dy,dz,gh1,gh2,gh3,trim,oo,bb,jj},
readVARN::derivWOPeri="Warning: ghost zone values are incorrect when lperi!={T,T,T}";
readVARN::ghvalues="Warning: ghost zone values are computed assuming periodic boundary conditions.";
PrintTemporary["Combining files..."];

grid=Transpose[raw/@{"x","y","z"}];
values=raw/@vars;
oo=bb=jj={{},{},{}};

(*tmp=Transpose@SortBy[Transpose[{grid,Sequence@@values}]//DeleteDuplicates,First];*)
(*tmp={grid,Sequence@@values}//Transpose//Union//Transpose;*)
merge[{l__List}]/;Length[{l}]>=2:=Flatten[MaximalBy[#,Abs,1]&/@Transpose[{l}],1];
merge[{l_List}]/;Length[{l}]==1:=l;
tmp=Transpose[merge/@GatherBy[{grid,Sequence@@values}//Transpose//Union,First]];
Expand All @@ -198,14 +201,14 @@ readVARN[sim_,iVAR_,addOn_List:{},OptionsPattern[]]:=With[{
jj=curl2[values[[varName[sim]/@{"aa1","aa2","aa3"}]],mx,my,mz,gh1,gh2,gh3,dx,dy,dz]
];

If[ltrim,
trim[f_]:=If[f=={},{},
ArrayReshape[f,{mx,my,mz}][[gh1+1;;mx-gh1,gh2+1;;my-gh2,gh3+1;;mz-gh3]]//Flatten
];
{grid,values,oo,bb,jj}=Map[trim,{grid,values,oo,bb,jj},{2}],
(*else*)
Message[readVARN::derivWOPeri]
trim[{}]={};
trim[f_]:=Switch[ltrim,
False,f,
True,ArrayReshape[f,{mx,my,mz}][[gh1+1;;mx-gh1,gh2+1;;my-gh2,gh3+1;;mz-gh3]]//Flatten,
"More",ArrayReshape[f,{mx,my,mz}][[2gh1+1;;mx-2gh1,2gh2+1;;my-2gh2,2gh3+1;;mz-2gh3]]//Flatten
];
If[ltrim==False,Message[readVARN::ghvalues]];
{grid,values,oo,bb,jj}=Map[trim,{grid,values,oo,bb,jj},{2}];

Join[
Thread[{"t","dx","dy","dz","deltay"}->
Expand All @@ -214,7 +217,7 @@ readVARN[sim_,iVAR_,addOn_List:{},OptionsPattern[]]:=With[{
readDim[sim]/@If[ltrim,{"nx","ny","nz"},{"mx","my","mz"}]],
Thread[{"x","y","z"}->grid],
Thread[vars->values],
(*long name to avoid shadowing the written auxillaries*)
(*long name to avoid shadowing the written auxiliaries*)
Thread[{"ooo1","ooo2","ooo3"}->oo],
Thread[{"bbb1","bbb2","bbb3"}->bb],
Thread[{"jjj1","jjj2","jjj3"}->jj]
Expand Down

0 comments on commit 64ccc36

Please sign in to comment.