Skip to content

Commit

Permalink
Merge pull request #20 from gantech/cppLibraryGluecode
Browse files Browse the repository at this point in the history
Cpp library gluecode
  • Loading branch information
ghaymanNREL authored May 5, 2017
2 parents fc50fe9 + ad65e60 commit 692e3dc
Show file tree
Hide file tree
Showing 43 changed files with 2,955 additions and 459 deletions.
33 changes: 32 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#

cmake_minimum_required(VERSION 2.8.12)
project(OpenFAST Fortran C)
project(OpenFAST CXX C Fortran)

include(${CMAKE_SOURCE_DIR}/cmake/FastCmakeUtils.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/FastFortranOptions.cmake)
Expand All @@ -33,6 +33,7 @@ option(DOUBLE_PRECISION "Treat REAL as double precision" on)
option(USE_DLL_INTERFACE "Enable runtime loading of dynamic libraries" on)
option(FPE_TRAP_ENABLED "Enable FPE trap in compiler options" off)
option(ORCA_DLL_LOAD "Enable OrcaFlex Library Load" off)
option(BUILD_FAST_CPP_API "Enable building FAST - C++ API" off)

# Setup Fortran Compiler options based on architecture/compiler
set_fast_fortran()
Expand Down Expand Up @@ -71,6 +72,7 @@ set(FAST_MODULES_LOCAL
orcaflex-interface
extptfm
openfoam
supercontroller
fast-library
)

Expand Down Expand Up @@ -107,3 +109,32 @@ if(BUILD_DOCUMENTATION)
endif()

add_subdirectory(glue-codes)

# Install fortran .mod files also to installation directory
install(CODE
"EXECUTE_PROCESS (COMMAND \"${CMAKE_COMMAND}\" -E copy_directory \"${CMAKE_Fortran_MODULE_DIRECTORY}\" \"${CMAKE_INSTALL_PREFIX}/include/openfast/\")")

# Install the library dependency information
install(EXPORT OpenFASTLibraries
DESTINATION lib/cmake/OpenFAST
FILE OpenFASTLibraries.cmake)

# Create OpenFAST config so that other codes can find OpenFAST
include(CMakePackageConfigHelpers)

set(INCLUDE_INSTALL_DIR include/)
set(LIB_INSTALL_DIR lib/)
set(FTNMOD_INSTALL_DIR include/openfast/)
if (BUILD_FAST_CPP_API)
set(OpenFAST_HAS_CXX_API TRUE)
else()
set(OpenFAST_HAS_CXX_API FALSE)
endif()

configure_package_config_file(
cmake/OpenFASTConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/OpenFASTConfig.cmake
INSTALL_DESTINATION lib/cmake/OpenFAST
PATH_VARS INCLUDE_INSTALL_DIR LIB_INSTALL_DIR FTNMOD_INSTALL_DIR)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/OpenFASTConfig.cmake
DESTINATION lib/cmake/OpenFAST)
9 changes: 5 additions & 4 deletions cmake/FastFortranOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ macro(set_fast_fortran)
get_filename_component(FCNAME "${CMAKE_Fortran_COMPILER}" NAME)

# Abort if we do not have gfortran or Intel Fortran Compiler.
if (NOT (FCNAME MATCHES "gfortran.*" OR FCNAME MATCHES "ifort*"))
if (NOT (${CMAKE_Fortran_COMPILER_ID} STREQUAL "GNU" OR
${CMAKE_Fortran_COMPILER_ID} STREQUAL "Intel"))
message(FATAL_ERROR "OpenFAST requires either GFortran or Intel Fortran Compiler. Compiler detected by CMake: ${FCNAME}")
endif()

Expand All @@ -54,11 +55,11 @@ macro(set_fast_fortran)
include_directories(${CMAKE_Fortran_MODULE_DIRECTORY})

# Get OS/Compiler specific options
if (FCNAME MATCHES "gfortran.*")
if (${CMAKE_Fortran_COMPILER_ID} STREQUAL "GNU")
set_fast_gfortran()
elseif(FCNAME MATCHES "ifort.*")
elseif(${CMAKE_Fortran_COMPILER_ID} STREQUAL "Intel")
set_fast_intel_fortran()
endif(FCNAME MATCHES "gfortran.*")
endif()
endmacro(set_fast_fortran)

#
Expand Down
27 changes: 27 additions & 0 deletions cmake/FindYAMLCPP.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

find_path(YAML_INCLUDES
yaml-cpp/yaml.h
HINTS ${YAML_ROOT} ${CMAKE_INSTALL_PREFIX}
PATH_SUFFIXES include)

find_library(YAML_LIBRARIES
NAMES libyaml-cpp.a yaml-cpp
HINTS ${YAML_ROOT} ${CMAKE_INSTALL_PREFIX}
PATH_SUFFIXES lib)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(YAMLCPP DEFAULT_MSG YAML_INCLUDES YAML_LIBRARIES)
mark_as_advanced(YAML_INCLUDES YAML_LIBRARIES)
65 changes: 65 additions & 0 deletions cmake/OpenFASTConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#
# Copyright 2016 National Renewable Energy Laboratory
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

