Commit a251cc92 authored by Pries, Jason's avatar Pries, Jason

Begin implementation of Switched Winding Synchrel PSO application

parent 0e90749e
......@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.2)
project(Oersted)
find_package(MPI REQUIRED)
find_package(OpenMP)
find_package(OpenMP REQUIRED)
include_directories(${MPI_INCLUDE_PATH})
include_directories(./lib/)
......@@ -31,4 +31,6 @@ include_directories(./src/Model\ Templates/include/)
add_subdirectory(./src/)
add_subdirectory(./test/)
\ No newline at end of file
add_subdirectory(./test/)
add_subdirectory(./apps/)
\ No newline at end of file
project(Oersted_Apps)
set(OERSTED_LIBRARIES
geometry
sketch
elements
mesh
materials
optimization
quadrature
physics
model_templates
stdc++fs
${MPI_C_LIBRARIES}
)
add_subdirectory(./Switched\ Winding\ Synchrel)
\ No newline at end of file
PROJECT(Switched\ Winding\ Synchrel)
set(SOURCE_FILES
main.cpp
designspace.h designspace.cpp
globalobjective.h globalobjective.cpp)
add_executable(sws ${SOURCE_FILES})
target_link_libraries(sws ${OERSTED_LIBRARIES})
\ No newline at end of file
#include "designspace.h"
CoordinateSpace design_space() {
CoordinateSpace d;
double_t inch{25.4e-3};
d["ls"] = Coordinate{1*inch,3.5*inch,6*inch};
d["rro"] = Coordinate{2*inch,3.25*inch,4.5*inch};
for (size_t i = 0; i != 6; ++i) {
std::string key = "at" + std::to_string(i);
d[key] = Coordinate{0.07,0.14,0.21};
}
for (size_t i = 0; i != 7; ++i) {
std::string key = "rt" + std::to_string(i);
d[key] = Coordinate{0.07,0.14,0.21};
}
d["sw"] = Coordinate{0.2,0.4,0.6};
d["bi"] = Coordinate{0.5,1.0,1.5};
d["sd"] = Coordinate{2,6,14};
return d;
}
\ No newline at end of file
#ifndef OE_APP_SWITCHED_WINDING_SYNCHREL_DESIGNSPACE_H
#define OE_APP_SWITCHED_WINDING_SYNCHREL_DESIGNSPACE_H
#include "Optimization.hpp"
CoordinateSpace design_space();
#endif //OE_APP_SWITCHED_WINDING_SYNCHREL_DESIGNSPACE_H
\ No newline at end of file
#include "globalobjective.h"
ObjectiveMap evaluate(Particle p) {
// TODO
return ObjectiveMap{};
}
std::function<ObjectiveMap(Particle)> global_objective() {
return evaluate;
}
\ No newline at end of file
#ifndef OE_APP_SWITCHED_WINDING_SYNCHREL_GLOBALOBJECTIVE_H
#define OE_APP_SWITCHED_WINDING_SYNCHREL_GLOBALOBJECTIVE_H
#include "Optimization.hpp"
std::function<ObjectiveMap(Particle)> global_objective();
ObjectiveMap evaluate(Particle p);
#endif // OE_APP_SWITCHED_WINDING_SYNCHREL_GLOBALOBJECTIVE_H
\ No newline at end of file
#include "designspace.h"
#include "globalobjective.h"
int main(int argc, char** argv) {
// Arguments
size_t swarm_size{41};
double_t objective_tolerance{1e-2};
size_t maximum_iterations{20};
CoordinateSpace ds = design_space();
std::cout << ds;
std::function<ObjectiveMap(Particle)> go = global_objective();
Swarm swarm{ds,go,swarm_size,objective_tolerance,maximum_iterations};
std::cout << swarm;
std::cerr << "Reminder: Don't forget to group limit at0-at5 and rt0-rt6" << std::endl;
return 0;
}
\ No newline at end of file
#include "CoordinateSpace.h"
void CoordinateSpace::limit_velocity(Particle &p) {
for (auto &key_coord : Coordinates) {
std::string const &key = key_coord.first;
Coordinate const &coord = key_coord.second;
for (auto &ki : Coordinates.map()) {
std::string const &key = ki.first;
Coordinate const &coord = Coordinates[ki.second];
double_t &pv = p.velocity(key);
double_t v = SpeedLimit * coord.range();
......@@ -16,9 +16,9 @@ void CoordinateSpace::limit_velocity(Particle &p) {
}
void CoordinateSpace::limit_position(Particle &p) {
for (auto &key_coord : Coordinates) {
std::string const &key = key_coord.first;
Coordinate const &coord = key_coord.second;
for (auto &ki : Coordinates.map()) {
std::string const &key = ki.first;
Coordinate const &coord = Coordinates[ki.second];
double_t &px = p.position(key);
double_t &pv = p.velocity(key);
......@@ -37,11 +37,19 @@ void CoordinateSpace::limit_position(Particle &p) {
void CoordinateSpace::perturb(Particle &p, std::mt19937 &rng){
std::uniform_real_distribution<> dist(-Perturbation,+Perturbation);
for (auto &key_coord : Coordinates) {
std::string const &key = key_coord.first;
Coordinate const &coord = key_coord.second;
for (auto &ki : Coordinates.map()) {
std::string const &key = ki.first;
Coordinate const &coord = Coordinates[ki.second];
p.position(key) += dist(rng) * coord.range();
p.velocity(key) += dist(rng) * coord.range();
}
}
std::ostream& operator<<(std::ostream& os, CoordinateSpace& p) {
for (auto ki : p.Coordinates.map()) {
std::string k = ki.first;
Coordinate c = p[k];
os << k << " \u220A [" << c.lower_bound() << "," << c.upper_bound() << "]" << std::endl;
}
}
\ No newline at end of file
......@@ -3,10 +3,13 @@
#include "Particle.h"
#include "Coordinate.h"
#include "ContiguousDataMap.h"
using CoordinateMap = ContiguousDataMap<std::string,Coordinate>;
class CoordinateSpace {
public:
Coordinate &coordinate(std::string key) { return Coordinates[key]; };
Coordinate &operator[] (std::string key) { return Coordinates[key]; };
Particle new_particle(std::mt19937 &rng);
......@@ -18,11 +21,14 @@ public:
void perturb(Particle &p, std::mt19937 &rng);
std::map<std::string,Coordinate> Coordinates;
double_t SpeedLimit{0.2};
double_t Perturbation{0.2e-3};
friend std::ostream& operator<<(std::ostream& os, CoordinateSpace& p);
protected:
CoordinateMap Coordinates;
};
......
......@@ -60,7 +60,7 @@ bool Particle::is_converged(CoordinateSpace &space, double_t state_tol, double_t
std::string k = ki.first;
CoordinateState &s = state(k);
if(!s.is_converged(state_tol * space.coordinate(k).range())) {
if(!s.is_converged(state_tol * space[k].range())) {
return false;
}
}
......@@ -69,20 +69,23 @@ bool Particle::is_converged(CoordinateSpace &space, double_t state_tol, double_t
}
std::ostream& operator<<(std::ostream& os, Particle& p) {
os << "P-Best: " << p.PersonalBest << ", G-Best: " << p.GlobalBest << std::endl;
for (auto &m : p.state().map()) {
os << m.first << ": " << p.state(m.second).PersonalBest << ", ";
}
os << std::endl;
os << std:: endl << "P-Best: " << p.PersonalBest << ", G-Best: " << p.GlobalBest;
return os;
}
Particle CoordinateSpace::new_particle(std::mt19937 &rng) {
std::uniform_real_distribution<> dist(-1.0,1.0);
Particle particle;
for (auto &key_coord : Coordinates) {
std::string const &key = key_coord.first;
Coordinate const &coord = key_coord.second;
for (auto &key_ind : Coordinates.map()) {
std::string const &key = key_ind.first;
Coordinate const &coord = Coordinates[key_ind.second];
double_t v = dist(rng) * coord.range() * SpeedLimit;
double_t p = dist(rng) * coord.range() * SpeedLimit + coord.nominal();
......
......@@ -41,6 +41,14 @@ void Swarm::run() {
}
}
std::ostream& operator<<(std::ostream& os, Swarm& s) {
for (auto &p : s.Particles) {
os << p;
}
return os;
}
void Swarm::mpi_master() {
std::uniform_real_distribution<> dist(0.0,1.0);
......
......@@ -14,16 +14,18 @@ public:
Particles.push_back(Space.new_particle(RNG));
}
// Setup MPI
MPI_Type_contiguous(sizeof(CoordinateState) / sizeof(double_t), MPI_DOUBLE, &MPI_CoordinateState);
MPI_Type_commit(&MPI_CoordinateState);
MPI_Initialized(&MPI_FLAG);
if (MPI_FLAG) {
MPI_Type_contiguous(sizeof(CoordinateState) / sizeof(double_t), MPI_DOUBLE, &MPI_CoordinateState);
MPI_Type_commit(&MPI_CoordinateState);
MPI_Type_vector((int)Particles[0].state().size(),1,1,MPI_CoordinateState,&MPI_ParticleState);
MPI_Type_commit(&MPI_ParticleState);
MPI_Type_vector((int) Particles[0].state().size(), 1, 1, MPI_CoordinateState, &MPI_ParticleState);
MPI_Type_commit(&MPI_ParticleState);
MPI_Comm_size(MPI_COMM_WORLD, &MPI_SIZE);
MPI_Comm_rank(MPI_COMM_WORLD, &MPI_RANK);
MPI_Comm_size(MPI_COMM_WORLD, &MPI_SIZE);
MPI_Comm_rank(MPI_COMM_WORLD, &MPI_RANK);
}
}
size_t size() const { return Particles.size(); };
......@@ -32,6 +34,8 @@ public:
void mpi_run();
friend std::ostream& operator<<(std::ostream& os, Swarm& s);
std::vector<Particle> Particles;
double_t Omega{0.5};
......
......@@ -5,6 +5,7 @@
#include "Mesh.hpp"
#include "ModelTemplates.hpp"
#include "Physics.hpp"
#include "Optimization.hpp"
#include "gtest.h"
......
......@@ -583,9 +583,6 @@ double_t get_maximum(Eigen::VectorXd value) {
}
TEST_F(Synchronous_Reluctance_Machine, test) {
// TODO: Create Particle class
std::cout << "TODO: Create class Particle" << std::endl;
DesignSpace d = setup_design_space();
auto px = initialize_position(d);
......
......@@ -6,8 +6,8 @@ TEST(Particle_Swarm_Optimization, mpi_swarm) {
CoordinateSpace cs;
cs.coordinate("x") = Coordinate{-2.0,0.0,2.0};
cs.coordinate("y") = Coordinate{-2.0,0.0,2.0};
cs["x"] = Coordinate{-2.0,0.0,2.0};
cs["y"] = Coordinate{-2.0,0.0,2.0};
std::function<ObjectiveMap(Particle p)> go = [](Particle p) {
ObjectiveMap output;
......@@ -46,8 +46,8 @@ TEST(Particle_Swarm_Optimization, mpi_multiobjective_swarm) {
CoordinateSpace cs;
cs.coordinate("x") = Coordinate{-2.0,0.0,2.0};
cs.coordinate("y") = Coordinate{-2.0,0.0,2.0};
cs["x"] = Coordinate{-2.0,0.0,2.0};
cs["y"] = Coordinate{-2.0,0.0,2.0};
std::function<ObjectiveMap(Particle p)> go = [](Particle p) {
ObjectiveMap output;
......
......@@ -3,26 +3,26 @@
TEST(Particle_Swarm_Optimization, coordinate_space_initialization) {
CoordinateSpace cs;
cs.coordinate("x") = Coordinate{-1.0,0.0,1.0};
cs.coordinate("y") = Coordinate{2.0,3.0,5.0};
cs["x"] = Coordinate{-1.0,0.0,1.0};
cs["y"] = Coordinate{2.0,3.0,5.0};
EXPECT_EQ(cs.coordinate("x").lower_bound(), -1.0);
EXPECT_EQ(cs.coordinate("x").nominal(), 0.0);
EXPECT_EQ(cs.coordinate("x").upper_bound(), +1.0);
EXPECT_NEAR(cs.coordinate("x").range(), 2.0, FLT_EPSILON);
EXPECT_EQ(cs["x"].lower_bound(), -1.0);
EXPECT_EQ(cs["x"].nominal(), 0.0);
EXPECT_EQ(cs["x"].upper_bound(), +1.0);
EXPECT_NEAR(cs["x"].range(), 2.0, FLT_EPSILON);
EXPECT_EQ(cs.coordinate("y").lower_bound(), 2.0);
EXPECT_EQ(cs.coordinate("y").nominal(), 3.0);
EXPECT_EQ(cs.coordinate("y").upper_bound(), 5.0);
EXPECT_NEAR(cs.coordinate("y").range(), 3.0, FLT_EPSILON);
EXPECT_EQ(cs["y"].lower_bound(), 2.0);
EXPECT_EQ(cs["y"].nominal(), 3.0);
EXPECT_EQ(cs["y"].upper_bound(), 5.0);
EXPECT_NEAR(cs["y"].range(), 3.0, FLT_EPSILON);
}
TEST(Particle_Swarm_Optimization, particle_initialization) {
CoordinateSpace cs;
cs.coordinate("x") = Coordinate{-1.0,0.0,1.0};
cs.coordinate("y") = Coordinate{2.0,3.0,5.0};
cs["x"] = Coordinate{-1.0,0.0,1.0};
cs["y"] = Coordinate{2.0,3.0,5.0};
std::random_device rd;
std::mt19937 rng(rd());
......@@ -30,17 +30,17 @@ TEST(Particle_Swarm_Optimization, particle_initialization) {
Particle p = cs.new_particle(rng);
for (std::string key : {"x","y"}) {
EXPECT_GE(p.position(key), cs.coordinate(key).lower_bound());
EXPECT_LE(p.position(key), cs.coordinate(key).upper_bound());
EXPECT_GE(p.position(key), cs[key].lower_bound());
EXPECT_LE(p.position(key), cs[key].upper_bound());
EXPECT_GE(p.velocity(key), -cs.coordinate(key).range());
EXPECT_LE(p.velocity(key), +cs.coordinate(key).range());
EXPECT_GE(p.velocity(key), -cs[key].range());
EXPECT_LE(p.velocity(key), +cs[key].range());
EXPECT_GE(p.personal_best(key), cs.coordinate(key).lower_bound());
EXPECT_LE(p.personal_best(key), cs.coordinate(key).upper_bound());
EXPECT_GE(p.personal_best(key), cs[key].lower_bound());
EXPECT_LE(p.personal_best(key), cs[key].upper_bound());
EXPECT_GE(p.global_best(key), cs.coordinate(key).lower_bound());
EXPECT_LE(p.global_best(key), cs.coordinate(key).upper_bound());
EXPECT_GE(p.global_best(key), cs[key].lower_bound());
EXPECT_LE(p.global_best(key), cs[key].upper_bound());
EXPECT_GE(p.personal_best(), FLT_MAX);
EXPECT_GE(p.global_best(), FLT_MAX);
......@@ -62,10 +62,10 @@ TEST(Particle_Swarm_Optimization, particle_initialization) {
TEST(Particle_Swarm_Optimization, single_objective_swarm) {
CoordinateSpace cs;
cs.coordinate("x") = Coordinate{-2.0,0.0,2.0};
cs.coordinate("y") = Coordinate{-2.0,0.0,2.0};
cs["x"] = Coordinate{-2.0,0.0,2.0};
cs["y"] = Coordinate{-2.0,0.0,2.0};
std::function<ObjectiveMap(Particle p)> go = [](Particle p) {
std::function<ObjectiveMap(Particle)> go = [](Particle p) {
ObjectiveMap output;
double_t x = p.position("x");
......@@ -97,8 +97,8 @@ TEST(Particle_Swarm_Optimization, single_objective_swarm) {
TEST(Particle_Swarm_Optimization, multiobjective_swarm) {
CoordinateSpace cs;
cs.coordinate("x") = Coordinate{-2.0,0.0,2.0};
cs.coordinate("y") = Coordinate{-2.0,0.0,2.0};
cs["x"] = Coordinate{-2.0,0.0,2.0};
cs["y"] = Coordinate{-2.0,0.0,2.0};
std::function<ObjectiveMap(Particle p)> go = [](Particle p) {
ObjectiveMap output;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment