source: branches/blt4/examples/objects/app-fermi/cpp/fermi4.cc @ 3957

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

sync with trunk

File size: 3.6 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) 2004-2012  HUBzero Foundation, LLC
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 <cstdlib>
17#include <cstdio>
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    Rappture::Table *result = NULL;
29
30    // declare program variables
31    double E            = 0.0;
32    double dE           = 0.0;
33    double kT           = 0.0;
34    double Emin         = 0.0;
35    double Emax         = 0.0;
36    double f            = 0.0;
37    size_t nPts         = 200;
38    double EArr[nPts];
39    double fArr[nPts];
40
41    // initialize the global interface
42    Rappture::Interface(argc,argv,&fermi_io);
43
44    //check the global interface for errors
45    if (Rappture::Interface::error() != 0) {
46        // there were errors while setting up the interface
47        // dump the traceback
48        Rappture::Outcome o = Rappture::Interface::outcome();
49        fprintf(stderr, "%s",o.context());
50        fprintf(stderr, "%s",o.remark());
51        return (Rappture::Interface::error());
52    }
53
54    // connect variables to the interface
55    // look in the global interface for an object named
56    // "temperature", convert its value to Kelvin, and
57    // store the value into the address of T.
58    // look in the global interface for an object named
59    // "Ef", convert its value to electron Volts and store
60    // the value into the address of Ef
61    // look in the global interface for an object named
62    // factorsTable and set the variable result to
63    // point to it.
64    Rappture::Interface::connect("temperature",&T,"units=K",NULL);
65    Rappture::Interface::connect("Ef",&Ef,"units=eV",NULL);
66    Rappture::Interface::connect("factorsTable",result,NULL);
67
68    // check the global interface for errors
69    if (Rappture::Interface::error() != 0) {
70        // there were errors while retrieving input data values
71        // dump the tracepack
72        Rappture::Outcome o = Rappture::Interface::outcome();
73        fprintf(stderr, "%s",o.context());
74        fprintf(stderr, "%s",o.remark());
75        return(Rappture::Interface::error());
76    }
77
78    // do science calculations
79    kT = 8.61734e-5 * T;
80    Emin = Ef - 10*kT;
81    Emax = Ef + 10*kT;
82
83    dE = (1.0/nPts)*(Emax-Emin);
84
85    E = Emin;
86    for (size_t idx = 0; idx < nPts; idx++) {
87        E = E + dE;
88        f = 1.0/(1.0 + exp((E - Ef)/kT));
89        fArr[idx] = f;
90        EArr[idx] = E;
91        Rappture::Utils::progress((int)((E-Emin)/(Emax-Emin)*100),"Iterating");
92    }
93
94    // store results in the results table
95    // add data to the table pointed to by the variable result.
96    // put the fArr data in the column named "Fermi-Dirac Factor"
97    // put the EArr data in the column named "Energy"
98
99    result->addData("Fermi-Dirac Factor",nPts,fArr);
100    result->addData("Energy",nPts,EArr);
101
102    // close the global interface
103    // signal to the graphical user interface that science
104    // calculations are complete and to display the data
105    // as described in the views
106    Rappture::Interface::close();
107
108    return 0;
109}
Note: See TracBrowser for help on using the repository browser.