Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gcasadesus committed Oct 18, 2024
1 parent 7e75589 commit c2d1681
Show file tree
Hide file tree
Showing 12 changed files with 337 additions and 216 deletions.
18 changes: 18 additions & 0 deletions examples/cpp/basics/ex_autodiff.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <lupnt/lupnt.h>
using namespace lupnt;

Vec3 func(Vec3 x, bool flag) { return flag ? x : x.array().square(); }

int main() {
Vec3 x(1, 2, 3);
bool flag = false;

Vec3 y;
MatX dydx = jacobian(func, wrt(x), at(x, flag), y);
std::cout << dydx << std::endl;
}

// Output:
// 2 0 0
// 0 4 0
// 0 0 6
15 changes: 15 additions & 0 deletions examples/cpp/basics/ex_time.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <lupnt/lupnt.h>
using namespace lupnt;

void func(Real time, Scheduler &sch) {
std::cout << time << ", "; // Print
auto f = [&sch](Real time) { func(time, sch); }; // Function
sch.ScheduleEvent(Event(time + 1.0 / time, f)); // Reschedule
}

int main() {
Scheduler sch;
auto f = [&sch](Real time) { func(time, sch); };
sch.ScheduleEvent(Event(1.2345, f));
sch.RunSimulation(3.0);
}
155 changes: 0 additions & 155 deletions examples/cpp/contact_plan/example_leader_election_sync_ring.cc

This file was deleted.

4 changes: 2 additions & 2 deletions examples/cpp/ex_load_save.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

using namespace lupnt;
int main() {
H5Easy::File file("ex_load_save.h5", H5Easy::File::Overwrite);
H5Easy::File file("ex_load_save.h5", H5Easy::File::OpenOrCreate);

MatX A = MatX::Random(10, 5);
std::cout << A << std::endl << std::endl;
H5Easy::dump(file, "/path/to/A", A.cast<double>());

MatX B = H5Easy::load<MatXd>(file, "/path/to/A");
MatX B = H5Easy::load<MatXd>(file, "path/to/A");
std::cout << B << std::endl;
}
34 changes: 0 additions & 34 deletions examples/cpp/other/example_scheduler.cc

This file was deleted.

273 changes: 273 additions & 0 deletions examples/python/notebooks/ex_surface.ipynb

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions include/lupnt/agents/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#pragma once

#include <lupnt/core/definitions.h>

#include <memory>

namespace lupnt {
Expand All @@ -19,7 +21,7 @@ namespace lupnt {
virtual ~Application() {};

virtual void Setup() = 0;
virtual void Step(double t) = 0;
virtual double GetFrequency() = 0;
virtual void Step(Real t) = 0;
virtual Real GetFrequency() = 0;
};
}; // namespace lupnt
3 changes: 3 additions & 0 deletions include/lupnt/core/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ namespace lupnt {
using Eigen::VectorX;

using Real = autodiff::real;
using autodiff::at;
using autodiff::jacobian;
using autodiff::wrt;
template <int rows, int cols> using Mat = Matrix<Real, rows, cols>;
template <int rows, int cols> using Matd = Matrix<double, rows, cols>;
template <int size> using Vec = Matrix<Real, size, 1>;
Expand Down
22 changes: 11 additions & 11 deletions include/lupnt/core/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
#include <limits>
#include <memory>

#define INF std::numeric_limits<double>::infinity()
#define SINGLE_EVENT -1

namespace lupnt {
class Event {
public:
double time_;
double frequency_;
double priority_ = 0.0;
std::function<void(double)> action;
Real time_;
Real frequency_;
Real priority_ = 0.0;
std::function<void(Real)> action;

Event(double time, std::function<void(double)> func, double freq = INF)
Event(Real time, std::function<void(Real)> func, Real freq = SINGLE_EVENT)
: time_(time), frequency_(freq), action(func) {}

// Comparator for priority queue
Expand All @@ -33,12 +33,12 @@ namespace lupnt {
return time_ > e.time_;
}

std::function<void(double)> GetAction() const { return action; }
double GetTime() const { return time_; }
double GetFrequency() const { return frequency_; }
std::function<void(Real)> GetAction() const { return action; }
Real GetTime() const { return time_; }
Real GetFrequency() const { return frequency_; }

void SetFrequency(double freq) { frequency_ = freq; }
void SetTime(double time) { time_ = time; }
void SetFrequency(Real freq) { frequency_ = freq; }
void SetTime(Real time) { time_ = time; }
};

}; // namespace lupnt
19 changes: 9 additions & 10 deletions include/lupnt/core/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,35 @@
#include <vector>

#include "lupnt/agents/application.h"
#include "lupnt/core/definitions.h"
#include "lupnt/core/event.h"

namespace lupnt {
class Scheduler {
private:
static std::priority_queue<Event> events;
static double time_;
static Real time_;
// Scheduler() = delete;
// ~Scheduler() = delete;

public:
static void Schedule(double time, std::function<void(double)> func,
double freq = std::numeric_limits<double>::infinity()) {
static void Schedule(Real time, std::function<void(Real)> func, Real freq = SINGLE_EVENT) {
ScheduleEvent(Event(time, func, freq));
}

static void ScheduleEvent(const Event &e) { events.push(e); }

static void ScheduleFunction(std::function<void(double)> func, double time = 0.0,
double freq = std::numeric_limits<double>::infinity()) {
static void ScheduleFunction(std::function<void(Real)> func, Real time = 0.0,
Real freq = SINGLE_EVENT) {
ScheduleEvent(Event(time, func, freq));
}

static void ScheduleApplication(Application &app, double time = 0.0,
double freq = std::numeric_limits<double>::infinity()) {
static void ScheduleApplication(Application &app, Real time = 0.0, Real freq = SINGLE_EVENT) {
app.Setup();
ScheduleEvent(Event(time, [&app](double t) { app.Step(t); }, app.GetFrequency()));
ScheduleEvent(Event(time, [&app](Real t) { app.Step(t); }, app.GetFrequency()));
}

static void RunSimulation(double endTime = std::numeric_limits<double>::infinity()) {
static void RunSimulation(Real endTime = SINGLE_EVENT) {
while (!events.empty() && events.top().GetTime() <= endTime) {
Event e = events.top();
events.pop();
Expand All @@ -51,7 +50,7 @@ namespace lupnt {
}
}

static double GetTime() { return time_; }
static Real GetTime() { return time_; }
};

}; // namespace lupnt
2 changes: 1 addition & 1 deletion source/cpp/core/scheduler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
namespace lupnt {
// Define the static member
std::priority_queue<Event> Scheduler::events;
double Scheduler::time_ = 0.0;
Real Scheduler::time_ = 0.0;

} // namespace lupnt
2 changes: 1 addition & 1 deletion source/python/pylupnt/plot/_mpl.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from PIL import Image

from .. import utils

Expand Down

0 comments on commit c2d1681

Please sign in to comment.