source: trunk/examples/objects/app-fermi/c/ex3/fermi.c @ 1655

Last change on this file since 1655 was 1655, checked in by dkearney, 11 years ago

examples of using a view in tcl and c, update tcl and c view apis

File size: 3.7 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_Table *result  = NULL;
29
30
31    // declare program variables
32    double E          = 0.0;
33    double dE         = 0.0;
34    double kT         = 0.0;
35    double Emin       = 0.0;
36    double Emax       = 0.0;
37    double f          = 0.0;
38    size_t nPts       = 200;
39    double EArr[nPts];
40    double EArr2[nPts];
41    double fArr[nPts];
42    double fArr2[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("factorsTable",result,NULL);
70
71    // check the global interface for errors
72    if (Rp_InterfaceError() != 0) {
73        // there were errors while retrieving input data values
74        // dump the traceback
75        Rp_Outcome *o = Rp_InterfaceOutcome();
76        fprintf(stderr, "%s", Rp_OutcomeContext(o));
77        fprintf(stderr, "%s", Rp_OutcomeRemark(o));
78        return(Rp_InterfaceError());
79    }
80
81    // do science calculations
82    kT = 8.61734e-5 * T;
83    Emin = Ef - (10*kT);
84    Emax = Ef + (10*kT);
85
86    dE = (1.0/nPts)*(Emax-Emin);
87
88    E = Emin;
89    for(size_t idx = 0; idx < nPts; idx++) {
90        E = E + dE;
91        f = 1.0/(1.0 + exp((E - Ef)/kT));
92        fArr[idx] = f;
93        fArr2[idx] = f*2;
94        EArr[idx] = E;
95        EArr2[idx] = E*2;
96        Rp_UtilsProgress((int)((E-Emin)/(Emax-Emin)*100),"Iterating");
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_TableStore(result,"Fermi-Dirac Factor",nPts,fArr);
105    Rp_TableStore(result,"Energy",nPts,EArr);
106    Rp_TableStore(result,"Fermi-Dirac Factor * 2",nPts,fArr2);
107    Rp_TableStore(result,"Energy * 2",nPts,EArr2);
108
109    // close the global interface
110    // signal to the graphical user interface that science
111    // calculations are complete and to display the data
112    // as described in the views
113    Rp_InterfaceClose();
114
115    return 0;
116}
Note: See TracBrowser for help on using the repository browser.