Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cpp library gluecode #20

Merged
merged 27 commits into from
May 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a28a81a
First attempt at adding cpp interface library and glue-code.
gantech Mar 23, 2017
e109ed7
Merge branch 'cppInterface' into cppLibraryGluecode
gantech Mar 23, 2017
ddd4565
Bunch of changes
gantech Mar 23, 2017
bb98706
Fix warning regarding return from non-void function
sayerhs Mar 23, 2017
42c288a
Update CMake build for FAST CPP API
sayerhs Mar 23, 2017
1fe607f
Merge pull request #1 from sayerhs/f/fastcpp
gantech Mar 23, 2017
444f49e
[BUGFIX] Remove unused library dependency from CMake compile rules.
sayerhs Mar 23, 2017
88577a8
Merge pull request #2 from sayerhs/f/fastcpp
gantech Mar 23, 2017
a39a1d6
[BUGFIX] Sequence building of SuperController_Types
sayerhs Mar 23, 2017
00b98f5
Merge pull request #3 from sayerhs/f/fastcpp
gantech Mar 24, 2017
0863b31
Updates to the C++ API. Also changing compilation options to work on …
gantech Mar 28, 2017
a7fda75
Temporary fix to exit after writing all turbine output files
gantech Mar 29, 2017
c95ae89
Updating the cpp interface to not call NormStop after deallocating me…
gantech Mar 29, 2017
d7091e6
Writing the turbine allocation to files for a cleaner test
gantech Mar 29, 2017
e682faf
Major changes to C++ API
gantech Mar 29, 2017
fe136b4
Installing mapcpplib as it is required by Nalu for linking
gantech Mar 30, 2017
4427991
Add OpenFASTConfig.cmake
sayerhs Apr 5, 2017
cc1a550
Merge pull request #4 from sayerhs/f/fastcpp
gantech Apr 5, 2017
f46724d
[BUGFIX] Add missing template config file
sayerhs Apr 5, 2017
4fec490
Merge remote-tracking branch 'gantech/cppLibraryGluecode' into f/fastcpp
sayerhs Apr 5, 2017
e8ee9e3
Updating the OpenFAST - C++ interface to be upto date with https://gi…
gantech Apr 24, 2017
9f47cd6
Updating output for a regression test and removing yaml-cpp library r…
gantech Apr 24, 2017
860e324
Merge branch 'dev' into cppLibraryGluecode
gantech Apr 24, 2017
00fb04c
Fix BeamDyn library export rules in CMake
sayerhs Apr 26, 2017
fcbcd32
Use compiler ID to check for compiler flavor instead of executable name
sayerhs Apr 26, 2017
a53c412
Adding a few more functions to access for a CFD solver
gantech Apr 28, 2017
ad65e60
Include installation of SC.h
gantech May 1, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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