source: trunk/examples/objects/app-fermi/fermi2.cc @ 1560

Last change on this file since 1560 was 1560, checked in by dkearney, 15 years ago

updates to the object system, fixed up tree and xml parser objects, added some basic tests for them and adopted number object to accept xml text and configure itself from the parsed xml. added fermi3.cc example program which contains suggested interface from apps meeting.

File size: 2.5 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
21int main(int argc, char * argv[]) {
22
23    Rappture::Library *lib = NULL;
24
25    Rappture::Number *T  = NULL;
26    Rappture::Number *Ef = NULL;
27    double dE           = 0.0;
28    double kT           = 0.0;
29    double Emin         = 0.0;
30    double Emax         = 0.0;
31    size_t nPts         = 200;
32    double E[nPts];
33    double f[nPts];
34
35    // create a rappture library from the file filePath
36    lib = new Rappture::Library(argv[1]);
37
38    if (lib == NULL) {
39        // cannot open file or out of memory
40        fprintf(stderr,"FAILED creating Rappture Library\n");
41        return(1);
42    }
43
44    T = new Rappture::Number(lib,"temperature");
45    Ef = new Rappture::Number(lib,"Ef");
46
47    if (lib.error() != 0) {
48        // there were errors while retrieving input data values
49        // dump the tracepack
50        fprintf(stderr, lib.traceback());
51        exit(lib.error());
52    }
53
54    kT = 8.61734e-5 * T->value("K");
55    Emin = Ef->value("eV") - 10*kT;
56    Emax = Ef->value("eV") + 10*kT;
57
58    dE = (1.0/nPts)*(Emax-Emin);
59
60    E = Emin;
61    for (size_t idx = 0; idx < nPts; idx++) {
62        E = E + dE;
63        f = 1.0/(1.0 + exp((E - Ef)/kT));
64        fArr[idx] = f;
65        EArr[idx] = E;
66        rpUtilsProgress((int)((E-Emin)/(Emax-Emin)*100),"Iterating");
67    }
68
69    const char *curveLabel = "Fermi-Dirac Curve"
70    const char *curveDesc = "Plot of Fermi-Dirac Calculation";
71
72    // do it the easy way,
73    // create a plot to add to the library
74    // plot is registered with lib upon object creation
75    // p1->add(nPts,xArr,yArr,format,curveLabel,curveDesc);
76
77    Rappture::Plot *p1 = new Rappture::Plot(lib);
78    p1->add(nPts,fArr,EArr,"",curveLabel,curveDesc);
79    p1.propstr("xlabel","Fermi-Dirac Factor");
80    p1.propstr("ylabel","Energy");
81    p1.propstr("yunits","eV");
82
83    lib.result();
84
85    // clean up memory
86    delete lib;
87    delete T;
88    delete Ef;
89    delete p1;
90
91    return 0;
92}
Note: See TracBrowser for help on using the repository browser.