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

[WIP] two more pedagogical examples #1166

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ add_subdirectory(particle_tracers)
add_subdirectory(poisson)
add_subdirectory(poisson_gmg)
add_subdirectory(sparse_advection)
add_subdirectory(ellipse/src ellipse)
add_subdirectory(monte_carlo_pi/src monte_carlo_pi)
59 changes: 59 additions & 0 deletions example/ellipse/inputs/parthinput.ellipse
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# ========================================================================================
# (C) (or copyright) 2024. Triad National Security, LLC. All rights reserved.
#
# This program was produced under U.S. Government contract 89233218CNA000001 for Los
# Alamos National Laboratory (LANL), which is operated by Triad National Security, LLC
# for the U.S. Department of Energy/National Nuclear Security Administration. All rights
# in the program are reserved by Triad National Security, LLC, and the U.S. Department
# of Energy/National Nuclear Security Administration. The Government is granted for
# itself and others acting on its behalf a nonexclusive, paid-up, irrevocable worldwide
# license in this material to reproduce, prepare derivative works, distribute copies to
# the public, perform publicly and display publicly, and to permit others to do so.
# ========================================================================================

<parthenon/job>
problem_id = ellipse

<parthenon/mesh>
refinement = adaptive
numlevel = 3

nx1 = 64
x1min = -2.0
x1max = 2.0
ix1_bc = outflow
ox1_bc = outflow

nx2 = 64
x2min = -2.0
x2max = 2.0
ix2_bc = outflow
ox2_bc = outflow

nx3 = 1
x3min = -0.5
x3max = 0.5

# How many meshblocks to use in a premade default kernel.
# A value of <1 means use the whole mesh.
pack_size = 1

<parthenon/meshblock>
nx1 = 8
nx2 = 8
nx3 = 1

<parthenon/refinement0>
field = phi # the name of the variable we want to refine on
method = derivative_order_1 # selects the first derivative method
refine_tol = 0.5 # tag for refinement if |(dfield/dx)/field| > refine_tol
derefine_tol = 0.05 # tag for derefinement if |(dfield/dx)/field| < derefine_tol
max_level = 3 # if set, limits refinement level from this criteria to no greater than max_level

<ellipse>
major_axis = 1.5
minor_axis = 1.0

<parthenon/output0>
file_type = hdf5
variables = phi
43 changes: 43 additions & 0 deletions example/ellipse/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#=========================================================================================
# (C) (or copyright) 2024. Triad National Security, LLC. All rights reserved.
#
# This program was produced under U.S. Government contract 89233218CNA000001 for Los
# Alamos National Laboratory (LANL), which is operated by Triad National Security, LLC
# for the U.S. Department of Energy/National Nuclear Security Administration. All rights
# in the program are reserved by Triad National Security, LLC, and the U.S. Department
# of Energy/National Nuclear Security Administration. The Government is granted for
# itself and others acting on its behalf a nonexclusive, paid-up, irrevocable worldwide
# license in this material to reproduce, prepare derivative works, distribute copies to
# the public, perform publicly and display publicly, and to permit others to do so.
#=========================================================================================

# There is no test but we build it if we build examples
if (NOT PARTHENON_DISABLE_EXAMPLES)
set (SRC_LIST
main.cpp

driver.hpp

ellipse/ellipse.cpp
ellipse/ellipse.hpp

indicator/indicator.cpp
indicator/indicator.hpp

pgen.cpp
pgen.hpp
)

add_executable(ellipse ${SRC_LIST})
target_include_directories(ellipse PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/generated>
)
target_compile_features(ellipse PRIVATE cxx_std_17)

if (Kokkos_ENABLE_CUDA)
target_compile_options(ellipse --expt-relaxed-constexpr)
endif()

target_link_libraries(ellipse PRIVATE parthenon)
endif()
31 changes: 31 additions & 0 deletions example/ellipse/src/driver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//========================================================================================
// (C) (or copyright) 2024. Triad National Security, LLC. All rights reserved.
//
// This program was produced under U.S. Government contract 89233218CNA000001 for Los
// Alamos National Laboratory (LANL), which is operated by Triad National Security, LLC
// for the U.S. Department of Energy/National Nuclear Security Administration. All rights
// in the program are reserved by Triad National Security, LLC, and the U.S. Department
// of Energy/National Nuclear Security Administration. The Government is granted for
// itself and others acting on its behalf a nonexclusive, paid-up, irrevocable worldwide
// license in this material to reproduce, prepare derivative works, distribute copies to
// the public, perform publicly and display publicly, and to permit others to do so.
//========================================================================================

