1 | /* |
---|
2 | * ---------------------------------------------------------------------- |
---|
3 | * EXAMPLE: Fermi-Dirac function in C |
---|
4 | * |
---|
5 | * This simple example shows how to use Rappture within a simulator |
---|
6 | * written in C. |
---|
7 | * ====================================================================== |
---|
8 | * AUTHOR: Derrick Kearney, Purdue University |
---|
9 | * Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
10 | * |
---|
11 | * See the file "license.terms" for information on usage and |
---|
12 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
13 | * ====================================================================== |
---|
14 | */ |
---|
15 | #include "rappture.h" |
---|
16 | |
---|
17 | #include <stdlib.h> |
---|
18 | #include <stdio.h> |
---|
19 | #include <math.h> |
---|
20 | #include <unistd.h> |
---|
21 | |
---|
22 | #include "fermi_io.c" |
---|
23 | |
---|
24 | int main(int argc, char * argv[]) { |
---|
25 | |
---|
26 | /* declare variables to interact with Rappture */ |
---|
27 | double T = 0.0; |
---|
28 | double Ef = 0.0; |
---|
29 | Rp_Plot *p1 = NULL; |
---|
30 | Rp_Plot *p2 = NULL; |
---|
31 | |
---|
32 | |
---|
33 | /* declare program variables */ |
---|
34 | double E = 0.0; |
---|
35 | double dE = 0.0; |
---|
36 | double kT = 0.0; |
---|
37 | double Emin = 0.0; |
---|
38 | double Emax = 0.0; |
---|
39 | double f = 0.0; |
---|
40 | size_t nPts = 200; |
---|
41 | size_t idx = 0; |
---|
42 | double EArr[nPts]; |
---|
43 | double fArr[nPts]; |
---|
44 | |
---|
45 | /* initialize the global interface */ |
---|
46 | Rp_InterfaceInit(argc,argv,&fermi_io); |
---|
47 | |
---|
48 | /* check the global interface for errors */ |
---|
49 | if (Rp_InterfaceError() != 0) { |
---|
50 | /* there were errors while setting up the interface */ |
---|
51 | /* dump the traceback */ |
---|
52 | Rp_Outcome *o = Rp_InterfaceOutcome(); |
---|
53 | fprintf(stderr, "%s", Rp_OutcomeContext(o)); |
---|
54 | fprintf(stderr, "%s", Rp_OutcomeRemark(o)); |
---|
55 | return(Rp_InterfaceError()); |
---|
56 | } |
---|
57 | |
---|
58 | /* |
---|
59 | * connect variables to the interface |
---|
60 | * look in the global interface for an object named |
---|
61 | * "temperature", convert its value to Kelvin, and |
---|
62 | * store the value into the address of T. |
---|
63 | * look in the global interface for an object named |
---|
64 | * "Ef", convert its value to electron Volts and store |
---|
65 | * the value into the address of Ef. |
---|
66 | * look in the global interface for an object named |
---|
67 | * factorsTable and set the variable result to |
---|
68 | * point to it. |
---|
69 | */ |
---|
70 | Rp_InterfaceConnect("temperature",&T,"units=K",NULL); |
---|
71 | Rp_InterfaceConnect("Ef",&Ef,"units=eV",NULL); |
---|
72 | Rp_InterfaceConnect("fdfPlot",p1,NULL); |
---|
73 | Rp_InterfaceConnect("fdfPlot2",p2,NULL); |
---|
74 | |
---|
75 | /* check the global interface for errors */ |
---|
76 | if (Rp_InterfaceError() != 0) { |
---|
77 | /* there were errors while retrieving input data values */ |
---|
78 | /* dump the traceback */ |
---|
79 | Rp_Outcome *o = Rp_InterfaceOutcome(); |
---|
80 | fprintf(stderr, "%s", Rp_OutcomeContext(o)); |
---|
81 | fprintf(stderr, "%s", Rp_OutcomeRemark(o)); |
---|
82 | return(Rp_InterfaceError()); |
---|
83 | } |
---|
84 | |
---|
85 | /* do science calculations */ |
---|
86 | kT = 8.61734e-5 * T; |
---|
87 | Emin = Ef - (10*kT); |
---|
88 | Emax = Ef + (10*kT); |
---|
89 | |
---|
90 | dE = (1.0/nPts)*(Emax-Emin); |
---|
91 | |
---|
92 | E = Emin; |
---|
93 | for(idx = 0; idx < nPts; idx++) { |
---|
94 | E = E + dE; |
---|
95 | f = 1.0/(1.0 + exp((E - Ef)/kT)); |
---|
96 | fArr[idx] = f; |
---|
97 | EArr[idx] = E; |
---|
98 | Rp_UtilsProgress((int)((E-Emin)/(Emax-Emin)*100),"Iterating"); |
---|
99 | } |
---|
100 | |
---|
101 | /* |
---|
102 | * set up the curves for the plot by using the add command |
---|
103 | * Rp_PlotAdd(plot,name,nPts,xdata,ydata,fmt); |
---|
104 | * |
---|
105 | * to group curves on the same plot, just keep adding curves |
---|
106 | * to save space, X array values are compared between curves. |
---|
107 | * the the X arrays contain the same values, we only store |
---|
108 | * one version in the internal data table, otherwise a new |
---|
109 | * column is created for the array. for big arrays this may take |
---|
110 | * some time, we should benchmark to see if this can be done |
---|
111 | * efficiently. |
---|
112 | */ |
---|
113 | Rp_PlotAdd(p1,"fdfCurve1",nPts,fArr,EArr,"g:o"); |
---|
114 | |
---|
115 | Rp_PlotAdd(p2,"fdfCurve2",nPts,fArr,EArr,"b-o"); |
---|
116 | Rp_PlotAdd(p2,"fdfCurve3",nPts,fArr,EArr,"p--"); |
---|
117 | |
---|
118 | /* |
---|
119 | * close the global interface |
---|
120 | * signal to the graphical user interface that science |
---|
121 | * calculations are complete and to display the data |
---|
122 | * as described in the views |
---|
123 | */ |
---|
124 | Rp_InterfaceClose(); |
---|
125 | |
---|
126 | return 0; |
---|
127 | } |
---|