1 | // ---------------------------------------------------------------------- |
---|
2 | // EXAMPLE: Fermi-Dirac function in Python. |
---|
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) 2004-2005 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 | |
---|
20 | int main(int argc, char * argv[]) { |
---|
21 | |
---|
22 | RpLibrary* lib = NULL; |
---|
23 | |
---|
24 | const char* data = NULL; |
---|
25 | char line[100]; |
---|
26 | |
---|
27 | double T = 0.0; |
---|
28 | double Ef = 0.0; |
---|
29 | double E = 0.0; |
---|
30 | double dE = 0.0; |
---|
31 | double kT = 0.0; |
---|
32 | double Emin = 0.0; |
---|
33 | double Emax = 0.0; |
---|
34 | double f = 0.0; |
---|
35 | |
---|
36 | int err = 0; |
---|
37 | |
---|
38 | // create a rappture library from the file filePath |
---|
39 | lib = rpLibrary(argv[1]); |
---|
40 | |
---|
41 | if (lib == NULL) { |
---|
42 | // cannot open file or out of memory |
---|
43 | printf("FAILED creating Rappture Library\n"); |
---|
44 | return(1); |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | rpGetString(lib,"input.(temperature).current",&data); |
---|
49 | T = rpConvertDbl(data, "K", &err); |
---|
50 | if (err) { |
---|
51 | printf ("Error while retrieving input.(temperature).current\n"); |
---|
52 | return(1); |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | rpGetString(lib,"input.(Ef).current",&data); |
---|
57 | Ef = rpConvertDbl(data, "eV", &err); |
---|
58 | if (err) { |
---|
59 | printf ("Error while retrieving input.(Ef).current\n"); |
---|
60 | return(1); |
---|
61 | } |
---|
62 | |
---|
63 | kT = 8.61734e-5 * T; |
---|
64 | Emin = Ef - 10*kT; |
---|
65 | Emax = Ef + 10*kT; |
---|
66 | |
---|
67 | E = Emin; |
---|
68 | dE = 0.005*(Emax-Emin); |
---|
69 | |
---|
70 | rpPutString ( lib, |
---|
71 | "output.curve(f12).about.label", |
---|
72 | "Fermi-Dirac Factor", |
---|
73 | RPLIB_OVERWRITE ); |
---|
74 | rpPutString ( lib, |
---|
75 | "output.curve(f12).xaxis.label", |
---|
76 | "Fermi-Dirac Factor", |
---|
77 | RPLIB_OVERWRITE ); |
---|
78 | rpPutString ( lib, |
---|
79 | "output.curve(f12).yaxis.label", |
---|
80 | "Energy", |
---|
81 | RPLIB_OVERWRITE ); |
---|
82 | rpPutString ( lib, |
---|
83 | "output.curve(f12).yaxis.units", |
---|
84 | "eV", |
---|
85 | RPLIB_OVERWRITE ); |
---|
86 | |
---|
87 | while (E < Emax) { |
---|
88 | f = 1.0/(1.0 + exp((E - Ef)/kT)); |
---|
89 | sprintf(line,"%f %f\n",f, E); |
---|
90 | rpPutString(lib,"output.curve(f12).component.xy", line, RPLIB_APPEND); |
---|
91 | E = E + dE; |
---|
92 | } |
---|
93 | |
---|
94 | rpResult(lib); |
---|
95 | return 0; |
---|
96 | } |
---|