Skip to content

Commit

Permalink
Merge pull request #22 from pshriwise/tools-n-args
Browse files Browse the repository at this point in the history
More tools w/ arguments
  • Loading branch information
pshriwise authored Mar 7, 2024
2 parents e736319 + c22aa02 commit 9ec63ce
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
[submodule "fmt"]
path = vendor/fmt
url = https://github.com/fmtlib/fmt.git
[submodule "vendor/argparse"]
path = vendor/argparse
url = https://github.com/p-ranav/argparse.git
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ if (NOT fmt_FOUND)
add_subdirectory(vendor/fmt)
endif()

# argparse
add_subdirectory(vendor/argparse)

list(APPEND xdg_sources
src/geometry/measure.cpp
src/geometry/plucker.cpp
Expand Down
9 changes: 7 additions & 2 deletions include/xdg/vec3da.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
#ifndef _XDG_VEC3DA_H
#define _XDG_VEC3DA_H

#include <array>
#include <assert.h>
#include <iostream>
#include <math.h>
#include <immintrin.h>
#include <xmmintrin.h>
#include <limits>
#include <vector>

#ifndef NDEBUG
#define __forceinline inline
#else
#define __forceinline inline __attribute__((always_inline))
#endif

#include <array>

#include "xdg/constants.h"

namespace xdg {
Expand All @@ -30,6 +30,11 @@ struct Vec3da {
struct{ double x,y,z; size_t a;};
};

__forceinline Vec3da(std::vector<double> const& vec) : x(vec[0]), y(vec[1]), z(vec[2]), a(0) {
if (vec.size() != 3)
throw std::runtime_error("Vec3da constructor from std::vector<double> requires a vector of size 3");
}

__forceinline Vec3da(std::array<double, 3> const& arr) : x(arr[0]), y(arr[1]), z(arr[2]), a(0) { }

__forceinline Vec3da () {}
Expand Down
15 changes: 13 additions & 2 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@


add_executable(particle-sim particle_sim.cpp)
target_link_libraries(particle-sim xdg)

set(TOOL_NAMES
particle_sim
ray_fire
find_volume
point_in_volume
)

foreach(tool ${TOOL_NAMES})
string(REPLACE "_" "-" tool_exec ${tool})
add_executable(${tool_exec} ${tool}.cpp)
target_link_libraries(${tool_exec} xdg argparse)
endforeach()
73 changes: 73 additions & 0 deletions tools/find_volume.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <iostream>
#include <memory>
#include <string>
#include <vector>

#include "xdg/error.h"
#include "xdg/mesh_manager_interface.h"
#include "xdg/moab/mesh_manager.h"
#include "xdg/vec3da.h"
#include "xdg/xdg.h"

#include "argparse/argparse.hpp"

using namespace xdg;

int main(int argc, char** argv) {

argparse::ArgumentParser args("XDG Find Volume Tool", "1.0", argparse::default_arguments::help);

args.add_argument("filename")
.help("Path to the input file");

args.add_argument("-l", "--list")
.default_value(false)
.implicit_value(true)
.help("List all volumes in the file and exit");

args.add_argument("-p", "--position")
.default_value(std::vector<double>{0.0, 0.0, 0.0})
.help("Ray origin").scan<'g', double>().nargs(3);

args.add_argument("-d", "--direction")
.default_value(std::vector<double>{0.0, 0.0, 1.0})
.help("Ray direction").scan<'g', double>().nargs(3);

try {
args.parse_args(argc, argv);
}
catch (const std::runtime_error& err) {
std::cout << err.what() << std::endl;
std::cout << args;
exit(0);
}

// create a mesh manager
std::shared_ptr<XDG> xdg = XDG::create(MeshLibrary::MOAB);
const auto& mm = xdg->mesh_manager();
mm->load_file(args.get<std::string>("filename"));
mm->init();
mm->parse_metadata();
xdg->prepare_raytracer();

if (args.get<bool>("--list")) {
std::cout << "Volumes: " << std::endl;
for (auto volume : mm->volumes()) {
std::cout << volume << std::endl;
}
exit(0);
}

Position position = args.get<std::vector<double>>("--position");
Direction direction = args.get<std::vector<double>>("--direction");

MeshID volume = xdg->find_volume(position, direction);

if (volume == ID_NONE) {
std::cout << "No volume found for position " << position << std::endl;
} else {
std::cout << "Point " << position << " is in Volume " << volume << std::endl;
}

return 0;
}
42 changes: 34 additions & 8 deletions tools/particle_sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
#include "xdg/vec3da.h"
#include "xdg/xdg.h"

#include "argparse/argparse.hpp"

using namespace xdg;

static double mfp {1.0};
static double MFP {1.0};

struct Particle {

Expand Down Expand Up @@ -47,7 +49,7 @@ void surf_dist() {
}

void sample_collision_distance() {
collision_distance_ = -std::log(1.0 - drand48()) / mfp;
collision_distance_ = -std::log(1.0 - drand48()) * MFP;
}

void collide() {
Expand Down Expand Up @@ -122,31 +124,55 @@ bool alive_ {true};

int main(int argc, char** argv) {

// argument parsing
argparse::ArgumentParser args("XDG Particle Pseudo-Simulation", "1.0", argparse::default_arguments::help);

args.add_argument("filename")
.help("Path to the input file");

args.add_argument("-v", "--verbose")
.default_value(false)
.implicit_value(true)
.help("Enable verbose output of particle events");

args.add_argument("-m", "--mfp")
.default_value(MFP)
.help("Mean free path of the particles").scan<'g', double>();

try {
args.parse_args(argc, argv);
}
catch (const std::runtime_error& err) {
std::cout << err.what() << std::endl;
std::cout << args;
exit(0);
}

// Problem Setup
srand48(42);

// create a mesh manager
std::shared_ptr<XDG> xdg = XDG::create(MeshLibrary::MOAB);
const auto& mm = xdg->mesh_manager();

std::string filename {argv[1]};

mm->load_file(filename);
mm->load_file(args.get<std::string>("filename"));
mm->init();
mm->parse_metadata();
xdg->prepare_raytracer();

// create a new particle
// update the mean free path
MFP = args.get<double>("--mfp");

const int n_particles {100};

const int max_events {1000};

bool verbose = false;
bool verbose_particles = args.get<bool>("--verbose");

for (int i = 0; i < n_particles; i++) {
int particle_id = i+1;
write_message("Starting particle {}", particle_id);
Particle p(xdg, particle_id, verbose);
Particle p(xdg, particle_id, verbose_particles);
p.initialize();
while (true) {
p.surf_dist();
Expand Down
75 changes: 75 additions & 0 deletions tools/point_in_volume.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <iostream>
#include <memory>
#include <string>
#include <vector>

#include "xdg/error.h"
#include "xdg/mesh_manager_interface.h"
#include "xdg/moab/mesh_manager.h"
#include "xdg/vec3da.h"
#include "xdg/xdg.h"

#include "argparse/argparse.hpp"

using namespace xdg;

int main(int argc, char** argv) {

argparse::ArgumentParser args("XDG Point in Volume Tool", "1.0", argparse::default_arguments::help);

args.add_argument("filename")
.help("Path to the input file");

args.add_argument("volume")
.help("Volume ID to query").scan<'i', int>();

args.add_argument("-l", "--list")
.default_value(false)
.implicit_value(true)
.help("List all volumes in the file and exit");

args.add_argument("-p", "--position")
.default_value(std::vector<double>{0.0, 0.0, 0.0})
.help("Ray origin").scan<'g', double>().nargs(3);

args.add_argument("-d", "--direction")
.default_value(std::vector<double>{0.0, 0.0, 1.0})
.help("Ray direction").scan<'g', double>().nargs(3);

try {
args.parse_args(argc, argv);
}
catch (const std::runtime_error& err) {
std::cout << err.what() << std::endl;
std::cout << args;
exit(0);
}

// create a mesh manager
std::shared_ptr<XDG> xdg = XDG::create(MeshLibrary::MOAB);
const auto& mm = xdg->mesh_manager();
mm->load_file(args.get<std::string>("filename"));
mm->init();
mm->parse_metadata();
xdg->prepare_raytracer();

if (args.get<bool>("--list")) {
std::cout << "Volumes: " << std::endl;
for (auto volume : mm->volumes()) {
std::cout << volume << std::endl;
}
exit(0);
}

MeshID volume = args.get<int>("volume");
Position position = args.get<std::vector<double>>("--position");
Direction direction = args.get<std::vector<double>>("--direction");

if (xdg->point_in_volume(volume, position, &direction)) {
std::cout << "Point " << position << " is in Volume " << volume << " (True)" << std::endl;
} else {
std::cout << "Point " << position << " is not in Volume " << volume << " (False)" << std::endl;
}

return 0;
}
75 changes: 75 additions & 0 deletions tools/ray_fire.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <iostream>
#include <memory>
#include <string>
#include <vector>

#include "xdg/error.h"
#include "xdg/mesh_manager_interface.h"
#include "xdg/moab/mesh_manager.h"
#include "xdg/vec3da.h"
#include "xdg/xdg.h"

#include "argparse/argparse.hpp"

using namespace xdg;

int main(int argc, char** argv) {

argparse::ArgumentParser args("XDG Ray Fire Tool", "1.0", argparse::default_arguments::help);

args.add_argument("filename")
.help("Path to the input file");

args.add_argument("volume")
.help("Volume ID to query").scan<'i', int>();

args.add_argument("-l", "--list")
.default_value(false)
.implicit_value(true)
.help("List all volumes in the file and exit");

args.add_argument("-o", "-p", "--origin", "--position")
.default_value(std::vector<double>{0.0, 0.0, 0.0})
.help("Ray origin/position").scan<'g', double>().nargs(3);

args.add_argument("-d", "--direction")
.default_value(std::vector<double>{0.0, 0.0, 1.0})
.help("Ray direction").scan<'g', double>().nargs(3);

try {
args.parse_args(argc, argv);
}
catch (const std::runtime_error& err) {
std::cout << err.what() << std::endl;
std::cout << args;
exit(0);
}


// create a mesh manager
std::shared_ptr<XDG> xdg = XDG::create(MeshLibrary::MOAB);
const auto& mm = xdg->mesh_manager();
mm->load_file(args.get<std::string>("filename"));
mm->init();
mm->parse_metadata();
xdg->prepare_raytracer();

if (args.get<bool>("--list")) {
std::cout << "Volumes: " << std::endl;
for (auto volume : mm->volumes()) {
std::cout << volume << std::endl;
}
exit(0);
}

MeshID volume = args.get<int>("volume");
Position origin = args.get<std::vector<double>>("--origin");
Direction direction = args.get<std::vector<double>>("--direction");

auto result = xdg->ray_fire(volume, origin, direction);

std::cout << "Distance: " << result.first << std::endl;
std::cout << "Surface: " << result.second << std::endl;

return 0;
}
1 change: 1 addition & 0 deletions vendor/argparse
Submodule argparse added at 1b3abd

0 comments on commit 9ec63ce

Please sign in to comment.