7
$\begingroup$

I would like to wrap my AceFEM analysis in one function and include it in a Mathematica package (.wl). I have followed the instructions on how to create packages from documentation and saved the following code as "MyPackage.wl".

(* AceFEM` context needs to be declared before defining my functions. *)

BeginPackage["MyPackage`",{"AceFEM`"}];

 tensionTest::usage="tensionTest[number of elements] does simple tension test and returns force vs. displacement curve.";

Begin["`Private`"];

 tensionTest[n_Integer]:=Module[{nstep,result}
  , SMTInputData[]
  ; SMTAddDomain[
        "test","OL:SEPEQ1DFHYQ1NeoHooke",{"E *"->1000.,"ν *"->0.3}
    ]
  ; SMTMesh["test","Q1",{n,n},{{{0,0},{1,0}},{{0,1},{1,1}}}]
  ; SMTAddEssentialBoundary[{ "X"==0&,1->0,2->0}]
  ; SMTAddEssentialBoundary[{ "X"==1&,1->1,2->0}]
  ; SMTAnalysis[]
  ; result={{0.,0.}}
  ; nstep=10
  ; Do[
        SMTNextStep["Δλ"->(1./nstep)]
      ; While[
            SMTConvergence[10^-8,15]
          , SMTNewtonIteration[];
        ]
      ; AppendTo[result
         , { SMTPostData["u",{1,1}]
           , Total[ SMTResidual["X"==1&] ][[1]]
           }
        ]
      , {i,1,nstep}
    ]
  ; result
 ]

End[];

EndPackage[];

Then I call this package from the notebook saved in the same directory. The package loads, AceFEM side window opens, but when I want to use the function tensionTest I get this strange error. I don't know why the function from the package doesn't work, because I am quite sure its syntax is correct?

Get["MyPackage.wl", Path -> NotebookDirectory[]]

tensionTest[10]

enter image description here

$\endgroup$

1 Answer 1

7
$\begingroup$

The solution is to add another (undocumented) context "AceCommon" to the list as the second argument of BeginPackage.

BeginPackage["MyPackage`", {"AceFEM`", "AceCommon`"}];

It seems that from all AceFEM symbols used in your function only SMTInputData is not defined in "AceFEM" context. Therefore when the package is called without all other necessary contexts, the symbol SMTInputData doesn't have the correct definition, analysis doesn't get initialized properly and fails in an unexpected way.

Get["MyPackage.wl", Path -> NotebookDirectory[]]

{Context[SMTInputData], Context[SMTAddDomain]}
(* {"AceCommon`", "AceFEM`"} *)

With this small fix in the package your function works as expected.

ListPlot[
  tensionTest[10],
  AxesLabel ->{"Displacement","Force"},AxesStyle->14,ImageSize -> 400
]

displacement-force

$\endgroup$
1
  • $\begingroup$ Does anyone know how to run the package from the example above (with specific number of elements set in the package) using of batch file? $\endgroup$
    – KratosMath
    Commented Oct 15, 2020 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.