source: trunk/src/fortran/RpUnitsFInterface.cc @ 83

Last change on this file since 83 was 83, checked in by dkearney, 17 years ago
  1. More cleaning of RpUnits and RpLibrary? code
  2. added rp_result code to c++/fortran/c code
  3. added rp_children, rp_lib_node[comp,type,id] for fortran code (need to test)
  4. adjusted convert function to recognize statements as follows:

convert("5J","neV")
convert("3.12075e+28neV","J")

  1. made joules a metric unit in RpUnits.cc
  2. tested examples/app-fermi/fortran/fermi.f with new rappture library.

added units conversion.

File size: 7.0 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  INTERFACE: Fortran Rappture Units Source
4 *
5 * ======================================================================
6 *  AUTHOR:  Derrick Kearney, Purdue University
7 *  Copyright (c) 2005
8 *  Purdue Research Foundation, West Lafayette, IN
9 * ======================================================================
10 */
11
12#include "RpUnits.h"
13#include "RpDict.h"
14#include "string.h"
15#include "RpUnitsFInterface.h"
16
17#define DICT_TEMPLATE <int,std::string>
18RpDict DICT_TEMPLATE fortObjDictUnits;
19
20int storeObject_UnitsStr(std::string objectName);
21RpUnits* getObject_UnitsInt(int objKey);
22
23int rp_define_unit( char* unitName,
24                    int* basisName,
25                    int unitName_len    ) {
26
27    int result = -1;
28    RpUnits* newUnit = NULL;
29    RpUnits* basis = NULL;
30    std::string basisStrName = "";
31    std::string newUnitName = "";
32    char* inText = NULL;
33
34    inText = null_terminate(unitName,unitName_len);
35
36    if (basisName && *basisName) {
37        basisStrName = fortObjDictUnits.find(*basisName);
38
39        if (basisStrName != "") {
40            basis = RpUnits::find(basisStrName);
41        }
42    }
43
44    // newUnit = RpUnits::define(unitName, basis);
45    newUnit = RpUnits::define(inText, basis);
46   
47    if (newUnit) {
48        result = storeObject_UnitsStr(newUnit->getUnitsName());
49    }
50
51    if (inText) {
52        free(inText);
53    }
54
55    return result;
56}
57
58
59int rp_find(char* searchName, int searchName_len)
60{
61
62    int result = -1;
63    char* inText = NULL;
64
65    inText = null_terminate(searchName,searchName_len);
66
67    // RpUnits* searchUnit = RpUnits::find(searchName);
68    RpUnits* searchUnit = RpUnits::find(inText);
69
70    if (searchUnit) {
71        result = storeObject_UnitsStr(searchUnit->getUnitsName());
72    }
73
74    if (inText) {
75        free(inText);
76    }
77
78    return result;
79}
80
81int rp_make_metric(int* basis)
82{
83    int result = -1;
84    RpUnits* newBasis = NULL;
85
86    if (basis && *basis) {
87        newBasis = getObject_UnitsInt(*basis);
88
89        if (newBasis) {
90            result = RpUnits::makeMetric(newBasis);
91        }
92    }
93
94    return result;
95}
96
97int rp_get_units(int* unitRefVal, char* retText, int retText_len)
98{
99    RpUnits* unitObj = NULL;
100    std::string unitNameText = "";
101    int result = 0;
102
103    if (unitRefVal && *unitRefVal) {
104        unitObj = getObject_UnitsInt(*unitRefVal);
105        if (unitObj) {
106            unitNameText = unitObj->getUnits();
107            fortranify(unitNameText.c_str(), retText, retText_len);
108        }
109    }
110
111    return result;
112}
113
114int rp_get_units_name(int* unitRefVal, char* retText, int retText_len)
115{
116    RpUnits* unitObj = NULL;
117    std::string unitNameText = "";
118    int result = 0;
119
120    if (unitRefVal && *unitRefVal) {
121        unitObj = getObject_UnitsInt(*unitRefVal);
122        if (unitObj) {
123            unitNameText = unitObj->getUnitsName();
124            fortranify(unitNameText.c_str(), retText, retText_len);
125        }
126    }
127   
128    return result;
129}
130
131int rp_get_exponent(int* unitRefVal, double* retExponent)
132{
133    RpUnits* unitObj = NULL;
134    int result = 0;
135
136    if (unitRefVal && *unitRefVal) {
137        unitObj = getObject_UnitsInt(*unitRefVal);
138        if (unitObj) {
139            *retExponent = unitObj->getExponent();
140        }
141    }
142
143    return result;
144}
145
146int rp_get_basis(int* unitRefVal)
147{
148    RpUnits* unitObj = NULL;
149    RpUnits* basisObj = NULL;
150    int result = -1;
151
152    if (unitRefVal && *unitRefVal) {
153        unitObj = getObject_UnitsInt(*unitRefVal);
154
155        if (unitObj) {
156            basisObj = unitObj->getBasis();
157           
158            if (basisObj) {
159                result = storeObject_UnitsStr(basisObj->getUnitsName());
160            }
161        }
162    }
163
164    return result;
165}
166
167int rp_units_convert_dbl (  char* fromVal,
168                            char* toUnitsName,
169                            double* convResult,
170                            int fromVal_len,
171                            int toUnitsName_len ) {
172
173    char* inFromVal = NULL;
174    char* inToUnitsName = NULL;
175    int result = -1;
176    int showUnits = 0;
177    std::string convStr = "";
178
179    // prepare string for c
180    inFromVal = null_terminate(fromVal,fromVal_len);
181    inToUnitsName = null_terminate(toUnitsName,toUnitsName_len);
182
183    if (inFromVal && inToUnitsName) {
184        // the call to RpUnits::convert() populates the result flag
185        convStr = RpUnits::convert(inFromVal,inToUnitsName,showUnits,&result);
186
187        if (!convStr.empty()) {
188            *convResult = atof(convStr.c_str());
189        }
190    }
191
192    // clean up memory
193    if (inFromVal) {
194        free(inFromVal);
195        inFromVal = NULL;
196    }
197
198    if (inToUnitsName) {
199        free(inToUnitsName);
200        inToUnitsName = NULL;
201    }
202
203    return result;
204}
205
206int rp_units_convert_str (      char* fromVal,
207                                char* toUnitsName,
208                                char* retText,
209                                int fromVal_len,
210                                int toUnitsName_len,
211                                int retText_len     ) {
212
213    char* inFromVal = NULL;
214    char* inToUnitsName = NULL;
215    std::string convStr = "";
216    int showUnits = 1;
217    int result = -1;
218
219    // prepare string for c
220    inFromVal = null_terminate(fromVal,fromVal_len);
221    inToUnitsName = null_terminate(toUnitsName,toUnitsName_len);
222
223    if (inFromVal && inToUnitsName) {
224
225        // do the conversion
226        convStr = RpUnits::convert(inFromVal,inToUnitsName,showUnits,&result);
227
228        if (!convStr.empty()) {
229            // prepare string for fortran
230            fortranify(convStr.c_str(), retText, retText_len);
231        }
232    }
233
234    // clean up out memory
235    if (inFromVal) {
236        free(inFromVal);
237        inFromVal = NULL;
238    }
239
240    if (inToUnitsName) {
241        free(inToUnitsName);
242        inToUnitsName = NULL;
243    }
244
245    // return the same result from the RpUnits::convert call.
246    return result;
247}
248
249int rp_units_add_presets ( char* presetName, int presetName_len) {
250   
251    char* inPresetName = NULL;
252    int result = -1;
253   
254    inPresetName = null_terminate(presetName,presetName_len);
255
256    result = RpUnits::addPresets(inPresetName);
257
258    if (inPresetName) {
259        free(inPresetName);
260        inPresetName = NULL;
261    }
262
263    return result;
264}
265
266
267
268
269//**********************************************************************//
270
271
272int storeObject_UnitsStr(std::string objectName) {
273
274    int retVal = -1;
275    int dictNextKey = fortObjDictUnits.size() + 1;
276    int newEntry = 0;
277
278    if (objectName != "") {
279        // dictionary returns a reference to the inserted value
280        // no error checking to make sure it was successful in entering
281        // the new entry.
282        fortObjDictUnits.set(dictNextKey,objectName, &newEntry);
283    }
284   
285    retVal = dictNextKey;
286    return retVal;
287}
288
289RpUnits* getObject_UnitsInt(int objKey) {
290
291    std::string basisName = *(fortObjDictUnits.find(objKey).getValue());
292
293    if (basisName == *(fortObjDictUnits.getNullEntry().getValue())) {
294        // basisName = "";
295        return NULL;
296    }
297
298   return RpUnits::find(basisName);
299
300}
Note: See TracBrowser for help on using the repository browser.