Skip to content

Commit

Permalink
Add of optional arguments to addVariablesExportAsCSV
Browse files Browse the repository at this point in the history
* Added optional argument unitid for one selected unit in unitclass
* Added optional argument varname for selected variable(s)
* Added optional argument precision for outputs digits number
* Updated tests to match new developments
  • Loading branch information
crevoisier committed Mar 15, 2017
1 parent c158bff commit 5d3d1be
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 13 deletions.
1 change: 1 addition & 0 deletions cmake/preprocess.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ CONFIGURE_FILE(${RESOURCESDIR}/man/ROpenFLUID-package.Rd.in ${PACKAGEBUILDDIR}/m
CONFIGURE_FILE(${RESOURCESDIR}/tests/00tests.R.in ${PACKAGEBUILDDIR}/tests/00tests.R @ONLY)
CONFIGURE_FILE(${RESOURCESDIR}/tests/PrimitivesExample.R.in ${PACKAGEBUILDDIR}/tests/PrimitivesExample.R @ONLY)
CONFIGURE_FILE(${RESOURCESDIR}/tests/PrimitivesExampleVerbose.R.in ${PACKAGEBUILDDIR}/tests/PrimitivesExampleVerbose.R @ONLY)
CONFIGURE_FILE(${RESOURCESDIR}/tests/PrimitivesExampleSelectedOutputs.R.in ${PACKAGEBUILDDIR}/tests/PrimitivesExampleSelectedOutputs.R @ONLY)



Expand Down
2 changes: 1 addition & 1 deletion config.in.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ SET(OpenFLUID_R_TITLE "R Interface to OpenFLUID Platform Framework for Modelling
SET(OpenFLUID_R_DESC "Provides a collection of functions to load, parameterize, run and analyze OpenFLUID simulations within the GNU R environment.")

# Version
SET(OpenFLUID_R_VERSION_PATCH "20160707")
SET(OpenFLUID_R_VERSION_PATCH "20170315")


# ===========================================================================================
Expand Down
16 changes: 12 additions & 4 deletions package/R/ROpenFLUID.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,27 @@ OpenFLUID.addExtraSimulatorsPaths <- function(paths)
#'
#' @param ofblob the simulation definition blob
#' @param unitclass the units class to add for simulation variables export
#' @param unitid the unit ID (optional)
#' @param varname the name of the variable(s) (optional)
#' @param precision the number of digits of the variables (optional)
#'
#' @examples \dontrun{
#' OpenFLUID.addVariablesExportAsCSV("TU")
#' OpenFLUID.addVariablesExportAsCSV("RS")
#' OpenFLUID.addVariablesExportAsCSV(ofsim,"TU")
#' OpenFLUID.addVariablesExportAsCSV(ofsim,"TU",1,"var1",precision=14)
#' OpenFLUID.addVariablesExportAsCSV(ofsim,"TU",2,"var1;var2")
#' OpenFLUID.addVariablesExportAsCSV(ofsim,"TU",1,"*")
#' }
#'
#' @seealso \code{\link{OpenFLUID.loadResult}}
OpenFLUID.addVariablesExportAsCSV <- function(ofblob,unitclass)
OpenFLUID.addVariablesExportAsCSV <- function(ofblob,unitclass,unitid=-1,varname="",precision=-1)
{
stopifnot(!is.null(ofblob))
stopifnot(is.character(unitclass))
stopifnot(is.numeric(unitid))
stopifnot(is.character(varname))
stopifnot(is.numeric(precision))

.Call("AddVariablesExportAsCSV", ofblob, unitclass, PACKAGE="ROpenFLUID")
.Call("AddVariablesExportAsCSV", ofblob, unitclass, as.integer(unitid), varname, as.integer(precision), PACKAGE="ROpenFLUID")

return(invisible(NULL))
}
Expand Down
27 changes: 24 additions & 3 deletions package/src/ROpenFLUID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1232,11 +1232,12 @@ void ROpenFLUID_RemoveAttribute(ROpenFLUID_ExtBlob_t* BlobHandle, const char* Un
// =====================================================================


void ROpenFLUID_AddVariablesExportAsCSV(ROpenFLUID_ExtBlob_t* BlobHandle, const char* UnitClass)
void ROpenFLUID_AddVariablesExportAsCSV(ROpenFLUID_ExtBlob_t* BlobHandle, const char* UnitClass, int UnitID, const char* VarName, int Prec)
{
ROpenFLUID_Blob_t* Data(reinterpret_cast<ROpenFLUID_Blob_t*>(BlobHandle));

std::string UnitClassStr(UnitClass);
std::string VarNameStr(VarName);
openfluid::fluidx::AdvancedMonitoringDescriptor AdvMonDesc(Data->FluidXDesc.monitoringDescriptor());

// 1. add CSV observer if not present
Expand All @@ -1251,11 +1252,31 @@ void ROpenFLUID_AddVariablesExportAsCSV(ROpenFLUID_ExtBlob_t* BlobHandle, const
ObsDesc.setParameter("format.ropenfluid.header",openfluid::core::StringValue("colnames-as-data"));
ObsDesc.setParameter("format.ropenfluid.date",openfluid::core::StringValue("%Y%m%d-%H%M%S"));
ObsDesc.setParameter("format.ropenfluid.colsep",openfluid::core::StringValue(" "));
if (Prec != -1)
{
std::ostringstream ssPrec;
ssPrec << Prec;
std::string PrecStr(ssPrec.str());
ObsDesc.setParameter("format.ropenfluid.precision",openfluid::core::StringValue(PrecStr));
}

// 3. add ropenfluidUnitClass output set
ObsDesc.setParameter("set.ropenfluid"+UnitClassStr+".unitclass",openfluid::core::StringValue(UnitClassStr));
ObsDesc.setParameter("set.ropenfluid"+UnitClassStr+".unitsIDs",openfluid::core::StringValue("*"));
ObsDesc.setParameter("set.ropenfluid"+UnitClassStr+".vars",openfluid::core::StringValue("*"));
if (UnitID != -1) {
std::ostringstream ssUnitID;
ssUnitID << UnitID;
std::string UnitIDStr(ssUnitID.str());
ObsDesc.setParameter("set.ropenfluid"+UnitClassStr+".unitsIDs",openfluid::core::StringValue(UnitIDStr));
}
else {
ObsDesc.setParameter("set.ropenfluid"+UnitClassStr+".unitsIDs",openfluid::core::StringValue("*"));
}
if (!VarNameStr.empty()) {
ObsDesc.setParameter("set.ropenfluid"+UnitClassStr+".vars",openfluid::core::StringValue(VarNameStr));
}
else {
ObsDesc.setParameter("set.ropenfluid"+UnitClassStr+".vars",openfluid::core::StringValue("*"));
}
ObsDesc.setParameter("set.ropenfluid"+UnitClassStr+".format",openfluid::core::StringValue("ropenfluid"));
}

Expand Down
2 changes: 1 addition & 1 deletion package/src/ROpenFLUID.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ const char* ROpenFLUID_GetAttribute(ROpenFLUID_ExtBlob_t* BlobHandle, const char

void ROpenFLUID_RemoveAttribute(ROpenFLUID_ExtBlob_t* BlobHandle, const char* UnitClass, const char* AttrName);

void ROpenFLUID_AddVariablesExportAsCSV(ROpenFLUID_ExtBlob_t* BlobHandle, const char* UnitClass);
void ROpenFLUID_AddVariablesExportAsCSV(ROpenFLUID_ExtBlob_t* BlobHandle, const char* UnitClass, int UnitID, const char* VarName, int Prec);


#ifdef __cplusplus
Expand Down
8 changes: 4 additions & 4 deletions package/src/ROpenFLUIDWrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static SEXP Rized_OpenFLUID_SetPeriod(SEXP Blob, SEXP Begin, SEXP End);
static SEXP Rized_OpenFLUID_GetPeriodBeginDate(SEXP Blob);
static SEXP Rized_OpenFLUID_GetPeriodEndDate(SEXP Blob);

static SEXP Rized_OpenFLUID_AddVariablesExportAsCSV(SEXP Blob, SEXP UnitClass);
static SEXP Rized_OpenFLUID_AddVariablesExportAsCSV(SEXP Blob, SEXP UnitClass, SEXP UnitID, SEXP VarName, SEXP Prec);



Expand Down Expand Up @@ -135,7 +135,7 @@ R_CallMethodDef callEntries[] = {
{ "SetPeriod", (DL_FUNC) &Rized_OpenFLUID_SetPeriod, 3},
{ "GetPeriodBeginDate", (DL_FUNC) &Rized_OpenFLUID_GetPeriodBeginDate, 1},
{ "GetPeriodEndDate", (DL_FUNC) &Rized_OpenFLUID_GetPeriodEndDate, 1},
{ "AddVariablesExportAsCSV", (DL_FUNC) &Rized_OpenFLUID_AddVariablesExportAsCSV, 2},
{ "AddVariablesExportAsCSV", (DL_FUNC) &Rized_OpenFLUID_AddVariablesExportAsCSV, 5},
{ NULL, NULL, 0}
};

Expand Down Expand Up @@ -858,8 +858,8 @@ SEXP Rized_OpenFLUID_GetPeriodEndDate(SEXP Blob)
// =====================================================================


SEXP Rized_OpenFLUID_AddVariablesExportAsCSV(SEXP Blob, SEXP UnitClass)
SEXP Rized_OpenFLUID_AddVariablesExportAsCSV(SEXP Blob, SEXP UnitClass, SEXP UnitID, SEXP VarName, SEXP Prec)
{
ROpenFLUID_AddVariablesExportAsCSV(R_ExternalPtrAddr(Blob),CHAR(STRING_ELT(UnitClass, 0)));
ROpenFLUID_AddVariablesExportAsCSV(R_ExternalPtrAddr(Blob),CHAR(STRING_ELT(UnitClass, 0)),INTEGER(UnitID)[0],CHAR(STRING_ELT(VarName, 0)),INTEGER(Prec)[0]);
}

22 changes: 22 additions & 0 deletions resources/tests/PrimitivesExampleSelectedOutputs.R.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
library(RUnit)

library("ROpenFLUID")

ofsim = OpenFLUID.openDataset("@OpenFLUID_PREFIX@/share/doc/openfluid/examples/projects/Primitives/IN")

OpenFLUID.setCurrentOutputDir(paste(getwd(),'/PrimitivesSelectedOutputs.OUT', sep = ""))

OpenFLUID.addExtraSimulatorsPaths("@OpenFLUID_PREFIX@/lib/openfluid/simulators")

OpenFLUID.addVariablesExportAsCSV(ofsim,"unitsB",7,"var5",precision=14)
OpenFLUID.addVariablesExportAsCSV(ofsim,"unitsA",1,"*")

OpenFLUID.printSimulationInfo(ofsim)

OpenFLUID.runSimulation(ofsim)

out=OpenFLUID.loadResult(ofsim,"unitsB",7,"var5")
plot(out$var5,type='l',col='blue')

out=OpenFLUID.loadResult(ofsim,"unitsA",1,"var1")
lines(out$var1,type='l',col='green')

0 comments on commit 5d3d1be

Please sign in to comment.