2

I have a physics simulation program which I run through a terminal window (I use a Mac) as shown below:

user$ programName inputfile.ele

I would like run a *.sh script that runs the simulation from Mathematica. I tried using Run but had no luck, though I was able to successfully follow through this example (How to run a shell script from inside Mathematica).

I am looking for advice on how to go about running the simulation from Mathematica. Ultimately, I would like to create a Do-loop and have Mathematica run the following script with different t2 values:

Do[
   Clear[t2]
   t2 = i;
   t2scan = Table[t2, {71}];
   t3scan = Range[0.01, 0.08, 0.001];
   t2plus3 = t2scan + t3scan;
   ans = 
     Table[
       NSolve[{SimpleEquation1 == 0, SimpleEquation2 == 0.03} /. t3 -> angle, 
       {f, g}], {angle, t3scan}];
   fdrift = Flatten[f /. ans];
   gdrift = Flatten[g /. ans];
   Export["/Users/Desktop/t2.txt", t2scan];
   Export["/Users/t3.txt", t3scan];
   Export["/Users/t2plus3.txt", t2plus3];
   Export["/Users/f.txt", fdrift];
   Export["/Users/g.txt", gdrift];
   Run["sh myScript.sh"]
   , {i, 0.01, 0.08, 0.001}]

Any help would be greatly appreciated.

user1886681
  • 265
  • 1
  • 14
  • What do you get as a return when you execute the Run command, 0 or some other number? – Jason B. Jun 25 '16 at 13:20
  • Yes! I get "32512" – user1886681 Jun 25 '16 at 18:13
  • okay - so that means you get an error message of some kind, it returns 0 when it runs correctly. Perhaps its a directory issue? Is the script myScript.sh in the same directory that Mathematica is running from? If you type Directory[] you see what MMA's working directory is. If you go to a terminal and cd into that directory and type your sh command, does it work from there? – Jason B. Jun 25 '16 at 18:17
  • I dont think its a directory issue; I SetDirectory[] to all the same folder. I figured the simulation runs from any directory because its part of the "PATH" variables. Maybe Mathematica doesn't recognize the simulation as a variable because its variables/environment is set differently than my terminal? – user1886681 Jun 25 '16 at 18:31
  • It's possible - See if adding the full path to the executable changes it... – Jason B. Jun 25 '16 at 18:32
  • Putting the full path for the simulation in my script just changed the number Mathematica spits out to 256. – user1886681 Jun 25 '16 at 18:38
  • 1
    It's frustrating that you can't see the error message it gets from the terminal. If you add " > output.txt" to the end of the command does it pit any info in that file? – Jason B. Jun 25 '16 at 18:42
  • Holy moly! It worked! The output.txt told me exactly what was wrong! – user1886681 Jun 25 '16 at 18:53

1 Answers1

2

Essentially, two things needed to be done and amended in my script:

  1. The exact path location of the simulation needed to clarified. So, instead of writing:

     programName inputfile.ele
    

I had to put:

     /User/location/somewhere/programName inputfile.ele
  1. Secondly, by putting a generic output I was able to more clearly see exactly what the problem is:

     programName inputfile.ele>output.txt
    

My particularly issue is that my simulation requires a rpn definition file which is normally set as a path/variable in my terminal but apparently Mathematica does not recognize it. And wallah, after setting the rpn definition file in my script I could run the simulation program easily through Mathematica!

user1886681
  • 265
  • 1
  • 14