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

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

another possible example for accessing input data

File size: 3.1 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 E            = 0.0;
28    double dE           = 0.0;
29    double kT           = 0.0;
30    double Emin         = 0.0;
31    double Emax         = 0.0;
32    double f            = 0.0;
33
34    // create a rappture library from the file filePath
35    lib = new Rappture::Library(argv[1]);
36
37    if (lib == NULL) {
38        // cannot open file or out of memory
39        fprintf(stderr,"FAILED creating Rappture Library\n");
40        return(1);
41    }
42
43    T = new Rappture::Number(lib,"temperature");
44    Ef = new Rappture::Number(lib,"Ef");
45
46    if (lib.error() != 0) {
47        // there were errors while retrieving input data values
48        // dump the tracepack
49        fprintf(stderr, lib.traceback());
50        exit(lib.error());
51    }
52
53    kT = 8.61734e-5 * T->value("K");
54    Emin = Ef->value("eV") - 10*kT;
55    Emax = Ef->value("eV") + 10*kT;
56
57    dE = 0.005*(Emax-Emin);
58
59    Rappture::SimpleDoubleBuffer fBuf;
60    Rappture::SimpleDoubleBuffer EBuf;
61
62    for (E = Emin; E < Emax; E = E + dE) {
63        f = 1.0/(1.0 + exp((E - Ef)/kT));
64        fBuf.append(f);
65        EBuf.append(E);
66        rpUtilsProgress((int)((E-Emin)/(Emax-Emin)*100),"Iterating");
67    }
68    const double *fArr = fBuf.data();
69    const double *EArr = EBuf.data();
70    size_t nPts = fBuf.nmemb();
71
72    const char *curveLabel = "Fermi-Dirac Curve"
73    const char *curveDesc = "Plot of Fermi-Dirac Calculation";
74
75    // do it the easy way,
76    // create a plot to add to the library
77    // p1->add(nPts,xArr,yArr,format,curveLabel,curveDesc);
78
79    Rappture::Plot *p1 = new Rappture::Plot();
80    p1->add(nPts,fArr,EArr,"",curveLabel,curveDesc);
81    p1.propstr("xlabel","Fermi-Dirac Factor");
82    p1.propstr("ylabel","Energy");
83    p1.propstr("yunits","eV");
84
85    // add the plot to the library
86    lib.put(p1);
87
88
89    // or do it the harder way,
90    // create a curve to add to the library
91    // c1->axis("xaxis","xlabel","xdesc","units","scale",dataArr,nPts);
92
93    Rappture::Curve *c1 = new Rappture::Curve("output.curve(FDF)");
94    c1->label(curveLabel);
95    c1->desc(curveDesc);
96    c1->axis("xaxis","Fermi-Dirac Factor","","","",fArr,nPts);
97    c1->axis("yaxis","Energy","","eV","",EArr,nPts);
98
99    // add the curve to the library
100    lib.put(c1);
101
102    lib.result();
103
104    // clean up memory
105    delete lib;
106    delete p1;
107    delete c1;
108
109    return 0;
110}
Note: See TracBrowser for help on using the repository browser.