source: branches/blt4/examples/objects/app-fermi/c/ex3/fermi.c @ 1824

Last change on this file since 1824 was 1824, checked in by gah, 14 years ago
File size: 3.9 KB
Line 
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
23int 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_TableColumn *x1 = NULL;
29    Rp_TableColumn *y1 = NULL;
30    Rp_TableColumn *x2 = NULL;
31    Rp_TableColumn *y2 = NULL;
32
33
34    // declare program variables
35    double E          = 0.0;
36    double dE         = 0.0;
37    double kT         = 0.0;
38    double Emin       = 0.0;
39    double Emax       = 0.0;
40    double f          = 0.0;
41    size_t nPts       = 200;
42    double EArr[nPts];
43    double EArr2[nPts];
44    double fArr[nPts];
45    double fArr2[nPts];
46
47    // initialize the global interface
48    Rp_InterfaceInit(argc,argv,&fermi_io);
49
50    // check the global interface for errors
51    if (Rp_InterfaceError() != 0) {
52        // there were errors while setting up the interface
53        // dump the traceback
54        Rp_Outcome *o = Rp_InterfaceOutcome();
55        fprintf(stderr, "%s", Rp_OutcomeContext(o));
56        fprintf(stderr, "%s", Rp_OutcomeRemark(o));
57        return(Rp_InterfaceError());
58    }
59
60    // connect variables to the interface
61    // look in the global interface for an object named
62    // "temperature", convert its value to Kelvin, and
63    // store the value into the address of T.
64    // look in the global interface for an object named
65    // "Ef", convert its value to electron Volts and store
66    // the value into the address of Ef.
67    // look in the global interface for the columns to
68    // store data. retrieve them for later use.
69    Rp_InterfaceConnect("temperature",&T,"units=K",NULL);
70    Rp_InterfaceConnect("Ef",&Ef,"units=eV",NULL);
71
72    Rp_InterfaceConnect("Fermi-Dirac Factor",x1,NULL);
73    Rp_InterfaceConnect("Energy",y1,NULL);
74    Rp_InterfaceConnect("Fermi-Dirac Factor * 2",x2,NULL);
75    Rp_InterfaceConnect("Energy * 2",y2,NULL);
76
77    // check the global interface for errors
78    if (Rp_InterfaceError() != 0) {
79        // there were errors while retrieving input data values
80        // dump the traceback
81        Rp_Outcome *o = Rp_InterfaceOutcome();
82        fprintf(stderr, "%s", Rp_OutcomeContext(o));
83        fprintf(stderr, "%s", Rp_OutcomeRemark(o));
84        return(Rp_InterfaceError());
85    }
86
87    // do science calculations
88    kT = 8.61734e-5 * T;
89    Emin = Ef - (10*kT);
90    Emax = Ef + (10*kT);
91
92    dE = (1.0/nPts)*(Emax-Emin);
93
94    E = Emin;
95    for(size_t idx = 0; idx < nPts; idx++) {
96        E = E + dE;
97        f = 1.0/(1.0 + exp((E - Ef)/kT));
98        fArr[idx] = f;
99        fArr2[idx] = f*2;
100        EArr[idx] = E;
101        EArr2[idx] = E*2;
102        Rp_UtilsProgress((int)((E-Emin)/(Emax-Emin)*100),"Iterating");
103    }
104
105    // store results in the results table
106    // add data to the table pointed to by the variable result.
107    // put the fArr data in the column named "Fermi-Dirac Factor"
108    // put the EArr data in the column named "Energy"
109    //
110    Rp_TableColumnStoreDouble(x1,nPts,fArr);
111    Rp_TableColumnStoreDouble(y1,nPts,EArr);
112    Rp_TableColumnStoreDouble(x2,nPts,fArr2);
113    Rp_TableColumnStoreDouble(y2,nPts,EArr2);
114
115    // close the global interface
116    // signal to the graphical user interface that science
117    // calculations are complete and to display the data
118    // as described in the views
119    Rp_InterfaceClose();
120
121    return 0;
122}
Note: See TracBrowser for help on using the repository browser.