@PACKAGE_INIT@

# Compilers use by OpenFAST build
set(OpenFAST_CXX_COMPILER "@CMAKE_CXX_COMPILER@")
set(OpenFAST_C_COMPILER "@CMAKE_C_COMPILER@")
set(OpenFAST_Fortran_COMPILER "@CMAKE_Fortran_COMPILER@")

# Compiler flags used by OpenFAST build
set(OpenFAST_CXX_COMPILER_FLAGS "@CMAKE_CXX_FLAGS@")
set(OpenFAST_C_COMPILER_FLAGS "@CMAKE_C_FLAGS@")
set(OpenFAST_Fortran_COMPILER_FLAGS "@CMAKE_Fortran_FLAGS@")

set_and_check(OpenFAST_INCLUDE_DIRS "@PACKAGE_INCLUDE_INSTALL_DIR@")
set_and_check(OpenFAST_LIBRARY_DIRS "@PACKAGE_LIB_INSTALL_DIR@")
set_and_check(OpenFAST_FTNMOD_DIR "@PACKAGE_FTNMOD_INSTALL_DIR@")

set(OpenFAST_LIBRARIES "openfastlib")
set(OpenFAST_CPP_LIBRARIES "openfastcpplib")

include("${CMAKE_CURRENT_LIST_DIR}/OpenFASTLibraries.cmake")

set(OpenFAST_HAS_CXX_API @OpenFAST_HAS_CXX_API@)
set(OpenFAST_FOUND TRUE)

if (NOT OpenFAST_FIND_COMPONENTS)
if (OpenFAST_HAS_CXX_API)
set(OpenFAST_FIND_COMPONENTS "openfastcpplib;openfastlib")
else()
set(OpenFAST_FIND_COMPONENTS "openfastlib")
endif()
endif ()

foreach (comp IN LISTS OpenFAST_FIND_COMPONENTS)
if (${comp} STREQUAL "openfastcpplib")
if (NOT OpenFAST_HAS_CXX_API)
set(OpenFAST_${comp}_FOUND FALSE)
message(WARNING "OpenFAST ${comp} NOT found")
if(OpenFAST_FIND_REQUIRED_${comp})
set(OpenFAST_FOUND FALSE)
endif()
else()
set(OpenFAST_${comp}_FOUND TRUE)
endif()
else()
set(OpenFAST_${comp}_FOUND TRUE)
endif()
endforeach ()

check_required_components(OpenFAST)
4 changes: 4 additions & 0 deletions glue-codes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@

add_subdirectory(fast)

if(BUILD_FAST_CPP_API)
add_subdirectory(fast-cpp)
endif()
67 changes: 67 additions & 0 deletions glue-codes/fast-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#
# Copyright 2016 National Renewable Energy Laboratory
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(MPI REQUIRED)
find_package(HDF5 REQUIRED)
find_package(YAMLCPP REQUIRED)

include_directories(${YAML_INCLUDES})
include_directories(${HDF5_INCLUDES})
include_directories(${CMAKE_SOURCE_DIR}/modules-local/fast-library/src/)
include_directories(${CMAKE_BINARY_DIR}/modules-local/openfoam/)
include_directories(${CMAKE_BINARY_DIR}/modules-local/supercontroller/)
include_directories(${MPI_INCLUDE_PATH})

add_library(openfastcpplib
src/OpenFAST.cpp)
target_link_libraries(openfastcpplib
openfastlib
${HDF5_LIBRARIES}
${CMAKE_DL_LIBS})

add_executable(openfastcpp
src/FAST_Prog.cpp)

target_link_libraries(openfastcpp openfastcpplib openfastlib
${MPI_LIBRARIES} ${YAML_LIBRARIES} ${CMAKE_DL_LIBS})

if(MPI_COMPILE_FLAGS)
set_target_properties(openfastcpp PROPERTIES
COMPILE_FLAGS "${MPI_COMPILE_FLAGS}")
endif(MPI_COMPILE_FLAGS)

if(MPI_LINK_FLAGS)
set_target_properties(openfastcpp PROPERTIES
LINK_FLAGS "${MPI_LINK_FLAGS}")
endif(MPI_LINK_FLAGS)

set_property(TARGET openfastcpp PROPERTY LINKER_LANGUAGE CXX)

install(TARGETS openfastcpplib
EXPORT "${CMAKE_PROJECT_NAME}Libraries"
RUNTIME DESTINATION lib
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib)

install(FILES
src/OpenFAST.H src/SC.h
DESTINATION include)

install(TARGETS openfastcpp
RUNTIME DESTINATION bin)
135 changes: 135 additions & 0 deletions glue-codes/fast-cpp/src/FAST_Prog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include "OpenFAST.H"
#include "yaml-cpp/yaml.h"
#include <iostream>
#include <mpi.h>

inline bool checkFileExists(const std::string& name) {
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}

