Skip to content

Commit

Permalink
Merge pull request #28 from aetx/clang-format
Browse files Browse the repository at this point in the history
Apply clang format style from Cabana
  • Loading branch information
streeve authored Jul 30, 2021
2 parents fc89254 + 34805d0 commit 819442a
Show file tree
Hide file tree
Showing 15 changed files with 685 additions and 777 deletions.
10 changes: 10 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
BasedOnStyle: LLVM
---
Language: Cpp
AlwaysBreakTemplateDeclarations: true
BreakBeforeBraces: Allman
BinPackParameters: true
IndentWidth: 4
SpacesInParentheses: true
BreakConstructorInitializersBeforeComma: true
PointerAlignment: Left
5 changes: 5 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ jobs:
cmake -B build -DCMAKE_PREFIX_PATH="$HOME/kokkos;$HOME/Cabana"
cmake --build build --parallel 2
cd build && ctest --output-on-failure
- name: Format
working-directory: build
run: |
make format
git diff --exit-code
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,21 @@ if( NOT Cabana_ENABLE_CAJITA )
endif()
find_package(Silo REQUIRED)

# find Clang Format
find_package( CLANG_FORMAT 10 )

# library
add_subdirectory(src)

# examples
add_subdirectory(examples)

##---------------------------------------------------------------------------##
## Clang Format
##---------------------------------------------------------------------------##
if(CLANG_FORMAT_FOUND)
file(GLOB_RECURSE FORMAT_SOURCES src/*.cpp src/*.hpp examples/*.cpp examples/*.hpp)
add_custom_target(format
COMMAND ${CLANG_FORMAT_EXECUTABLE} -i -style=file ${FORMAT_SOURCES}
DEPENDS ${FORMAT_SOURCES})
endif()
63 changes: 63 additions & 0 deletions cmake/FindCLANG_FORMAT.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
############################################################################
# Copyright (c) 2018-2021 by the Cabana authors #
# All rights reserved. #
# #
# This file is part of the Cabana library. Cabana is distributed under a #
# BSD 3-clause license. For the licensing terms see the LICENSE file in #
# the top-level directory. #
# #
# SPDX-License-Identifier: BSD-3-Clause #
############################################################################
#
# Find clang-format
#
# CLANG_FORMAT_EXECUTABLE - Path to clang-format executable
# CLANG_FORMAT_FOUND - True if the clang-format executable was found.
# CLANG_FORMAT_VERSION - The version of clang-format found
#

find_program(CLANG_FORMAT_EXECUTABLE
NAMES clang-format
clang-format-10
clang-format-9
clang-format-8
clang-format-7
clang-format-6.0
clang-format-5.0
clang-format-4.0
clang-format-3.9
clang-format-3.8
clang-format-3.7
clang-format-3.6
clang-format-3.5
clang-format-3.4
clang-format-3.3
DOC "clang-format executable")
mark_as_advanced(CLANG_FORMAT_EXECUTABLE)

# Extract version from command "clang-format -version"
if(CLANG_FORMAT_EXECUTABLE)
execute_process(COMMAND ${CLANG_FORMAT_EXECUTABLE} -version
OUTPUT_VARIABLE clang_format_version
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)

if(clang_format_version MATCHES "^.*clang-format version .*")
# clang_format_version sample: "clang-format version 3.9.1-4ubuntu3~16.04.1
# (tags/RELEASE_391/rc2)"
string(REGEX
REPLACE "^.*clang-format version ([.0-9]+).*"
"\\1"
CLANG_FORMAT_VERSION
"${clang_format_version}")
# CLANG_FORMAT_VERSION sample: "3.9.1"
else()
set(CLANG_FORMAT_VERSION 0.0)
endif()
else()
set(CLANG_FORMAT_VERSION 0.0)
endif()

include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set CLANG_FORMAT_FOUND to TRUE
# if all listed variables are TRUE
find_package_handle_standard_args(CLANG_FORMAT REQUIRED_VARS CLANG_FORMAT_EXECUTABLE VERSION_VAR CLANG_FORMAT_VERSION)
60 changes: 21 additions & 39 deletions examples/dam_break.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <ExaMPM_Solver.hpp>
#include <ExaMPM_BoundaryConditions.hpp>
#include <ExaMPM_Solver.hpp>

#include <Cabana_Core.hpp>

Expand All @@ -20,19 +20,18 @@ struct ParticleInitFunc
double _volume;
double _mass;

ParticleInitFunc( const double cell_size,
const int ppc,
ParticleInitFunc( const double cell_size, const int ppc,
const double density )
: _volume( cell_size * cell_size * cell_size / ppc )
, _mass( _volume * density )
{}
{
}

template<class ParticleType>
KOKKOS_INLINE_FUNCTION
bool operator()( const double x[3], ParticleType& p ) const
template <class ParticleType>
KOKKOS_INLINE_FUNCTION bool operator()( const double x[3],
ParticleType& p ) const
{
if ( 0.0 <= x[0] && x[0] <= 0.4 &&
0.0 <= x[1] && x[1] <= 0.4 &&
if ( 0.0 <= x[0] && x[0] <= 0.4 && 0.0 <= x[1] && x[1] <= 0.4 &&
0.0 <= x[2] && x[2] <= 0.6 )
{
// Affine matrix.
Expand Down Expand Up @@ -65,32 +64,29 @@ struct ParticleInitFunc
};

//---------------------------------------------------------------------------//
void damBreak( const double cell_size,
const int ppc,
const int halo_size,
const double delta_t,
const double t_final,
const int write_freq,
void damBreak( const double cell_size, const int ppc, const int halo_size,
const double delta_t, const double t_final, const int write_freq,
const std::string& device )
{
// The dam break domain is in a box on [0,1] in each dimension.
Kokkos::Array<double,6> global_box = { 0.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
Kokkos::Array<double, 6> global_box = { 0.0, 0.0, 0.0, 1.0, 1.0, 1.0 };

// Compute the number of cells in each direction. The user input must
// squarely divide the domain.
std::array<int,3> global_num_cell = { static_cast<int>(1.0 / cell_size),
static_cast<int>(1.0 / cell_size),
static_cast<int>(1.0 / cell_size) };
std::array<int, 3> global_num_cell = {
static_cast<int>( 1.0 / cell_size ),
static_cast<int>( 1.0 / cell_size ),
static_cast<int>( 1.0 / cell_size ) };

// This will look like a 2D problem so make the Y direction periodic.
std::array<bool,3> periodic = { false, false, false };
std::array<bool, 3> periodic = { false, false, false };

// Due to the 2D nature of the problem we will only partition in Y. The
// behavior of the fluid will be to largely just run out in X and Z with
// little movement in Y.
int comm_size;
MPI_Comm_size( MPI_COMM_WORLD, &comm_size );
std::array<int,3> ranks_per_dim = { 1, comm_size, 1 };
std::array<int, 3> ranks_per_dim = { 1, comm_size, 1 };
Cajita::ManualPartitioner partitioner( ranks_per_dim );

// Material properties.
Expand All @@ -112,23 +108,10 @@ void damBreak( const double cell_size,
bc.boundary[5] = ExaMPM::BoundaryType::FREE_SLIP;

// Solve the problem.
auto solver =
ExaMPM::createSolver( device,
MPI_COMM_WORLD,
global_box,
global_num_cell,
periodic,
partitioner,
halo_size,
ParticleInitFunc(cell_size,ppc,density),
ppc,
bulk_modulus,
density,
gamma,
kappa,
delta_t,
gravity,
bc );
auto solver = ExaMPM::createSolver(
device, MPI_COMM_WORLD, global_box, global_num_cell, periodic,
partitioner, halo_size, ParticleInitFunc( cell_size, ppc, density ),
ppc, bulk_modulus, density, gamma, kappa, delta_t, gravity, bc );
solver->solve( t_final, write_freq );
}

Expand Down Expand Up @@ -168,7 +151,6 @@ int main( int argc, char* argv[] )
MPI_Finalize();

return 0;

}

//---------------------------------------------------------------------------//
61 changes: 22 additions & 39 deletions examples/free_fall.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <ExaMPM_Solver.hpp>
#include <ExaMPM_BoundaryConditions.hpp>
#include <ExaMPM_Solver.hpp>

#include <Cabana_Core.hpp>

Expand All @@ -20,18 +20,18 @@ struct ParticleInitFunc
double _volume;
double _mass;

ParticleInitFunc( const double cell_size,
const int ppc,
ParticleInitFunc( const double cell_size, const int ppc,
const double density )
: _volume( cell_size * cell_size * cell_size / (ppc*ppc*ppc) )
: _volume( cell_size * cell_size * cell_size / ( ppc * ppc * ppc ) )
, _mass( _volume * density )
{}
{
}

template<class ParticleType>
KOKKOS_INLINE_FUNCTION
bool operator()( const double x[3], ParticleType& p ) const
template <class ParticleType>
KOKKOS_INLINE_FUNCTION bool operator()( const double x[3],
ParticleType& p ) const
{
if ( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] < 0.25 * 0.25 )
if ( x[0] * x[0] + x[1] * x[1] + x[2] * x[2] < 0.25 * 0.25 )
{
// Affine matrix.
for ( int d0 = 0; d0 < 3; ++d0 )
Expand Down Expand Up @@ -63,32 +63,29 @@ struct ParticleInitFunc
};

//---------------------------------------------------------------------------//
void freeFall( const double cell_size,
const int ppc,
const int halo_size,
const double delta_t,
const double t_final,
const int write_freq,
void freeFall( const double cell_size, const int ppc, const int halo_size,
const double delta_t, const double t_final, const int write_freq,
const std::string& device )
{
// The dam break domain is in a box on [0,1] in each dimension.
Kokkos::Array<double,6> global_box = { -0.5, -0.5, -0.5, 0.5, 0.5, 0.5 };
Kokkos::Array<double, 6> global_box = { -0.5, -0.5, -0.5, 0.5, 0.5, 0.5 };

// Compute the number of cells in each direction. The user input must
// squarely divide the domain.
std::array<int,3> global_num_cell = { static_cast<int>(1.0 / cell_size),
static_cast<int>(1.0 / cell_size),
static_cast<int>(1.0 / cell_size) };
std::array<int, 3> global_num_cell = {
static_cast<int>( 1.0 / cell_size ),
static_cast<int>( 1.0 / cell_size ),
static_cast<int>( 1.0 / cell_size ) };

// This will look like a 2D problem so make the Y direction periodic.
std::array<bool,3> periodic = { true, true, true };
std::array<bool, 3> periodic = { true, true, true };

// Due to the 2D nature of the problem we will only partition in Y. The
// behavior of the fluid will be to largely just run out in X and Z with
// little movement in Y.
int comm_size;
MPI_Comm_size( MPI_COMM_WORLD, &comm_size );
std::array<int,3> ranks_per_dim = { 1, comm_size, 1 };
std::array<int, 3> ranks_per_dim = { 1, comm_size, 1 };
Cajita::ManualPartitioner partitioner( ranks_per_dim );

// Material properties.
Expand All @@ -110,23 +107,10 @@ void freeFall( const double cell_size,
bc.boundary[5] = ExaMPM::BoundaryType::NONE;

// Solve the problem.
auto solver =
ExaMPM::createSolver( device,
MPI_COMM_WORLD,
global_box,
global_num_cell,
periodic,
partitioner,
halo_size,
ParticleInitFunc(cell_size,ppc,density),
ppc,
bulk_modulus,
density,
gamma,
kappa,
delta_t,
gravity,
bc );
auto solver = ExaMPM::createSolver(
device, MPI_COMM_WORLD, global_box, global_num_cell, periodic,
partitioner, halo_size, ParticleInitFunc( cell_size, ppc, density ),
ppc, bulk_modulus, density, gamma, kappa, delta_t, gravity, bc );
solver->solve( t_final, write_freq );
}

Expand Down Expand Up @@ -166,7 +150,6 @@ int main( int argc, char* argv[] )
MPI_Finalize();

return 0;

}

//---------------------------------------------------------------------------//
10 changes: 5 additions & 5 deletions src/ExaMPM_BoundaryConditions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ struct BoundaryType
struct BoundaryCondition
{
KOKKOS_INLINE_FUNCTION
void operator()( const int gi, const int gj, const int gk,
double& ux, double& uy, double& uz ) const
void operator()( const int gi, const int gj, const int gk, double& ux,
double& uy, double& uz ) const
{
// Low x
if ( gi <= min[0] )
Expand Down Expand Up @@ -125,9 +125,9 @@ struct BoundaryCondition
}
}

Kokkos::Array<int,6> boundary;
Kokkos::Array<int,3> min;
Kokkos::Array<int,3> max;
Kokkos::Array<int, 6> boundary;
Kokkos::Array<int, 3> min;
Kokkos::Array<int, 3> max;
};

//---------------------------------------------------------------------------//
Expand Down
Loading

0 comments on commit 819442a

Please sign in to comment.