1 | /* |
---|
2 | *---------------------------------------------------------------------- |
---|
3 | * EXAMPLE: Fermi-Dirac function in C. |
---|
4 | * |
---|
5 | * This simple example shows how to use Rappture to wrap up a |
---|
6 | * simulator written in Matlab or some other language. |
---|
7 | * |
---|
8 | *====================================================================== |
---|
9 | * AUTHOR: Derrick Kearney, Purdue University |
---|
10 | * Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
11 | * |
---|
12 | * See the file "license.terms" for information on usage and |
---|
13 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
14 | *====================================================================== |
---|
15 | */ |
---|
16 | |
---|
17 | #include "rappture.h" |
---|
18 | #include <stdlib.h> |
---|
19 | #include <stdio.h> |
---|
20 | |
---|
21 | enum prog_consts { MAXLEN = 1000 }; |
---|
22 | |
---|
23 | int main(int argc, char * argv[]) { |
---|
24 | |
---|
25 | RpLibrary *lib = NULL; |
---|
26 | |
---|
27 | double T = 0.0; |
---|
28 | double Ef = 0.0; |
---|
29 | |
---|
30 | int err = 0; |
---|
31 | int skipCnt = 4; |
---|
32 | |
---|
33 | FILE *fp = NULL; |
---|
34 | |
---|
35 | char *prog = argv[0]; /* program name for errors */ |
---|
36 | const char *data = NULL; |
---|
37 | char line[MAXLEN+1]; |
---|
38 | |
---|
39 | // create a rappture library from the file filePath |
---|
40 | lib = rpLibrary(argv[1]); |
---|
41 | |
---|
42 | if (lib == NULL) { |
---|
43 | // cannot open file or out of memory |
---|
44 | fprintf(stderr,"%s: FAILED creating Rappture Library\n",prog); |
---|
45 | exit(1); |
---|
46 | } |
---|
47 | |
---|
48 | rpGetString(lib,"input.(temperature).current",&data); |
---|
49 | T = rpConvertDbl(data, "K", &err); |
---|
50 | if (err) { |
---|
51 | fprintf(stderr, "%s: Error retrieving current temperature\n",prog); |
---|
52 | exit(err); |
---|
53 | } |
---|
54 | |
---|
55 | rpGetString(lib,"input.(Ef).current",&data); |
---|
56 | Ef = rpConvertDbl(data, "eV", &err); |
---|
57 | if (err) { |
---|
58 | fprintf(stderr,"%s: Error while retrieving current EF\n",prog); |
---|
59 | exit(err); |
---|
60 | } |
---|
61 | |
---|
62 | fp = fopen("indeck","w"); |
---|
63 | if (fp == NULL) { |
---|
64 | fprintf(stderr,"%s: Error while writing to indeck",prog); |
---|
65 | exit(1); |
---|
66 | } |
---|
67 | |
---|
68 | fprintf(fp,"%f\n%f",Ef,T); |
---|
69 | fclose(fp); |
---|
70 | fp = NULL; |
---|
71 | |
---|
72 | // run the science program and capture the exit status |
---|
73 | err = system("octave --silent fermi.m < indeck"); |
---|
74 | |
---|
75 | if (err == 1) { |
---|
76 | fprintf(stderr,"%s: calling octave science program failed",prog); |
---|
77 | exit(err); |
---|
78 | } |
---|
79 | |
---|
80 | // Label output graph with title, x-axis label, |
---|
81 | // y-axis label, and y-axis units |
---|
82 | rpPutString ( lib, |
---|
83 | "output.curve(f12).about.label", |
---|
84 | "Fermi-Dirac Factor", |
---|
85 | RPLIB_OVERWRITE ); |
---|
86 | rpPutString ( lib, |
---|
87 | "output.curve(f12).xaxis.label", |
---|
88 | "Fermi-Dirac Factor", |
---|
89 | RPLIB_OVERWRITE ); |
---|
90 | rpPutString ( lib, |
---|
91 | "output.curve(f12).yaxis.label", |
---|
92 | "Energy", |
---|
93 | RPLIB_OVERWRITE ); |
---|
94 | rpPutString(lib, "output.curve(f12).yaxis.units", "eV", RPLIB_OVERWRITE); |
---|
95 | |
---|
96 | fp = fopen("out.dat","r"); |
---|
97 | if (fp == NULL) { |
---|
98 | fprintf(stderr,"%s: Error while writing to out.dat",prog); |
---|
99 | exit(1); |
---|
100 | } |
---|
101 | |
---|
102 | while (skipCnt > 0) { |
---|
103 | fgets(line,MAXLEN,fp); |
---|
104 | skipCnt--; |
---|
105 | } |
---|
106 | |
---|
107 | // it turns out that the octave script places the x and y values |
---|
108 | // in the correct format needed by rappture which is the x value |
---|
109 | // followed by a space followed by the y value and ended with a |
---|
110 | // newline character. this allows us to just read each line and |
---|
111 | // place it into our rappture object at the correct location |
---|
112 | // output.curve(f12).component.xy |
---|
113 | while (fgets(line,MAXLEN,fp)) { |
---|
114 | rpPutString(lib,"output.curve(f12).component.xy",line,RPLIB_APPEND); |
---|
115 | } |
---|
116 | |
---|
117 | remove("indeck"); |
---|
118 | remove("out.dat"); |
---|
119 | |
---|
120 | rpResult(lib); |
---|
121 | exit (0); |
---|
122 | } |
---|