#ifndef _DRIVER_HPP_
#define _DRIVER_HPP_

#include <parthenon/driver.hpp>

class ToyDriver : parthenon::Driver {
public:
ToyDriver(ParameterInput *pin, ApplicationInput *app_in, Mesh *pm)
: parthenon::Driver(pin, app_in, pm) {
InitializeOutputs();
}
parthenon::DriverStatus Execute() override {
pouts->MakeOutputs(pmesh, pinput);
return parthenon::DriverStatus::complete;
}
};

#endif // _DRIVER_HPP_
35 changes: 35 additions & 0 deletions example/ellipse/src/ellipse/ellipse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//========================================================================================
// (C) (or copyright) 2024. Triad National Security, LLC. All rights reserved.
//
// This program was produced under U.S. Government contract 89233218CNA000001 for Los
// Alamos National Laboratory (LANL), which is operated by Triad National Security, LLC
// for the U.S. Department of Energy/National Nuclear Security Administration. All rights
// in the program are reserved by Triad National Security, LLC, and the U.S. Department
// of Energy/National Nuclear Security Administration. The Government is granted for
// itself and others acting on its behalf a nonexclusive, paid-up, irrevocable worldwide
// license in this material to reproduce, prepare derivative works, distribute copies to
// the public, perform publicly and display publicly, and to permit others to do so.
//========================================================================================

#include <memory>

#include <parthenon/package.hpp>
using namespace parthenon::package::prelude;

#include "ellipse.hpp"

namespace Ellipse {

std::shared_ptr<StateDescriptor> Initialize(ParameterInput *pin) {
auto pkg = std::make_shared<StateDescriptor>("ellipse");

// parse input deck and add params for ellipse shape.
const Real major_axis = pin->GetOrAddReal("ellipse", "major_axis", 1.0);
const Real minor_axis = pin->GetOrAddReal("ellipse", "minor_axis", 1.0);
pkg->AddParam("major_axis", major_axis);
pkg->AddParam("minor_axis", minor_axis);

return pkg;
}

} // namespace Ellipse
27 changes: 27 additions & 0 deletions example/ellipse/src/ellipse/ellipse.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//========================================================================================
// (C) (or copyright) 2024. Triad National Security, LLC. All rights reserved.
//
// This program was produced under U.S. Government contract 89233218CNA000001 for Los
// Alamos National Laboratory (LANL), which is operated by Triad National Security, LLC
// for the U.S. Department of Energy/National Nuclear Security Administration. All rights
// in the program are reserved by Triad National Security, LLC, and the U.S. Department
// of Energy/National Nuclear Security Administration. The Government is granted for
// itself and others acting on its behalf a nonexclusive, paid-up, irrevocable worldwide
// license in this material to reproduce, prepare derivative works, distribute copies to
// the public, perform publicly and display publicly, and to permit others to do so.
//========================================================================================

#ifndef _ELLIPSE_ELLIPSE_HPP_
#define _ELLIPSE_ELLIPSE_HPP_

#include <memory>
#include <parthenon/package.hpp>

namespace Ellipse {
using namespace parthenon::package::prelude;

std::shared_ptr<StateDescriptor> Initialize(ParameterInput *pin);

} // namespace Ellipse

#endif // _ELLIPSE_ELLIPSE_HPP_
33 changes: 33 additions & 0 deletions example/ellipse/src/indicator/indicator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//========================================================================================
// (C) (or copyright) 2024. Triad National Security, LLC. All rights reserved.
//
// This program was produced under U.S. Government contract 89233218CNA000001 for Los
// Alamos National Laboratory (LANL), which is operated by Triad National Security, LLC
// for the U.S. Department of Energy/National Nuclear Security Administration. All rights
// in the program are reserved by Triad National Security, LLC, and the U.S. Department
// of Energy/National Nuclear Security Administration. The Government is granted for
// itself and others acting on its behalf a nonexclusive, paid-up, irrevocable worldwide
// license in this material to reproduce, prepare derivative works, distribute copies to
// the public, perform publicly and display publicly, and to permit others to do so.
//========================================================================================

