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 | int main(int argc, char * argv[]) { |
---|
22 | |
---|
23 | Rp_Library *lib = NULL; |
---|
24 | Rp_Number *T = NULL; |
---|
25 | |
---|
26 | double Ef = 0.0; |
---|
27 | double E = 0.0; |
---|
28 | double dE = 0.0; |
---|
29 | double kT = 0.0; |
---|
30 | double Emin = 0.0; |
---|
31 | double Emax = 0.0; |
---|
32 | double f = 0.0; |
---|
33 | size_t nPts = 200; |
---|
34 | double EArr[nPts]; |
---|
35 | double fArr[nPts]; |
---|
36 | |
---|
37 | Rp_Plot *p1 = NULL; |
---|
38 | |
---|
39 | // create a rappture library from the file filePath |
---|
40 | lib = Rp_LibraryInit(); |
---|
41 | Rp_LibraryLoadFile(lib,argv[1]); |
---|
42 | |
---|
43 | if (Rp_LibraryError(lib) != 0) { |
---|
44 | // cannot open file or out of memory |
---|
45 | Rp_Outcome *o = Rp_LibraryOutcome(lib); |
---|
46 | fprintf(stderr, "%s", Rp_OutcomeContext(o)); |
---|
47 | fprintf(stderr, "%s", Rp_OutcomeRemark(o)); |
---|
48 | return(Rp_LibraryError(lib)); |
---|
49 | } |
---|
50 | |
---|
51 | Rp_Connect(lib,"temperature",T); |
---|
52 | Rp_LibraryValue(lib,"Ef",&Ef,1,"units=eV"); |
---|
53 | |
---|
54 | if (Rp_LibraryError(lib) != 0) { |
---|
55 | // there were errors while retrieving input data values |
---|
56 | // dump the tracepack |
---|
57 | Rp_Outcome *o = Rp_LibraryOutcome(lib); |
---|
58 | fprintf(stderr, "%s", Rp_OutcomeContext(o)); |
---|
59 | fprintf(stderr, "%s", Rp_OutcomeRemark(o)); |
---|
60 | return(Rp_LibraryError(lib)); |
---|
61 | } |
---|
62 | |
---|
63 | kT = 8.61734e-5 * Rp_NumberValue(T,"K"); |
---|
64 | Emin = Ef - 10*kT; |
---|
65 | Emax = Ef + 10*kT; |
---|
66 | |
---|
67 | dE = (1.0/nPts)*(Emax-Emin); |
---|
68 | |
---|
69 | E = Emin; |
---|
70 | for(size_t idx = 0; idx < nPts; idx++) { |
---|
71 | E = E + dE; |
---|
72 | f = 1.0/(1.0 + exp((E - Ef)/kT)); |
---|
73 | fArr[idx] = f; |
---|
74 | EArr[idx] = E; |
---|
75 | Rp_UtilsProgress((int)((E-Emin)/(Emax-Emin)*100),"Iterating"); |
---|
76 | } |
---|
77 | |
---|
78 | // do it the easy way, |
---|
79 | // create a plot to add to the library |
---|
80 | // plot is registered with lib upon object creation |
---|
81 | // p1->add(nPts,xArr,yArr,format,name); |
---|
82 | |
---|
83 | p1 = Rp_PlotInit(lib); |
---|
84 | Rp_PlotAdd(p1,nPts,fArr,EArr,"","fdfactor"); |
---|
85 | Rp_PlotPropstr(p1,"label","Fermi-Dirac Curve"); |
---|
86 | Rp_PlotPropstr(p1,"desc","Plot of Fermi-Dirac Calculation"); |
---|
87 | Rp_PlotPropstr(p1,"xlabel","Fermi-Dirac Factor"); |
---|
88 | Rp_PlotPropstr(p1,"ylabel","Energy"); |
---|
89 | Rp_PlotPropstr(p1,"yunits","eV"); |
---|
90 | |
---|
91 | Rp_LibraryResult(lib); |
---|
92 | |
---|
93 | return 0; |
---|
94 | } |
---|