1.4 Rappture and MATLAB PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Adding Rappture to

MATLAB Applications

Michael McLennan
HUBzero® Platform for Scientific Collaboration
Purdue University

This work licensed under See license online: 1


Creative Commons by-nc-sa/3.0
Example: Matlab/Octave Tool

The usual way… The Rappture way…


% matlab -nodisplay -r fermi
Enter the Fermi level (eV):
Ef = 2.4
Enter the temperature (K):
T = 77

% more out.dat
FERMI-DIRAC FUNCTION F1/2

f1/2 Energy (eV)


------------ ------------
0.999955 2.33365
0.99995 2.33431
0.999944 2.33498

https://nanohub.org/infrastructure/rappture
source code: rappture/examples/app-fermi
2
What is the interface?

disp('Enter
disp('Enter the
the Fermi
Fermi level
level (eV):');
(eV):'); number
Ef
Ef == input('
input(' Ef
Ef == ');
');

disp('Enter
disp('Enter the
the temperature
temperature (K):');
(K):'); number
TT == input('
input(' TT == ');
');

kT
kT == 8.61734e-5
8.61734e-5 ** T;
T; physics
Emin
Emin == Ef
Ef -- 10*kT;
10*kT;
Emax
Emax == Ef
Ef ++ 10*kT;
10*kT;

EE == linspace(Emin,Emax,200);
linspace(Emin,Emax,200);
ff == 1.0
1.0 ./
./ (1.0
(1.0 ++ exp((E
exp((E -- Ef)/kT));
Ef)/kT));

fid
fid == fopen('out.dat','w');
fopen('out.dat','w');
fprintf(fid,'FERMI-DIRAC
fprintf(fid,'FERMI-DIRAC FUNCTION
FUNCTION
F1/2\n\n');
F1/2\n\n');
fprintf(fid,‘f1/2
fprintf(fid,‘f1/2 Energy
Energy (eV)\n');
(eV)\n');
fprintf(fid,'------------
fprintf(fid,'------------ ------------\n');
------------\n'); curve
fprintf(fid,'%12g
fprintf(fid,'%12g %12g\n',
%12g\n', [f;
[f; E]);
E]);
fclose(fid);
fclose(fid);

quit;
quit;

3
Build the interface: Temperature input

Define this input

4
Build the interface: Ef input

Define this input

5
Build the interface: Curve output

Define this output

6
Build the interface: Use MATLAB/Octave

Define tool info

7
Add physics to the generated code

Generated script: main.m



% get input value for input.number(temperature) and convert to K
str = rpLibGetString(io,'input.number(temperature).current');
[temperature,err] = rpUnitsConvertDbl(str, 'K');

% get input value for input.number(Ef) and convert to eV


str = rpLibGetString(io,'input.number(Ef).current');
[Ef,err] = rpUnitsConvertDbl(str, 'eV');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
kT
kT == 8.61734e-5
8.61734e-5 ** T;
T; physics
% Add your code here for the main body of your program
Emin
Emin == Ef
Ef -- 10*kT;
10*kT;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Emax
Emax == Ef
Ef ++ 10*kT;
10*kT;

EE == linspace(Emin,Emax,200);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
linspace(Emin,Emax,200);
% Save output values backff to Rappture
== 1.0
1.0 ./
./ (1.0
(1.0 ++ exp((E
exp((E -- Ef)/kT));
Ef)/kT));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% save output value for output.curve(f12)


% this assumes a vector 'x' and a vector 'y'
xydata = [x;y];
str = sprintf('%12g %12g\n', xydata);
rpLibPutString(io,'output.curve(f12).component.xy',str,0);

8
Add physics to the generated code

Final script: main.m



% get input value for input.number(temperature) and convert to K
str = rpLibGetString(io,'input.number(temperature).current');
[temperature,err] = rpUnitsConvertDbl(str, 'K');

% get input value for input.number(Ef) and convert to eV


str = rpLibGetString(io,'input.number(Ef).current');
[Ef,err] = rpUnitsConvertDbl(str, 'eV');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
kT = 8.61734e-5 * temperature;
Emin = Ef - 10*kT;
Emax = Ef + 10*kT;

E = linspace(Emin,Emax,200);
f = 1.0 ./ (1.0 + exp((E - Ef)/kT));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% save output value for output.curve(f12)


% this assumes a vector 'x' and a vector 'y'
xydata = [f;E];
str = sprintf('%12g %12g\n', xydata);
rpLibPutString(io,'output.curve(f12).component.xy',str,0);

9
Running the code

<XML> driver1827.xml
Builder Runner

kT
kT == 8.61734e-5
8.61734e-5 ** temperature;
temperature; physics
Emin
Emin == Ef
Ef -- 10*kT;
10*kT;
<?xml
<?xml version="1.0"?>
version="1.0"?> Emax == Ef
Emax Ef ++ 10*kT;
10*kT;
<run>
<run>
…… EE == linspace(Emin,Emax,200);
linspace(Emin,Emax,200);
ff == 1.0
1.0 ./
./ (1.0
(1.0 ++ exp((E
exp((E -- Ef)/kT));
Ef)/kT));
<output>
<output>
<curve
<curve id="f12">
id="f12">
……
<component>
<component>
<xy>
<xy>
0.999955
0.999955 2.33365
2.33365
0.99995
0.99995 2.33431
<XML>
2.33431 <XML>
0.999944 2.33498
0.999944 2.33498
……
</output>
</output>
</run>
</run> tool.xml run12703129102.xml
10
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
driver1529.xml fermi.m tool.xml
$ use rappture
$ matlab –r infile=\’driver1529.xml\’,main

11
Assignment #4: Build a simple Spirograph tool

n1 n2 n3
number number number

Spirograph equation

z
curve

In MATLAB/Octave:
t = linspace(0, 1, 1000);
z = exp(i*2*pi*n1*t) + exp(i*2*pi*n2*t) + exp(i*2*pi*n3*t);
plot(real(z),imag(z));

12

You might also like