#include <memory>

#include <parthenon/package.hpp>
using namespace parthenon::package::prelude;

#include "indicator.hpp"

namespace Indicator {

std::shared_ptr<StateDescriptor> Initialize(ParameterInput *pin) {
auto pkg = std::make_shared<StateDescriptor>("indicator");

// register the phi variable
Metadata m({Metadata::OneCopy, Metadata::Cell});
pkg->AddField<phi>(m);

return pkg;
}

} // namespace Indicator
34 changes: 34 additions & 0 deletions example/ellipse/src/indicator/indicator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//========================================================================================
// (C) (or copyright) 2024. Triad National Security, LLC. All rights reserved.
//
// This program was produced under U.S. Government contract 89233218CNA000001 for Los
// Alamos National Laboratory (LANL), which is operated by Triad National Security, LLC
// for the U.S. Department of Energy/National Nuclear Security Administration. All rights
// in the program are reserved by Triad National Security, LLC, and the U.S. Department
// of Energy/National Nuclear Security Administration. The Government is granted for
// itself and others acting on its behalf a nonexclusive, paid-up, irrevocable worldwide
// license in this material to reproduce, prepare derivative works, distribute copies to
// the public, perform publicly and display publicly, and to permit others to do so.
//========================================================================================

#ifndef _INDICATOR_INDICATOR_HPP_
#define _INDICATOR_INDICATOR_HPP_

#include <memory>
#include <parthenon/package.hpp>

namespace Indicator {
using namespace parthenon::package::prelude;

struct phi : public parthenon::variable_names::base_t<false> {
template <class... Ts>
KOKKOS_INLINE_FUNCTION phi(Ts &&...args)
: parthenon::variable_names::base_t<false>(std::forward<Ts>(args)...) {}
static std::string name() { return "phi"; }
};

std::shared_ptr<StateDescriptor> Initialize(ParameterInput *pin);

} // namespace Indicator

#endif // _INDICATOR_INDICATOR_HPP_
65 changes: 65 additions & 0 deletions example/ellipse/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//========================================================================================
// (C) (or copyright) 2024. Triad National Security, LLC. All rights reserved.
//
// This program was produced under U.S. Government contract 89233218CNA000001 for Los
// Alamos National Laboratory (LANL), which is operated by Triad National Security, LLC
// for the U.S. Department of Energy/National Nuclear Security Administration. All rights
// in the program are reserved by Triad National Security, LLC, and the U.S. Department
// of Energy/National Nuclear Security Administration. The Government is granted for
// itself and others acting on its behalf a nonexclusive, paid-up, irrevocable worldwide
// license in this material to reproduce, prepare derivative works, distribute copies to
// the public, perform publicly and display publicly, and to permit others to do so.
//========================================================================================

#include <parthenon/driver.hpp>
#include <parthenon_manager.hpp>
using namespace parthenon::driver::prelude;

#include "driver.hpp"
#include "ellipse/ellipse.hpp"
#include "indicator/indicator.hpp"
#include "pgen.hpp"

int main(int argc, char *argv[]) {
parthenon::ParthenonManager pman;

// Set up kokkos and read pin
auto manager_status = pman.ParthenonInitEnv(argc, argv);
if (manager_status == ParthenonStatus::complete) {
pman.ParthenonFinalize();
return 0;
}
if (manager_status == ParthenonStatus::error) {
pman.ParthenonFinalize();
return 1;
}

// Redefine parthenon defaults
pman.app_input->ProcessPackages = [](std::unique_ptr<ParameterInput> &pin) {
Packages_t packages;
packages.Add(Indicator::Initialize(pin.get()));
packages.Add(Ellipse::Initialize(pin.get()));
return packages;
};
pman.app_input->ProblemGenerator = SetupEllipse;

// call ParthenonInit to set up the mesh
// scope so that the mesh object, kokkos views, etc, all get cleaned
// up before kokkos::finalize
pman.ParthenonInitPackagesAndMesh();
{

// Initialize the driver
ToyDriver driver(pman.pinput.get(), pman.app_input.get(), pman.pmesh.get());

// This line actually runs the simulation
auto driver_status = driver.Execute(); // unneeded here

// call MPI_Finalize and Kokkos::finalize if necessary
}
pman.ParthenonFinalize();

// MPI and Kokkos can no longer be used

return (0);
}
Loading
Loading