Adding Rappture to MATLAB Applications Michael Mc Lennan

Adding Rappture to MATLAB Applications Michael Mc. Lennan Software Architect HUBzero™ Platform for Scientific Collaboration

Example: Matlab/Octave Tool The usual way… % matlab -nodisplay -r fermi Enter the Fermi level (e. V): Ef = 2. 4 Enter the temperature (K): T = 77 The Rappture way… % more out. dat FERMI-DIRAC FUNCTION F 1/2 f 1/2 ------0. 999955 0. 999944 Energy (e. V) ------2. 33365 2. 33431 2. 33498 https: //developer. nanohub. org/projects/rappture source code: rappture/examples/app-fermi

How does it work? description of tool, including inputs and outputs <XML> tool. xml executable Rappture GUI Produces the user interface automatically!

What is the interface? Input number disp('Enter the Fermi level (e. V): '); Ef = input(' Ef = '); number disp('Enter the temperature (K): '); T = input(' T = '); k. T = 8. 61734 e-5 * T; Emin = Ef - 10*k. T; Emax = Ef + 10*k. T; E = linspace(Emin, Emax, 200); f = 1. 0. / (1. 0 + exp((E - Ef)/k. T)); Physics curve Output fid = fopen('out. dat', 'w'); fprintf(fid, 'FERMI-DIRAC FUNCTION F 1/2nn'); fprintf(fid, ‘f 1/2 Energy (e. V)n'); fprintf(fid, '------n'); fprintf(fid, '%12 gn', [f; E]); fclose(fid); quit;

Describing inputs disp('Enter the Fermi level (e. V): '); Ef = input(' Ef = '); disp('Enter the temperature (K): '); T = input(' T = '); <number id="Ef"> <about> k. T = 8. 61734 e-5 * T; <label>Fermi Level</label> Emin = Ef - 10*k. T; <description>Energy at center Emax = of Ef distribution. </description> + 10*k. T; </about> E = linspace(Emin, Emax, 200); <units>e. V</units> f = 1. 0. / (1. 0 + exp((E - Ef)/k. T)); <min>-10 e. V</min> <max>10 e. V</max> <default>0 e. V</default> </number> Rappture description of a number input fid = fopen('out. dat', 'w'); fprintf(fid, 'FERMI-DIRAC FUNCTION F 1/2nn'); fprintf(fid, ‘f 1/2 Energy (e. V)n'); fprintf(fid, '------n'); fprintf(fid, '%12 gn', [f; E]); fclose(fid); quit;

Describing inputs disp('Enter the Fermi level (e. V): '); Ef = input(' Ef = '); disp('Enter the temperature (K): '); T = input(' T = '); <number id="temperature"> k. T = 8. 61734 e-5 * T; <about> Emin = Ef - 10*k. T; <label>Ambient temperature</label> Emax = Ef + 10*k. T; <description>Temperature of the environment. </description> </about> E = linspace(Emin, Emax, 200); f = 1. 0. / (1. 0 + exp((E - Ef)/k. T)); <units>K</units> <min>0 K</min> fid = fopen('out. dat', 'w'); <max>500 K</max> fprintf(fid, 'FERMI-DIRAC FUNCTION <default>300 K</default> F 1/2nn'); <preset> fprintf(fid, ‘f 1/2 Energy (e. V)n'); fprintf(fid, '------n'); <value>77 K</value> fprintf(fid, '%12 gn', [f; E]); <label>77 K (liquid nitrogen)</label> fclose(fid); </preset> … quit; </number>

Describing outputs disp('Enter the Fermi level (e. V): '); Ef = input(' Ef = '); <curve id="f 12"> the temperature (K): '); <about><label>Fermi-Dirac disp('Enter Factor</label></about> T = input(' T = '); <xaxis> <label>Fermi-Dirac Factor</label> k. T = 8. 61734 e-5 * T; </xaxis> Emin = Ef - 10*k. T; <yaxis> Emax = Ef + 10*k. T; <label>Energy</label> E = linspace(Emin, Emax, 200); <units>e. V</units> f = 1. 0. / (1. 0 + exp((E - Ef)/k. T)); </yaxis> </curve> fid = fopen('out. dat', 'w'); fprintf(fid, 'FERMI-DIRAC FUNCTION F 1/2nn'); fprintf(fid, ‘f 1/2 Energy (e. V)n'); fprintf(fid, '------n'); fprintf(fid, '%12 gn', [f; E]); fclose(fid); quit;

Describing the Whole Tool File: tool. xml <? xml version="1. 0"? > <run> <tool> <about>Press Simulate to view results. </about> <command>matlab –nodisplay -r infile='@driver‘, path(‘@tool’, path), fermi</command> </tool> <input> <number id="temperature">…</number> <number id="Ef">…</number> </input> <output> <curve id="f 12">…</curve> </output> </run> Basic structure from this page:

Half-way there Once you’ve created the tool. xml file, you can test the GUI Rappture <XML> tool. xml % cd tooldir % ls tool. xml % rappture Fix this next… executable Rappture GUI

How does it work? Only input file needed by your program description of tool, including inputs and outputs <XML> 300 K Rappture 300 K tool. xml driver 159. xml Rappture Library API executable Rappture GUI <XML> 300 K run 2743. xml All inputs and outputs—complete record of the run

Updating our original script fermi. m Matlab script disp('Enter the Fermi level (e. V): '); Ef = input(' Ef = '); Input disp('Enter the temperature (K): '); T = input(' T = '); % do k. T = Emin Emax fermi calculations (science). . . 8. 61734 e-5 * T; = Ef - 10*k. T; = Ef + 10*k. T; E = linspace(Emin, Emax, 200); f = 1. 0. / (1. 0 + exp((E - Ef)/k. T)); … Physics

Read the fermi level fermi. m Matlab script % open the driver file lib = rp. Lib(infile); % get the input values and convert to correct units Ef = rp. Lib. Get. String(lib, 'input. number(Ef). current'); [Ef, err] = rp. Units. Convert. Dbl(Ef, 'e. V'); “ 240 me. V” 2. 4 T = rp. Lib. Get. String(lib, 'input. number(temperature). current'); tool. xml: [T, err] = rp. Units. Convert. Dbl(T, 'K'); <? xml version="1. 0"? > fermi calculations (science). . . <run> 8. 61734 e-5 * T; … = Ef - 10*k. T; <input> = Ef + 10*k. T; <number id="Ef"> <current>2. 4 e. V</current> E = linspace(Emin, Emax, 200); </number> f = 1. 0. / (1. 0 + exp((E - Ef)/k. T)); </input> … … </run> % do k. T = Emin Emax input. number(Ef). current

Read the temperature fermi. m Matlab script % open the driver file lib = rp. Lib(infile); % get the input values and convert to correct units Ef = rp. Lib. Get. String(lib, 'input. number(Ef). current'); [Ef, err] = rp. Units. Convert. Dbl(Ef, 'e. V'); T = rp. Lib. Get. String(lib, 'input. number(temperature). current'); [T, err] = rp. Units. Convert. Dbl(T, 'K'); % do k. T = Emin Emax fermi calculations (science). . . 8. 61734 e-5 * T; = Ef - 10*k. T; = Ef + 10*k. T; E = linspace(Emin, Emax, 200); f = 1. 0. / (1. 0 + exp((E - Ef)/k. T)); … “ 100 C” 373. 15

Updating our original script fermi. m Matlab script … E = linspace(Emin, Emax, 200); f = 1. 0. / (1. 0 + exp((E - Ef)/k. T)); Physics fid = fopen('out. dat', 'w'); fprintf(fid, 'FERMI-DIRAC FUNCTION F 1/2nn'); fprintf(fid, ‘f 1/2 Energy (e. V)n'); fprintf(fid, '------n'); fprintf(fid, '%12 gn', [f; E]); fclose(fid); quit; Output

Save the results fermi. m Matlab script … E = linspace(Emin, Emax, 200); f = 1. 0. / (1. 0 + exp((E - Ef)/k. T)); Means “overwrite” (no “append”) % store output in curve put. Str = sprintf('%12 gn', [f; E]); rp. Lib. Put. String(lib, 'output. curve(f 12). component. xy', put. Str, 0); % save the output rp. Lib. Result(lib); quit; <XML> run 1128018316. xml

Save the results fermi. m Matlab script … E = linspace(Emin, Emax, 200); f = 1. 0. / (1. 0 + exp((E - Ef)/k. T)); % store output in curve put. Str = sprintf('%12 gn', [f; E]); <? xml version="1. 0"? > rp. Lib. Put. String(lib, 'output. curve(f 12). component. xy', put. Str, 0); <run> … % save the output <output> rp. Lib. Result(lib); <curve id="f 12"> … quit; <component> <xy> 0. 999955 2. 33365 0. 99995 2. 33431 <XML> 0. 999944 2. 33498 … </output> </run> run 1128018316. xml

Run the program How does the program get invoked? <XML> 300 K Rappture 300 K tool. xml driver 159. xml executable Rappture GUI <XML> 300 K run 2743. xml

How does the program get invoked? Replaced with directory containing tool. xml file <XML> Ex: /apps/yourtool/current Replaced with name of driver file tool. xml Ex: driver 159. xml <? xml version="1. 0"? > <run> <tool> <about>Press Simulate to view results. </about> <command>matlab –nodisplay –r infile='@driver‘, path(‘@tool’, path), fermi </command> </tool> Set infile to the <input> name of driver file … </input> Add tool directory <output> to search path … </output> Invoke fermi. m </run>

How does the program get invoked? matlab –nodisplay -r infile='@driver‘, path(‘@tool’, path), fermi matlab –nodisplay -r infile=‘driver 159. xml‘, path(‘/apps/yourtool/current’, path), fermi % open the driver file lib = rp. Lib(infile); Rappture % get the input values and convert to correct units Ef = rp. Lib. Get. String(lib, 'input. number(Ef). current'); [Ef, err] = rp. Units. Convert. Dbl(Ef, 'e. V'); … … % save the output rp. Lib. Result(lib); quit; <XML> 300 K run 2743. xml

Using Octave 3 octave --silent --eval infile='@driver‘, path(‘@tool’, path), fermi octave --silent --eval infile=‘driver 159. xml‘, path(‘/apps/yourtool/current’, path), fermi % open the driver file lib = rp. Lib(infile); Rappture % get the input values and convert to correct units Ef = rp. Lib. Get. String(lib, 'input. number(Ef). current'); [Ef, err] = rp. Units. Convert. Dbl(Ef, 'e. V'); … … % save the output rp. Lib. Result(lib); quit; <XML> 300 K run 2743. xml

Using Octave 2 octave --silent --eval infile='@driver‘, path(‘@tool’, path), fermi octave --silent @tool/fermi. m @driver octave --silent /apps/yourtool/current/fermi. m driver 159. xml % open the driver file lib = rp. Lib(argv{1}); Rappture % get the input values and convert to correct units Ef = rp. Lib. Get. String(lib, 'input. number(Ef). current'); [Ef, err] = rp. Units. Convert. Dbl(Ef, 'e. V'); … Watch out! Use curly braces here

Debugging If something goes wrong, MATLAB goes into “debug” mode: Waiting for you to type a MATLAB command Click Abort instead Run it by hand: $ ls driver 1529. xml fermi. m tool. xml $ use rappture $ matlab –r infile=’driver 1529. xml’, fermi
- Slides: 22