11

Recently I've been making movies by having Mathematica create figures and then I combine all of them together using ffmpeg. The problem now is that my macbook is not fast enough to generate the figures I want in a short amount of time.

I have access to a linux machine and I would like to be able to send a script to that machine so that I can generate my figures. The problem I'm facing is that Mathematica can't open display ":0.0".

enter image description here

I also found this, but it did not solve my problem. Does anyone know how to generate figures in the terminal? All I want it to do is to write them to a file.

jmlopez
  • 6,470
  • 4
  • 28
  • 48
  • The link you gave seems to indicate that you must have a FrontEnd (or at least a capable windows server) to do anything with Export, or did I misunderstand the link? – tkott May 02 '12 at 20:19
  • My answer here will help – rm -rf May 02 '12 at 20:21
  • Does it work if you start the kernel with sh -c 'unset DISPLAY; exec math'? – celtschk May 02 '12 at 20:25
  • @celtschk, No, it still gives me the same error. – jmlopez May 02 '12 at 20:30
  • @R.M, When I try the <<JavaGraphics`, I get the same error. – jmlopez May 02 '12 at 20:33
  • Do you have X installed/running on your Mac? If so, you might log into the Linux box with ssh -X and get it to connect to your Mac. – celtschk May 02 '12 at 20:43
  • Another possibility would be if you have Xvfb on the Linux computer installed or can install it. Then you could use that to get a "fake" display. – celtschk May 02 '12 at 20:46
  • @celtschk, how can I find out if I have Xvfb? and now would I use it? – jmlopez May 02 '12 at 20:48
  • It looks like you're not forwarding X11. Either use ssh -Y or set ForwardX11Trusted to yes in your .ssh/config – rm -rf May 02 '12 at 20:52
  • @R.M, I used the ssh -Y option but it didn't work. I tried the command mathematica and it gave me 'can't open display ""'. But I just want to use the command line version. – jmlopez May 02 '12 at 20:58
  • Things also depend on what version of X11 you have. For older versions, you may be able to fix the forwarding problem by the following: defaults write ~/Library/Preferences/com.apple.X11 nolisten_tcp -boolean false - and you definitely have to log in with ssh -Y to get the X11 display. Also, do not type mathematica but instead math to start the Kernel only (the former starts the notebook interface). – Jens May 02 '12 at 21:09
  • Maybe one should also check if your X11 installation is actually working locally. – Jens May 02 '12 at 21:12
  • You can find how to use it on http://en.wikipedia.org/wiki/Xvfb (if calling it succeeds, you have it, otherwise you probably don't). Probably you'd just write Xvfb :1 & export DISPLAY=:1; math (I'm assuming you are using the bash shell on Linux). – celtschk May 02 '12 at 21:22
  • @Jens, I tried running the same commands in my macbook. Seems that I can save graphics from the terminal. I have logged in the machines with 'ssh -Y' but now I get: connect /tmp/.X11-unix/X0: No such file or directory Can't open display "localhost:11.0" – jmlopez May 02 '12 at 22:43
  • WHat happens in the local Terminal if you type "xterm&" ? – Jens May 02 '12 at 22:49
  • @Jens, that's interesting, this is what happened in my macbook: dhcp09:~ jmlopez$ xterm Xt error: Can't open display: :0.0 – jmlopez May 02 '12 at 22:52
  • 1
    Alright then. You should go to the web site http://xquartz.macosforge.org/landing/ and download the latest version of X11 appropriate for your OS X version. After installing it, everything should work. – Jens May 02 '12 at 23:06
  • @Jens, Thank you so much!!!! sigh*, that's what it was. After I updated to XQuartz it worked. – jmlopez May 02 '12 at 23:21
  • OK - for posterity I'll post this as an answer then... – Jens May 02 '12 at 23:27

3 Answers3

9

Since graphics can no longer be exported without access to a front end, even with remote connections you have to have X11 installed and working on your local machine. Therefore, the first thing you should do is: go to the web site http://xquartz.macosforge.org/landing/ and download the latest version of X11 appropriate for your OS X version. After installing it, everything should work.

Edit

In view of the comments, it's worth asking what the alternatives are to creating graphics on a remote host. Since the X11 protocol slows down the interaction between Kernel and FrontEnd, it's not practical on Mac OS X to create graphics that way any more. Unless you have older versions of Mathematica running on the remote host, that's the end of the line for this approach.

So what to do? You can hope you have a VNC server so you can start the interactive session entirely on the host. That's a good solution if it's available.

If not, then perhaps you'll be best off running the graphics generation as a background job on your own computer. If you have a multicore machine, you can start a Kernel session in the Terminal and let it run there, while doing other things in an interactive notebook session. At least in this way, you won't have to worry about not finding the FrontEnd.

On my Mac, I've set up a math command that starts /Applications/Mathematica.app/Contents/MacOS/MathKernel from the Terminal (see this page for details), and in such a Kernel session I would then (without calling JavaGraphics) do the graphics generation, e.g.:

t = Table[ParametricPlot3D[{Sin[u], Sin[v], Sin[u + v]}, {u, 0, 2 Pi}, {v,0, 2 Pi}, RegionFunction -> Function[{x, y, z}, z < a], PlotRange -> {{-1, 1}, {-1, 1}, {-1, 1}}],{a, -1,1,.01}];
Export["t.gif",t]

While that's running, I'm able to use the Mathematica notebook interface in parallel.

I said not to call JavaGraphics, because once that is initialized the Kernel session will want to display all the pictures you create. One can turn that off again by adding DisplayFunction->Identity to the plot command options, but if the plan is to do a non-interactive job, I'd suggest not loading JavaGraphics in the first place.

Jens
  • 97,245
  • 7
  • 213
  • 499
  • 6
    I wonder what drove Wolfram to this, err, interesting choice. There's nothing inherent in exporting graphics which couldn't be done on a kernel (and even more so, if you don't need to display anything on screen, a connection to a screen should not be required). – celtschk May 03 '12 at 08:55
  • 2
    Yes, it cripples any workflow that relied on batch jobs to generate figures. It's bad. – Jens May 03 '12 at 14:19
  • It also evidently makes the output of batch jobs dependent on the actual X setup on your server, which adds a kind of fun and excitement to bacth workflows that I would just as happily do without. – Pillsy May 03 '12 at 14:37
  • I really thought that this would be more efficient since the kernel will be running in a faster machine but it seems as if the graphics are being generated in my machine over an internet connection and this just makes the whole process way slower. What a bunch of bull... – jmlopez May 03 '12 at 18:34
  • 1
    Agreed. E.g., to make movies one uses render farms where the frames are generated and post-processed in background jobs. Since MMA is already quite slow at complex graphics, having no way to offload to background jobs is terrible. I'm surprised they did that. – Jens May 03 '12 at 19:11
4

The function UsingFrontEnd may help if the machine on which you are running the command has an available FrontEnd.

rcollyer
  • 33,976
  • 7
  • 92
  • 191
  • David, I edited your answer to include a link to the documentation, as that is an obscure function, in addition to adding code highlighting to it. – rcollyer May 03 '12 at 14:09
0

I think you need to do two things. First, you need to set up a virtual desktop. Ideally, you should just be able to execute vncserver from the linux command line. If not, you'll have to install it. Second, you're generating graphics from the command line but you'll still the services of a FrontEnd, so execute Developer`InstallFrontEnd[] from within Mathematica.

I think this will work but I'm unable to test at the moment. And I've been wrong before. Many times.

Mark McClure
  • 32,469
  • 3
  • 103
  • 161
  • I don't have control of those machines so I won't be able to install vncserver to try it out. At this moment I do not wish to bother the administrators. I tried the command you gave me and I got: connect /tmp/.X11-unix/X0: No such file or directory Can't open display "localhost:11.0" – jmlopez May 02 '12 at 22:45