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_Table *result = 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 | double EArr[nPts]; |
---|
41 | double fArr[nPts]; |
---|
42 | |
---|
43 | /* initialize the global interface */ |
---|
44 | Rp_InterfaceInit(argc,argv,&fermi_io); |
---|
45 | |
---|
46 | /* check the global interface for errors */ |
---|
47 | if (Rp_InterfaceError() != 0) { |
---|
48 | /* there were errors while setting up the interface */ |
---|
49 | /* dump the traceback */ |
---|
50 | Rp_Outcome *o = Rp_InterfaceOutcome(); |
---|
51 | fprintf(stderr, "%s", Rp_OutcomeContext(o)); |
---|
52 | fprintf(stderr, "%s", Rp_OutcomeRemark(o)); |
---|
53 | return(Rp_InterfaceError()); |
---|
54 | } |
---|
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 | */ |
---|
68 | Rp_InterfaceConnect("temperature",&T,"units=K",NULL); |
---|
69 | Rp_InterfaceConnect("Ef",&Ef,"units=eV",NULL); |
---|
70 | Rp_InterfaceConnect("factorsTable",result,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(size_t 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 | /* |
---|
99 | * store results in the results table |
---|
100 | * add data to the table pointed to by the variable result. |
---|
101 | * put the fArr data in the column named "Fermi-Dirac Factor" |
---|
102 | * put the EArr data in the column named "Energy" |
---|
103 | */ |
---|
104 | Rp_TableAddData(result,"Fermi-Dirac Factor",nPts,fArr); |
---|
105 | Rp_TableAddData(result,"Energy",nPts,EArr); |
---|
106 | |
---|
107 | /* |
---|
108 | * close the global interface |
---|
109 | * signal to the graphical user interface that science |
---|
110 | * calculations are complete and to display the data |
---|
111 | * as described in the views |
---|
112 | */ |
---|
113 | Rp_InterfaceClose(); |
---|
114 | |
---|
115 | return 0; |
---|
116 | } |
---|