Skip to content

Commit

Permalink
added a plotbodies utility
Browse files Browse the repository at this point in the history
  • Loading branch information
sevec committed Oct 4, 2024
1 parent c8e8b79 commit 5bf16f7
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 9 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,5 @@ add_subdirectory(cli/info)

if (BUILD_UTILS)
add_subdirectory(cli/ssftotxt)
add_subdirectory(cli/plotbodies)
endif()
5 changes: 5 additions & 0 deletions cli/plotbodies/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(PLOTBODIES_Sources PlotBodies.cpp)
add_executable(plotbodies ${PLOTBODIES_Sources})

target_include_directories(plotbodies PRIVATE ../../core)
target_link_libraries(plotbodies PRIVATE core)
95 changes: 95 additions & 0 deletions cli/plotbodies/PlotBodies.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/// \brief Tracks body mass over time binary

#include "Sph.h"
#include "io/Table.h"
#include "post/Analysis.h"
#include <fstream>
#include <iostream>

using namespace Sph;

static Array<ArgDesc> params{
{ "f", "filemask", ArgEnum::STRING, "Mask for the input files (i.e. 'collision_%d.ssf')." },
{ "o", "output", ArgEnum::STRING, "Path for the output file (i.e. 'plot_data.txt')." },
{ "n", "number", ArgEnum::INT, "Number of bodies to plot." },
{ "c",
"components",
ArgEnum::BOOL,
"Whether the bodies are components (groups of overlapping particles, for SPH solver), or isolated "
"particles (for hard-sphere solver with merging). Defaults to true." },
};

int main(int argc, char* argv[]) {
try {
ArgParser parser(params);
parser.parse(argc, argv);

Size outputCount = parser.getArg<int>("n");
String outputFile = parser.getArg<String>("o");
String filemask = parser.getArg<String>("f");
bool doComponents = parser.tryGetArg<bool>("c").valueOr(true);
OutputFile mask = OutputFile(Path(filemask));
Statistics stats;
Table table(5);
table.setCell(0, 0, "# Time [s]");
for (Size c = 0; c < outputCount; ++c) {
table.setCell(c + 1, 0, "# Mass " + toString(c + 1) + "[kg]");
}

Size tableRow = 1;
while (true) {
Path path = mask.getNextPath(stats);
if (!FileSystem::pathExists(path)) {
break;
}
AutoPtr<IInput> input = Factory::getInput(path);
std::cout << "Reading file '" << path.string() << "'" << std::endl;
Storage storage;
Outcome result = input->load(path, storage, stats);
if (!result) {
std::cout << "Failed to read the file. " << result.error() << std::endl;
}
Float time = stats.get<Float>(StatisticsId::RUN_TIME);
std::cout << "Analyzing simulation at time t=" << time << " ..." << std::endl;
table.setCell(0, tableRow, toString(time));

if (doComponents) {
ArrayView<const Float> m = storage.getValue<Float>(QuantityId::MASS);
Array<Size> indices;
Size componentCount = Post::findComponents(
storage, 2.f, Post::ComponentFlag::OVERLAP | Post::ComponentFlag::SORT_BY_MASS, indices);

for (Size c = 0; c < outputCount; ++c) {
Float totalMass = 0;
for (Size i = 0; i < indices.size(); ++i) {
if (indices[i] == c) {
totalMass += m[i];
}
}
std::cout << "Body " << c << " has mass " << totalMass << std::endl;
table.setCell(c + 1, tableRow, toString(totalMass));
}
} else {
Array<Float> m = storage.getValue<Float>(QuantityId::MASS).clone();
std::sort(m.begin(), m.end(), std::greater<Float>());

for (Size c = 0; c < outputCount; ++c) {
std::cout << "Particle " << c << " has mass " << m[c] << std::endl;
table.setCell(c + 1, tableRow, toString(m[c]));
}
}
tableRow++;
}

std::cout << "Writing to " << outputFile << std::endl;
std::ofstream ofs(outputFile.toWstring());

ofs << table.toString();

} catch (const std::exception& e) {
std::cout << "Cannot run program. " << e.what() << std::endl;
}


return 0;
}
34 changes: 25 additions & 9 deletions core/system/ArgsParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ ArgParser::ArgParser(ArrayView<const ArgDesc> args) {
} });
}

template <typename TValue>
void ArgParser::insertArg(const String& name, const String& textValue) {
const Optional<TValue> value = fromString<TValue>(textValue);
if (!value) {
throw ArgError("Cannot parse value of parameter " + name);
}
params.insert(name, value.value());
}

template <>
void ArgParser::insertArg<bool>(const String& name, const String& textValue) {
if (textValue == "true") {
params.insert(name, true);
} else if (textValue == "false") {
params.insert(name, false);
} else {
throw ArgError("Cannot parse value of parameter " + name);
}
}

void ArgParser::parse(const int argc, char** argv) {
params.clear();
for (int i = 1; i < argc; ++i) {
Expand Down Expand Up @@ -42,6 +62,8 @@ INLINE String toString(const ArgEnum type) {
return "FLOAT";
case ArgEnum::STRING:
return "STRING";
case ArgEnum::BOOL:
return "BOOL";
default:
NOT_IMPLEMENTED;
}
Expand Down Expand Up @@ -89,6 +111,9 @@ void ArgParser::parseValueArg(const ArgDesc& desc, const String& value) {
case ArgEnum::STRING:
insertArg<String>(desc.shortName, value);
break;
case ArgEnum::BOOL:
insertArg<bool>(desc.shortName, value);
break;
default:
STOP;
}
Expand All @@ -98,15 +123,6 @@ void ArgParser::parseValueArg(const ArgDesc& desc, const String& value) {
}
}

template <typename TValue>
void ArgParser::insertArg(const String& name, const String& textValue) {
const Optional<TValue> value = fromString<TValue>(textValue);
if (!value) {
throw ArgError("Cannot parse value of parameter " + name);
}
params.insert(name, value.value());
}

void ArgParser::throwIfUnknownArg(const String& name) const {
auto iter = std::find_if(descs.begin(), descs.end(), [&name](const ArgDesc& desc) { //
return desc.shortName == name;
Expand Down

0 comments on commit 5bf16f7

Please sign in to comment.