Skip to content

Commit

Permalink
add properties export and create restart
Browse files Browse the repository at this point in the history
  • Loading branch information
gouarin committed Jun 24, 2024
1 parent 956f8ec commit aaf13ef
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 17 deletions.
45 changes: 45 additions & 0 deletions include/scopi/json.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include <nlohmann/json.hpp>

#include <xtensor/xjson.hpp>
#include <xtensor/xtensor.hpp>

#include "property.hpp"

namespace nl = nlohmann;

namespace nlohmann
{
template <std::size_t dim>
struct adl_serializer<scopi::property<dim>>
{
using xt_t = xt::xtensor<double, 1>;
using xt_t2 = xt::xtensor<double, 2>;
using moment_t = typename std::conditional<dim == 2, double, xt_t>::type;
using rotation_t = typename std::conditional<dim == 2, double, xt_t>::type;

static void to_json(json&, const scopi::property<dim>&)
{
}

static void from_json(const json& j, scopi::property<dim>& p)
{
p.velocity(j["velocity"].get<xt_t>())
.desired_velocity(j["desired_velocity"].get<xt_t>())
.omega(j["omega"].get<rotation_t>())
.desired_omega(j["desired_omega"].get<rotation_t>())
.force(j["force"].get<xt_t>())
.mass(j["mass"].get<double>())
.moment_inertia(j["moment_inertia"].get<moment_t>());
if (j["active"].get<bool>())
{
p.activate();
}
else
{
p.deactivate();
}
}
};
}
19 changes: 10 additions & 9 deletions include/scopi/objects/methods/write_objects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <xtensor-blas/xlinalg.hpp>
#include <xtensor/xfixed.hpp>
#include <xtensor/xio.hpp>
#include <xtensor/xjson.hpp>

#include "../dispatch.hpp"
#include "../neighbor.hpp"
Expand Down Expand Up @@ -43,10 +44,10 @@ namespace scopi

object["type"] = "sphere";
object["id"] = id;
object["position"] = s.pos();
object["position"] = xt::flatten(s.pos());
object["radius"] = s.radius();
object["rotation"] = xt::flatten(s.rotation());
object["quaternion"] = s.q();
object["quaternion"] = xt::flatten(s.q());

return object;
// std::cout << "write_objects : SPHERE" << std::endl;
Expand Down Expand Up @@ -80,11 +81,11 @@ namespace scopi

object["type"] = "superellipsoid";
object["id"] = id;
object["position"] = s.pos();
object["position"] = xt::flatten(s.pos());
object["radius"] = s.radius();
object["squareness"] = s.squareness();
object["rotation"] = xt::flatten(s.rotation());
object["quaternion"] = s.q();
object["quaternion"] = xt::flatten(s.q());

return object;

Expand Down Expand Up @@ -121,10 +122,10 @@ namespace scopi

object["type"] = "plane";
object["id"] = id;
object["position"] = p.pos();
object["position"] = xt::flatten(p.pos());
object["normal"] = p.normal();
object["rotation"] = xt::flatten(p.rotation());
object["quaternion"] = p.q();
object["quaternion"] = xt::flatten(p.q());

return object;

Expand Down Expand Up @@ -165,7 +166,7 @@ namespace scopi
object["p2"] = extrema[1];
object["normal"] = seg.normal();
object["tangent"] = seg.tangent();
object["quaternion"] = seg.q();
object["quaternion"] = xt::flatten(seg.q());

return object;
}
Expand All @@ -187,10 +188,10 @@ namespace scopi
for (std::size_t i = 0; i < w.size(); ++i)
{
nl::json json_worm;
json_worm["position"] = w.pos(i);
json_worm["position"] = xt::flatten(w.pos(i));
json_worm["id"] = id + i;
json_worm["radius"] = w.radius();
json_worm["quaternion"] = w.q(i);
json_worm["quaternion"] = xt::flatten(w.q(i));
object["worm"].push_back(json_worm);
}
return object;
Expand Down
81 changes: 81 additions & 0 deletions include/scopi/restart.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#pragma once

