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

Last change on this file since 3957 was 3957, checked in by gah, 11 years ago

sync with trunk

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