void readTurbineData(int iTurb, fast::fastInputs & fi, YAML::Node turbNode) {

//Read turbine data for a given turbine using the YAML node
fi.globTurbineData[iTurb].TurbID = turbNode["turb_id"].as<int>();
fi.globTurbineData[iTurb].FASTInputFileName = turbNode["FAST_input_filename"].as<std::string>() ;
fi.globTurbineData[iTurb].FASTRestartFileName = turbNode["restart_filename"].as<std::string>() ;
if (turbNode["turbine_base_pos"].IsSequence() ) {
fi.globTurbineData[iTurb].TurbineBasePos = turbNode["turbine_base_pos"].as<std::vector<double> >() ;
}
if (turbNode["turbine_hub_pos"].IsSequence() ) {
fi.globTurbineData[iTurb].TurbineHubPos = turbNode["turbine_hub_pos"].as<std::vector<double> >() ;
}
fi.globTurbineData[iTurb].numForcePtsBlade = turbNode["num_force_pts_blade"].as<int>();
fi.globTurbineData[iTurb].numForcePtsTwr = turbNode["num_force_pts_tower"].as<int>();

}

void readInputFile(fast::fastInputs & fi, std::string cInterfaceInputFile, double * tEnd) {

fi.comm = MPI_COMM_WORLD;

// Check if the input file exists and read it
if ( checkFileExists(cInterfaceInputFile) ) {

YAML::Node cDriverInp = YAML::LoadFile(cInterfaceInputFile);

fi.nTurbinesGlob = cDriverInp["nTurbinesGlob"].as<int>();

if (fi.nTurbinesGlob > 0) {

if(cDriverInp["dryRun"]) {
fi.dryRun = cDriverInp["dryRun"].as<bool>();
}

if(cDriverInp["debug"]) {
fi.debug = cDriverInp["debug"].as<bool>();
}

fi.tStart = cDriverInp["tStart"].as<double>();
*tEnd = cDriverInp["tEnd"].as<double>();
fi.nEveryCheckPoint = cDriverInp["nEveryCheckPoint"].as<int>();
fi.dtFAST = cDriverInp["dtFAST"].as<double>();
fi.tMax = cDriverInp["tMax"].as<double>(); // tMax is the total duration to which you want to run FAST. This should be the same or greater than the max time given in the FAST fst file. Choose this carefully as FAST writes the output file only at this point if you choose the binary file output.

if(cDriverInp["superController"]) {
fi.scStatus = cDriverInp["superController"].as<bool>();
fi.scLibFile = cDriverInp["scLibFile"].as<std::string>();
fi.numScInputs = cDriverInp["numScInputs"].as<int>();
fi.numScOutputs = cDriverInp["numScOutputs"].as<int>();
}

fi.globTurbineData.resize(fi.nTurbinesGlob);
for (int iTurb=0; iTurb < fi.nTurbinesGlob; iTurb++) {
if (cDriverInp["Turbine" + std::to_string(iTurb)]) {
readTurbineData(iTurb, fi, cDriverInp["Turbine" + std::to_string(iTurb)] );
} else {
throw std::runtime_error("Node for Turbine" + std::to_string(iTurb) + " not present in input file or I cannot read it");
}
}

} else {
throw std::runtime_error("Number of turbines <= 0 ");
}

} else {
throw std::runtime_error("Input file " + cInterfaceInputFile + " does not exist or I cannot access it");
}

}

int main() {
int iErr;
int nProcs;
int rank;
std::vector<double> torque (3, 0.0);
std::vector<double> thrust (3, 0.0);

iErr = MPI_Init(NULL, NULL);
iErr = MPI_Comm_size( MPI_COMM_WORLD, &nProcs);
iErr = MPI_Comm_rank( MPI_COMM_WORLD, &rank);

double tEnd ; // This doesn't belong in the FAST - C++ interface
int ntEnd ; // This doesn't belong in the FAST - C++ interface

std::string cDriverInputFile="cDriver.i";
fast::OpenFAST FAST;
fast::fastInputs fi ;
try {
readInputFile(fi, cDriverInputFile, &tEnd);
}
catch( const std::runtime_error & ex) {
std::cerr << ex.what() << std::endl ;
std::cerr << "Program quitting now" << std::endl ;
return 1;
}
ntEnd = tEnd/fi.dtFAST; //Calculate the last time step

FAST.setInputs(fi);
FAST.allocateTurbinesToProcsSimple();
// Or allocate turbines to procs by calling "setTurbineProcNo(iTurbGlob, procId)" for turbine.

FAST.init();
if (!FAST.isRestart()) {
FAST.solution0();
}

if( !FAST.isDryRun() ) {
for (int nt = FAST.get_ntStart(); nt < ntEnd; nt++) {
FAST.step();
if (FAST.isDebug()) {
FAST.computeTorqueThrust(0,torque,thrust);
std::cout.precision(16);
std::cout << "Torque = " << torque[0] << " " << torque[1] << " " << torque[2] << std::endl ;
std::cout << "Thrust = " << thrust[0] << " " << thrust[1] << " " << thrust[2] << std::endl ;
}
}
}

FAST.end() ;
MPI_Finalize() ;

return 0;

}

Loading

0 comments on commit 692e3dc

Please sign in to comment.