#include <nlohmann/json.hpp>
#include <xtensor/xjson.hpp>

#include "container.hpp"
#include "json.hpp"
#include "objects/types/plane.hpp"
#include "objects/types/segment.hpp"
#include "objects/types/sphere.hpp"
#include "objects/types/superellipsoid.hpp"
#include "objects/types/worm.hpp"
#include "property.hpp"

namespace nl = nlohmann;

namespace scopi
{

template <std::size_t dim>
auto from_json(const nl::json& j)
{
using xt_t = xt::xtensor<double, 1>;
scopi_container<dim> container;

for (const auto& o : j["objects"])
{
std::string type = o["type"];

if (type == "sphere")
{
sphere<dim> s({o["position"].get<xt_t>()}, {o["quaternion"].get<xt_t>()}, o["radius"].get<double>());
container.push_back(s, o["properties"]);
}
else if (type == "superellipsoid")
{
superellipsoid<dim> s({o["position"].get<xt_t>()},
{o["quaternion"].get<xt_t>()},
o["radius"].get<xt_t>(),
o["squareness"].get<xt_t>());
container.push_back(s, o["properties"]);
}
else if (type == "segment")
{
segment<dim> s(o["p1"].get<xt_t>(), o["p2"].get<xt_t>());
container.push_back(s, o["properties"]);
}
else if (type == "plane")
{
plane<dim> s({o["position"].get<xt_t>()}, {o["quaternion"].get<xt_t>()});
container.push_back(s, o["properties"]);
}
else if (type == "worm")
{
std::vector<type::position_t<dim>> pos;
std::vector<type::quaternion_t> q;
for (auto& p : o["worm"])
{
pos.push_back(p["position"].get<xt_t>());
q.push_back(p["quaternion"].get<xt_t>());
}
worm<dim> w(pos, q, o["worm"][0]["radius"], pos.size());
container.push_back(w, o["properties"]);
}
}
return container;
}

// template <std::size_t dim, class Contact>
// auto contact_from_json(const nl::json& j)
// {
// using xt_t = xt::xtensor<double, 1>;
// std::vector<std::pair<std::size_t, std::size_t>> contacts;

// for (const auto& c : j["contacts"])
// {
// contacts.push_back({c["id1"], c["id2"]});
// }
// return contacts;
// }
}
26 changes: 18 additions & 8 deletions include/scopi/solver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,16 +383,26 @@ namespace scopi

for (std::size_t i = 0; i < m_particles.size(); ++i)
{
json_output["objects"].push_back(write_objects_dispatcher<dim>::dispatch(*m_particles[i], i));
}

if (m_params.write_velocity)
{
for (std::size_t i = 0; i < m_particles.size(); ++i)
auto offset = m_particles.offset(i);
nl::json object = write_objects_dispatcher<dim>::dispatch(*m_particles[i], offset);
nl::json& prop = object["properties"];
prop["velocity"] = m_particles.v()(offset);
prop["desired_velocity"] = m_particles.vd()(offset);
prop["omega"] = m_particles.omega()(offset);
prop["desired_omega"] = m_particles.desired_omega()(offset);
prop["force"] = m_particles.f()(offset);
prop["mass"] = m_particles.m()(offset);
prop["moment_inertia"] = m_particles.j()(offset);
if (offset < m_particles.nb_inactive())
{
json_output["objects"][i]["velocity"] = m_particles.v()(i);
json_output["objects"][i]["rotationvelocity"] = m_particles.omega()(i);
prop["active"] = false;
}
else
{
prop["active"] = true;
}

json_output["objects"].push_back(object);
}

json_output["contacts"] = {};
Expand Down

0 comments on commit aaf13ef

Please sign in to comment.