From 1454236a81d9737abb480dc9d552390b76e6b667 Mon Sep 17 00:00:00 2001 From: Alex Lin Date: Mon, 9 May 2016 16:40:48 -0500 Subject: [PATCH] Use udunits package for units conversions Created a check in the configure script looking for the udunits header files. Also added a sim to test some of the new units capabilities. refs #231 --- autoconf/configure.ac | 28 +++ configure | 90 +++++++++ share/trick/makefiles/Makefile.common | 4 +- share/trick/makefiles/config_Linux.mk | 3 - share/trick/makefiles/config_user.mk.in | 2 + test/SIM_measurement_units/RUN_test/input.py | 194 +++++++++++++++++++ test/SIM_measurement_units/S_define | 117 +++++++++++ test/SIM_measurement_units/S_overrides.mk | 2 + test/makefile | 1 + 9 files changed, 436 insertions(+), 5 deletions(-) create mode 100644 test/SIM_measurement_units/RUN_test/input.py create mode 100644 test/SIM_measurement_units/S_define create mode 100644 test/SIM_measurement_units/S_overrides.mk diff --git a/autoconf/configure.ac b/autoconf/configure.ac index d785a75f..256103a9 100644 --- a/autoconf/configure.ac +++ b/autoconf/configure.ac @@ -106,6 +106,15 @@ AC_DEFUN([AX_GTEST_HOME],[ AC_SUBST([GTEST_HOME]) ]) +AC_DEFUN([AX_UDUNITS_HOME],[ + AC_ARG_WITH([udunits], + AS_HELP_STRING([--with-udunits@<:@=DIR@:>@], [UDUnits root directory]), + [UDUNITS_HOME="$withval"], + [UDUNITS_HOME=""] + ) + AC_SUBST([UDUNITS_HOME]) +]) + dnl test if we want to use er7_utils, default to yes AC_DEFUN([AX_ER7_UTILS],[ AC_ARG_ENABLE([er7utils], @@ -181,6 +190,25 @@ AC_CHECK_FILE([$LLVM_LIB_DIR/libLLVMSupport.a], AC_PATH_PROG(CLANG, clang, noclang, "$LLVM_BIN_DIR:$LLVM_HOME/bin:/bin:/usr/bin:/usr/local/bin:/sw/bin") AS_IF([test "$ac_cv_path_CLANG" = "noclang"],AC_MSG_ERROR([could not find clang]),[]) +dnl look for udunits in /usr/include and /usr/include/udunits2 +AX_UDUNITS_HOME([]) +AS_IF([test "$UDUNITS_HOME" = ""], + [ + AC_CHECK_HEADER([udunits2.h], [UDUNITS_INCLUDES=], + AC_CHECK_HEADER([udunits2/udunits2.h],[UDUNITS_INCLUDES=-I/usr/include/udunits2],AC_MSG_ERROR([could not find udunits development headers]))) + ], + [ + AC_CHECK_FILE([$UDUNITS_HOME/include/udunits2.h], + [UDUNITS_INCLUDES=-I$UDUNITS_HOME/include], + [AC_CHECK_FILE([$UDUNITS_HOME/lib/udunits2.h], + [UDUNITS_INCLUDES=-I$UDUNITS_HOME/lib], + AC_MSG_ERROR([could not find udunits2])) + ] + ) + ] +) +AC_SUBST([UDUNITS_INCLUDES]) + dnl process the other optional command line arguments AX_HDF5_HOME([]) AX_DMTCP_HOME([]) diff --git a/configure b/configure index bed3ead8..155db31e 100755 --- a/configure +++ b/configure @@ -627,6 +627,8 @@ BC635_HOME TPROCTE_HOME DMTCP_HOME HDF5_HOME +UDUNITS_INCLUDES +UDUNITS_HOME CLANG LLVM_CONFIG LLVM_HOME @@ -720,6 +722,7 @@ ac_user_opts=' enable_option_checking with_x with_llvm +with_udunits with_hdf5 with_dmtcp with_tpro @@ -1368,6 +1371,7 @@ Optional Packages: --without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no) --with-x use the X Window System --with-llvm[=DIR] LLVM root directory + --with-udunits[=DIR] UDUnits root directory --with-hdf5[=DIR] HDF5 root directory --with-dmtcp[=DIR] DMTCP root directory --with-tpro[=DIR] TPRO root directory @@ -2243,6 +2247,8 @@ ac_configure="$SHELL $ac_aux_dir/configure" # Please don't use this var. + + @@ -5655,6 +5661,90 @@ fi +# Check whether --with-udunits was given. +if test "${with_udunits+set}" = set; then : + withval=$with_udunits; UDUNITS_HOME="$withval" +else + UDUNITS_HOME="" + +fi + + + +if test "$UDUNITS_HOME" = ""; then : + + ac_fn_cxx_check_header_mongrel "$LINENO" "udunits2.h" "ac_cv_header_udunits2_h" "$ac_includes_default" +if test "x$ac_cv_header_udunits2_h" = xyes; then : + UDUNITS_INCLUDES= +else + ac_fn_cxx_check_header_mongrel "$LINENO" "udunits2/udunits2.h" "ac_cv_header_udunits2_udunits2_h" "$ac_includes_default" +if test "x$ac_cv_header_udunits2_udunits2_h" = xyes; then : + UDUNITS_INCLUDES=-I/usr/include/udunits2 +else + as_fn_error $? "could not find udunits development headers" "$LINENO" 5 +fi + + +fi + + + +else + + as_ac_File=`$as_echo "ac_cv_file_$UDUNITS_HOME/include/udunits2.h" | $as_tr_sh` +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $UDUNITS_HOME/include/udunits2.h" >&5 +$as_echo_n "checking for $UDUNITS_HOME/include/udunits2.h... " >&6; } +if eval \${$as_ac_File+:} false; then : + $as_echo_n "(cached) " >&6 +else + test "$cross_compiling" = yes && + as_fn_error $? "cannot check for file existence when cross compiling" "$LINENO" 5 +if test -r "$UDUNITS_HOME/include/udunits2.h"; then + eval "$as_ac_File=yes" +else + eval "$as_ac_File=no" +fi +fi +eval ac_res=\$$as_ac_File + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5 +$as_echo "$ac_res" >&6; } +if eval test \"x\$"$as_ac_File"\" = x"yes"; then : + UDUNITS_INCLUDES=-I$UDUNITS_HOME/include +else + as_ac_File=`$as_echo "ac_cv_file_$UDUNITS_HOME/lib/udunits2.h" | $as_tr_sh` +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $UDUNITS_HOME/lib/udunits2.h" >&5 +$as_echo_n "checking for $UDUNITS_HOME/lib/udunits2.h... " >&6; } +if eval \${$as_ac_File+:} false; then : + $as_echo_n "(cached) " >&6 +else + test "$cross_compiling" = yes && + as_fn_error $? "cannot check for file existence when cross compiling" "$LINENO" 5 +if test -r "$UDUNITS_HOME/lib/udunits2.h"; then + eval "$as_ac_File=yes" +else + eval "$as_ac_File=no" +fi +fi +eval ac_res=\$$as_ac_File + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5 +$as_echo "$ac_res" >&6; } +if eval test \"x\$"$as_ac_File"\" = x"yes"; then : + UDUNITS_INCLUDES=-I$UDUNITS_HOME/lib +else + as_fn_error $? "could not find udunits2" "$LINENO" 5 +fi + + + +fi + + + +fi + + + + # Check whether --with-hdf5 was given. if test "${with_hdf5+set}" = set; then : withval=$with_hdf5; HDF5_HOME="$withval" diff --git a/share/trick/makefiles/Makefile.common b/share/trick/makefiles/Makefile.common index 8682b211..40817b41 100644 --- a/share/trick/makefiles/Makefile.common +++ b/share/trick/makefiles/Makefile.common @@ -71,8 +71,8 @@ TRICK_SYSTEM_CXXFLAGS = $(TRICK_INCLUDES) $(TRICK_VERSIONS) -fpic TRICK_SYSTEM_SFLAGS = -I${TRICK_HOME}/share/trick $(TRICK_INCLUDES) $(TRICK_VERSIONS) # append additional c and cxx flags defined in config_*.mk files. -TRICK_SYSTEM_CFLAGS += $(TRICK_ADDITIONAL_CFLAGS) -TRICK_SYSTEM_CXXFLAGS += $(TRICK_ADDITIONAL_CXXFLAGS) +TRICK_SYSTEM_CFLAGS += $(TRICK_ADDITIONAL_CFLAGS) $(UDUNITS_INCLUDES) +TRICK_SYSTEM_CXXFLAGS += $(TRICK_ADDITIONAL_CXXFLAGS) $(UDUNITS_INCLUDES) # -fexceptions is included to propogate execptions through C code. TRICK_SYSTEM_CFLAGS += -fexceptions diff --git a/share/trick/makefiles/config_Linux.mk b/share/trick/makefiles/config_Linux.mk index 50f26b7a..b91fe69b 100644 --- a/share/trick/makefiles/config_Linux.mk +++ b/share/trick/makefiles/config_Linux.mk @@ -64,6 +64,3 @@ ifeq "" "c++11" TRICK_ADDITIONAL_CXXFLAGS += -std=c++11 -D_HAVE_STL_RANDOM endif -# Additional include directory for udunits. -TRICK_ADDITIONAL_CXXFLAGS += -I/usr/include/udunits2 - diff --git a/share/trick/makefiles/config_user.mk.in b/share/trick/makefiles/config_user.mk.in index ce14414a..327d8f6a 100644 --- a/share/trick/makefiles/config_user.mk.in +++ b/share/trick/makefiles/config_user.mk.in @@ -19,6 +19,8 @@ PTHREAD_LIBS = @PTHREAD_LIBS@ LLVM_HOME = @LLVM_HOME@ MOTIF_HOME = @MOTIF_HOME@ +UDUNITS_INCLUDES = @UDUNITS_INCLUDES@ + HDF5 = @HDF5_HOME@ DMTCP = @DMTCP_HOME@ TPROCTE = @TPROCTE_HOME@ diff --git a/test/SIM_measurement_units/RUN_test/input.py b/test/SIM_measurement_units/RUN_test/input.py new file mode 100644 index 00000000..c0dad836 --- /dev/null +++ b/test/SIM_measurement_units/RUN_test/input.py @@ -0,0 +1,194 @@ + +def main(): + + unit.s = 1 + unit.ps = unit.s + unit.ns = unit.s + unit.us = unit.s + unit.ms = unit.s + unit.cs = unit.s + unit.ds = unit.s + unit.das = unit.s + unit.hs = unit.s + unit.ks = unit.s + unit.Ms = unit.s + unit.Gs = unit.s + unit.Ts = unit.s + unit.min = unit.s + unit.hr = unit.s + unit.day = unit.s + print unit.s + print unit.ps + print unit.ns + print unit.us + print unit.ms + print unit.cs + print unit.ds + print unit.das + print unit.hs + print unit.ks + print unit.Ms + print unit.Gs + print unit.Ts + print unit.min + print unit.hr + print unit.day + + unit.m = 1 + unit.ft = unit.m + unit.kft = unit.m + unit.inch = unit.m + unit.yd = unit.m + unit.mi = unit.m + unit.nmile = unit.m + print unit.m + print unit.ft + print unit.kft + print unit.inch + print unit.yd + print unit.mi + print unit.nmile + + unit.rad = trick.attach_units("r", 10) + + unit.rad = 1 + unit.degree = unit.rad + unit.arcsecond = unit.rad + unit.arcminute = unit.rad + unit.revolution = unit.rad + print unit.rad + print unit.degree + print unit.arcsecond + print unit.arcminute + print unit.revolution + + unit.g = 1000 + unit.kg = unit.g + unit.t = unit.g + unit.slug = unit.g + unit.lb = unit.g + print unit.g + print unit.kg + print unit.t + print unit.slug + print unit.lb + + unit.N = 1 + unit.ozf = unit.N + unit.lbf = unit.N + print unit.N + print unit.ozf + print unit.lbf + + unit.V = 1 + unit.A = 1 + unit.ohm = 1 + print unit.V + print unit.A + print unit.ohm + + unit.degC = 0 + unit.degF = unit.degC + unit.K = unit.degC + unit.degR = unit.degC + print unit.degC + print unit.degF + print unit.K + print unit.degR + + unit.J = 1 + unit.BTU = unit.J + unit.ton_TNT = unit.J + unit.Wh = unit.J + unit.kWh = unit.J + print unit.J + print unit.BTU + print unit.ton_TNT + print unit.Wh + print unit.kWh + + unit.W = 1 + unit.hp = unit.W + print unit.W + print unit.hp + + unit.Pa = 1 + unit.psi = unit.Pa + unit.atm = unit.Pa + unit.mmHg = unit.Pa + unit.inHg = unit.Pa + unit.cmH2O = unit.Pa + unit.inch_H2O_39F = unit.Pa + print unit.Pa + print unit.psi + print unit.atm + print unit.mmHg + print unit.inHg + print unit.cmH2O + print unit.inch_H2O_39F + + unit.l = 1 + unit.gallon = unit.l + unit.floz = unit.l + print unit.l + print unit.gallon + print unit.floz + + unit.Hz = 1 + print unit.Hz + + unit.one = 1 + unit.count = unit.one + print unit.one + print unit.count + + unit.g = 1 + unit.mps = 3e8 + unit.J = unit.g * unit.mps * unit.mps + print unit.J + + unit.one = 10 + unit.s = 0.01 + unit.Hz = unit.one / unit.s + print unit.Hz + + unit.fpf = 10 + unit.mps = unit.fpf + print unit.fpf, "=", unit.mps + + unit.furlong = 100 + unit.fortnight = 10 + unit.mps = unit.furlong / unit.fortnight + print unit.furlong, "/", unit.fortnight, "=", unit.mps + + unit.m = 3 + temp = unit.m**3 + unit.m3 = unit.m**3 + print temp + print unit.m3 + + unit.s = 0.05 + temp = 1 / unit.s + unit.Hz = 1 / unit.s + print temp + print unit.Hz + + unit.dashdash = 3 + temp = 5 ** unit.dashdash + print temp + + unit.dashdash = 2.5 + temp = 5 ** unit.dashdash + print temp + + unit.s = 5 + unit.dashdash = 2.5 + unit.s *= unit.dashdash + print unit.s + + trick.stop(1.0) + +if __name__ == "__main__": + main() + + diff --git a/test/SIM_measurement_units/S_define b/test/SIM_measurement_units/S_define new file mode 100644 index 00000000..0ca6da9c --- /dev/null +++ b/test/SIM_measurement_units/S_define @@ -0,0 +1,117 @@ +/************************TRICK HEADER************************* +PURPOSE: + (blah blah blah) +*************************************************************/ + +#include "sim_objects/default_trick_sys.sm" + +class unitSimObject : public Trick::SimObject { + + public: + + double r ; // trick_units((r)) test + double r2 ; // trick_units(r2) test + double r2pers ; // trick_units(r2/s) test + double rhat2pers ; // trick_units(r^2/s) test + double d ; // trick_units(d) test + double cnt ; // trick_units(cnt) test + double crazy ; // trick_units(as^2*am3/d2/r*cnt/one*F/C*R/v) test + double another_crazy ; // trick_units((m^2*kg3)/(degree2/rad)*(count/1)*(degF/degC*degR/V)) test + double test ; // trick_units(m3/s2) test + + double temp_F ; // trick_units(degF) test + double temp_C ; // trick_units(degC) test + + double fpf ; // trick_units(furlong/fortnight) test + double mps ; // trick_units(m/s) test + + double s ; // trick_units(s) test + double ps ; // trick_units(ps) test + double ns ; // trick_units(ns) test + double us ; // trick_units(us) test + double ms ; // trick_units(ms) test + double cs ; // trick_units(cs) test + double ds ; // trick_units(ds) test + double das ; // trick_units(das) test + double hs ; // trick_units(hs) test + double ks ; // trick_units(ks) test + double Ms ; // trick_units(Ms) test + double Gs ; // trick_units(Gs) test + double Ts ; // trick_units(Ts) test + double min ; // trick_units(min) test + double hr ; // trick_units(hr) test + double day ; // trick_units(day) test + + double m ; // trick_units(m) test + double ft ; // trick_units(ft) test + double kft ; // trick_units(kft) test + double inch ; // trick_units(in) test + double yd ; // trick_units(yd) test + double mi ; // trick_units(mi) test + double nmile ; // trick_units(nmile) test + + double rad ; // trick_units(rad) test + double degree ; // trick_units(degree) test + double arcsecond ; // trick_units(arcsecond) test + double arcminute ; // trick_units(arcminute) test + double revolution ; // trick_units(revolution) test + + double g ; // trick_units(g) test + double kg ; // trick_units(kg) test + double t ; // trick_units(t) test + double slug ; // trick_units(slug) test + double lb ; // trick_units(lb) test + + double N ; // trick_units(N) test + double ozf ; // trick_units(ozf) test + double lbf ; // trick_units(lbf) test + + double V ; // trick_units(V) test + double A ; // trick_units(A) test + double ohm ; // trick_units(ohm) test + + double degC ; // trick_units(degC) test + double degF ; // trick_units(degF) test + double K ; // trick_units(K) test + double degR ; // trick_units(degR) test + + double J ; // trick_units(J) test + double BTU ; // trick_units(BTU) test + double ton_TNT ; // trick_units(ton_TNT) test + double Wh ; // trick_units(W*h) test + double kWh ; // trick_units(kW*h) test + + double W ; // trick_units(W) test + double hp ; // trick_units(hp) test + + double Pa ; // trick_units(Pa) test + double psi ; // trick_units(psi) test + double atm ; // trick_units(atm) test + double mmHg ; // trick_units(mmHg) test + double inHg ; // trick_units(inHg) test + double cmH2O ; // trick_units(cmH2O) test + double inch_H2O_39F ; // trick_units(inch_H2O_39F) test + + double l ; // trick_units(l) test + double gallon ; // trick_units(gallon) test + double floz ; // trick_units(floz) test + + double Hz ; // trick_units(Hz) test + + double one ; // trick_units(1) test + double count ; // trick_units(count) test + + double mol ; // trick_units(mol) test + + double furlong ; // trick_units(furlong) test + double fortnight ; // trick_units(fortnight) test + + double m3 ; // trick_units(m^3) test + double dashdash ; // trick_units(--) test + + unitSimObject() { } +} ; + +// Instantiations +unitSimObject unit ; + diff --git a/test/SIM_measurement_units/S_overrides.mk b/test/SIM_measurement_units/S_overrides.mk new file mode 100644 index 00000000..9cb5f154 --- /dev/null +++ b/test/SIM_measurement_units/S_overrides.mk @@ -0,0 +1,2 @@ + +#TRICK_ICGFLAGS += -units-truth-is-scary diff --git a/test/makefile b/test/makefile index 329b1aa0..faa659c0 100644 --- a/test/makefile +++ b/test/makefile @@ -18,6 +18,7 @@ COMPILE_DIRS = \ SIMS_NEEDING_TEST = \ SIM_alloc_test \ SIM_demo_inputfile \ + SIM_measurement_units \ SIM_test_abstract \ SIM_test_inherit \ SIM_test_ip2 \