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