Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rotation cleanup #2382

Merged
merged 10 commits into from
Nov 30, 2018
19 changes: 11 additions & 8 deletions src/core/communication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,15 +644,16 @@ void mpi_send_rotational_inertia_slave(int pnode, int part) {
#endif
}

void mpi_rotate_particle(int pnode, int part, double axis[3], double angle) {
void mpi_rotate_particle(int pnode, int part, const Vector3d &axis,
double angle) {
#ifdef ROTATION
mpi_call(mpi_rotate_particle_slave, pnode, part);

if (pnode == this_node) {
Particle *p = local_particles[part];
local_rotate_particle(p, axis, angle);
local_rotate_particle(*p, axis, angle);
} else {
MPI_Send(axis, 3, MPI_DOUBLE, pnode, SOME_TAG, comm_cart);
MPI_Send(axis.data(), 3, MPI_DOUBLE, pnode, SOME_TAG, comm_cart);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use boost::mpi, which will also fix all the builds.

MPI_Send(&angle, 1, MPI_DOUBLE, pnode, SOME_TAG, comm_cart);
}

Expand All @@ -664,10 +665,12 @@ void mpi_rotate_particle_slave(int pnode, int part) {
#ifdef ROTATION
if (pnode == this_node) {
Particle *p = local_particles[part];
double axis[3], angle;
MPI_Recv(axis, 3, MPI_DOUBLE, 0, SOME_TAG, comm_cart, MPI_STATUS_IGNORE);
Vector3d axis;
double angle;
MPI_Recv(axis.data(), 3, MPI_DOUBLE, 0, SOME_TAG, comm_cart,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boost::mpi

MPI_STATUS_IGNORE);
MPI_Recv(&angle, 1, MPI_DOUBLE, 0, SOME_TAG, comm_cart, MPI_STATUS_IGNORE);
local_rotate_particle(p, axis, angle);
local_rotate_particle(*p, axis, angle);
}

on_particle_change();
Expand Down Expand Up @@ -815,7 +818,7 @@ void mpi_send_quat_slave(int pnode, int part) {

/********************* REQ_SET_OMEGA ********/

void mpi_send_omega(int pnode, int part, double omega[3]) {
void mpi_send_omega(int pnode, int part, const double omega[3]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const Vector3d& omega

#ifdef ROTATION
mpi_call(mpi_send_omega_slave, pnode, part);

Expand Down Expand Up @@ -847,7 +850,7 @@ void mpi_send_omega_slave(int pnode, int part) {

/********************* REQ_SET_TORQUE ********/

void mpi_send_torque(int pnode, int part, double torque[3]) {
void mpi_send_torque(int pnode, int part, const double torque[3]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const Vector3d& torque

#ifdef ROTATION
mpi_call(mpi_send_torque_slave, pnode, part);

Expand Down
7 changes: 4 additions & 3 deletions src/core/communication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ void mpi_send_rotational_inertia(int node, int part, double rinertia[3]);
\param axis rotation axis
\param angle rotation angle
*/
void mpi_rotate_particle(int node, int part, double axis[3], double angle);
void mpi_rotate_particle(int pnode, int part, const Vector3d &axis,
double angle);
#endif

#ifdef AFFINITY
Expand Down Expand Up @@ -278,15 +279,15 @@ void mpi_send_rotation(int pnode, int part, short int rot);
\param node the node it is attached to.
\param omega its new angular velocity.
*/
void mpi_send_omega(int node, int part, double omega[3]);
void mpi_send_omega(int node, int part, const double omega[3]);

/** Issue REQ_SET_TORQUE: send particle torque.
Also calls \ref on_particle_change.
\param part the particle.
\param node the node it is attached to.
\param torque its new torque.
*/
void mpi_send_torque(int node, int part, double torque[3]);
void mpi_send_torque(int node, int part, const double torque[3]);
#endif

#ifdef DIPOLES
Expand Down
6 changes: 3 additions & 3 deletions src/core/minimize_energy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ bool steepest_descent_step(void) {
}
#ifdef ROTATION
// Rotational increment
double dq[3]; // Vector parallel to torque
Vector3d dq; // Vector parallel to torque
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like can directly initialize dq and remove the loop in line 100.


for (int j = 0; j < 3; j++) {
dq[j] = 0;
Expand All @@ -106,7 +106,7 @@ bool steepest_descent_step(void) {
dq[j] = params->gamma * p.f.torque[j];
}
// Normalize rotation axis and compute amount of rotation
double l = normr(dq);
double l = dq.norm();
if (l > 0.0) {
for (int j = 0; j < 3; j++)
dq[j] /= l;
Expand All @@ -116,7 +116,7 @@ bool steepest_descent_step(void) {
l = sgn(l) * params->max_displacement;

// Rotate the particle around axis dq by amount l
local_rotate_particle(&(p), dq, l);
local_rotate_particle(p, dq, l);
}
#endif

Expand Down
16 changes: 2 additions & 14 deletions src/core/observables/ParticleAngularVelocities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,8 @@ class ParticleAngularVelocities : public PidObservable {
std::vector<double> res(n_values());
for (int i = 0; i < ids().size(); i++) {
#ifdef ROTATION

double RMat[9];
double omega[3];
define_rotation_matrix(partCfg[ids()[i]], RMat);
omega[0] = RMat[0 + 3 * 0] * partCfg[ids()[i]].m.omega[0] +
RMat[1 + 3 * 0] * partCfg[ids()[i]].m.omega[1] +
RMat[2 + 3 * 0] * partCfg[ids()[i]].m.omega[2];
omega[1] = RMat[0 + 3 * 1] * partCfg[ids()[i]].m.omega[0] +
RMat[1 + 3 * 1] * partCfg[ids()[i]].m.omega[1] +
RMat[2 + 3 * 1] * partCfg[ids()[i]].m.omega[2];
omega[2] = RMat[0 + 3 * 2] * partCfg[ids()[i]].m.omega[0] +
RMat[1 + 3 * 2] * partCfg[ids()[i]].m.omega[1] +
RMat[2 + 3 * 2] * partCfg[ids()[i]].m.omega[2];

const Vector3d omega =
convert_vector_body_to_space(partCfg[i], partCfg[i].m.omega);
res[3 * i + 0] = omega[0];
res[3 * i + 1] = omega[1];
res[3 * i + 2] = omega[2];
Expand Down
16 changes: 2 additions & 14 deletions src/core/observables/ParticleBodyVelocities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,8 @@ class ParticleBodyVelocities : public PidObservable {
#ifdef ROTATION

double RMat[9];
double vel_lab[3];
double vel_body[3];

vel_lab[0] = partCfg[ids()[i]].m.v[0];
vel_lab[1] = partCfg[ids()[i]].m.v[1];
vel_lab[2] = partCfg[ids()[i]].m.v[2];
define_rotation_matrix(partCfg[ids()[i]], RMat);

vel_body[0] = RMat[0 + 3 * 0] * vel_lab[0] +
RMat[0 + 3 * 1] * vel_lab[1] + RMat[0 + 3 * 2] * vel_lab[2];
vel_body[1] = RMat[1 + 3 * 0] * vel_lab[0] +
RMat[1 + 3 * 1] * vel_lab[1] + RMat[1 + 3 * 2] * vel_lab[2];
vel_body[2] = RMat[2 + 3 * 0] * vel_lab[0] +
RMat[2 + 3 * 1] * vel_lab[1] + RMat[2 + 3 * 2] * vel_lab[2];
const Vector3d vel_body =
convert_vector_space_to_body(partCfg[i], partCfg[i].m.v);

res[3 * i + 0] = vel_body[0];
res[3 * i + 1] = vel_body[1];
Expand Down
54 changes: 11 additions & 43 deletions src/core/particle_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ int set_particle_rotation(int part, int rot) {
}
#endif
#ifdef ROTATION
int rotate_particle(int part, double axis[3], double angle) {
int rotate_particle(int part, const Vector3d &axis, double angle) {
auto const pnode = get_particle_node(part);

mpi_rotate_particle(pnode, part, axis, angle);
Expand Down Expand Up @@ -657,65 +657,37 @@ int set_particle_quat(int part, double quat[4]) {
return ES_OK;
}

int set_particle_omega_lab(int part, double omega_lab[3]) {
int set_particle_omega_lab(int part, const Vector3d &omega_lab) {
auto const &particle = get_particle_data(part);

/* Internal functions require the body coordinates
so we need to convert to these from the lab frame */

double A[9];
double omega[3];

define_rotation_matrix(particle, A);

omega[0] = A[0 + 3 * 0] * omega_lab[0] + A[0 + 3 * 1] * omega_lab[1] +
A[0 + 3 * 2] * omega_lab[2];
omega[1] = A[1 + 3 * 0] * omega_lab[0] + A[1 + 3 * 1] * omega_lab[1] +
A[1 + 3 * 2] * omega_lab[2];
omega[2] = A[2 + 3 * 0] * omega_lab[0] + A[2 + 3 * 1] * omega_lab[1] +
A[2 + 3 * 2] * omega_lab[2];

auto const pnode = get_particle_node(part);
mpi_send_omega(pnode, part, omega);
mpi_send_omega(pnode, part,
convert_vector_space_to_body(particle, omega_lab).data());
return ES_OK;
}

int set_particle_omega_body(int part, double omega[3]) {
int set_particle_omega_body(int part, const Vector3d &omega) {
auto const pnode = get_particle_node(part);
mpi_send_omega(pnode, part, omega);
mpi_send_omega(pnode, part, omega.data());
return ES_OK;
}

int set_particle_torque_lab(int part, double torque_lab[3]) {
int set_particle_torque_lab(int part, const Vector3d &torque_lab) {
auto const &particle = get_particle_data(part);

/* Internal functions require the body coordinates
so we need to convert to these from the lab frame */

double A[9];
double torque[3];

define_rotation_matrix(particle, A);

torque[0] = A[0 + 3 * 0] * torque_lab[0] + A[0 + 3 * 1] * torque_lab[1] +
A[0 + 3 * 2] * torque_lab[2];
torque[1] = A[1 + 3 * 0] * torque_lab[0] + A[1 + 3 * 1] * torque_lab[1] +
A[1 + 3 * 2] * torque_lab[2];
torque[2] = A[2 + 3 * 0] * torque_lab[0] + A[2 + 3 * 1] * torque_lab[1] +
A[2 + 3 * 2] * torque_lab[2];

auto const pnode = get_particle_node(part);
mpi_send_torque(pnode, part, torque);
mpi_send_torque(pnode, part,
convert_vector_space_to_body(particle, torque_lab).data());
return ES_OK;
}

int set_particle_torque_body(int part, double torque[3]) {
int set_particle_torque_body(int part, const Vector3d &torque) {
auto const pnode = get_particle_node(part);

/* Nothing to be done but pass, since the coordinates
are already in the proper frame */

mpi_send_torque(pnode, part, torque);
mpi_send_torque(pnode, part, torque.data());
return ES_OK;
}

Expand Down Expand Up @@ -1239,10 +1211,6 @@ void pointer_to_omega_body(Particle const *p, double const *&res) {
res = p->m.omega.data();
}

void pointer_to_torque_lab(Particle const *p, double const *&res) {
res = p->f.torque.data();
}

void pointer_to_quat(Particle const *p, double const *&res) {
res = p->r.quat.data();
}
Expand Down
12 changes: 6 additions & 6 deletions src/core/particle_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ int set_particle_rotation(int part, int rot);
@param axis rotation axis
@param angle rotation angle
*/
int rotate_particle(int part, double axis[3], double angle);
int rotate_particle(int part, const Vector3d &axis, double angle);

#ifdef AFFINITY
/** Call only on the master node: set particle affinity.
Expand Down Expand Up @@ -736,28 +736,28 @@ int set_particle_quat(int part, double quat[4]);
@param omega its new angular velocity.
@return ES_OK if particle existed
*/
int set_particle_omega_lab(int part, double omega[3]);
int set_particle_omega_lab(int part, const Vector3d &omega);

/** Call only on the master node: set particle angular velocity in body frame.
@param part the particle.
@param omega its new angular velocity.
@return ES_OK if particle existed
*/
int set_particle_omega_body(int part, double omega[3]);
int set_particle_omega_body(int part, const Vector3d &omega);

/** Call only on the master node: set particle torque from lab frame.
@param part the particle.
@param torque its new torque.
@return ES_OK if particle existed
*/
int set_particle_torque_lab(int part, double torque[3]);
int set_particle_torque_lab(int part, const Vector3d &torque);

/** Call only on the master node: set particle torque in body frame.
@param part the particle.
@param torque its new torque.
@return ES_OK if particle existed
*/
int set_particle_torque_body(int part, double torque[3]);
int set_particle_torque_body(int part, const Vector3d &torque);
#endif

#ifdef DIPOLES
Expand Down Expand Up @@ -995,7 +995,7 @@ int number_of_particles_with_type(int type);
#ifdef ROTATION
void pointer_to_omega_body(Particle const *p, double const *&res);

void pointer_to_torque_lab(Particle const *p, double const *&res);
inline Vector3d get_torque_body(const Particle &p) { return p.f.torque; }

void pointer_to_quat(Particle const *p, double const *&res);

Expand Down
4 changes: 2 additions & 2 deletions src/core/rotate_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void local_rotate_system(double phi, double theta, double alpha) {
mpi::all_reduce(comm_cart, local_com, std::plus<Vector3d>()) / total_mass;

// Rotation axis in Cartesian coordinates
double axis[3];
Vector3d axis;
axis[0] = sin(theta) * cos(phi);
axis[1] = sin(theta) * sin(phi);
axis[2] = cos(theta);
Expand All @@ -67,7 +67,7 @@ void local_rotate_system(double phi, double theta, double alpha) {
p.r.p[j] = com[j] + res[j];
}
#ifdef ROTATION
local_rotate_particle(&p, axis, alpha);
local_rotate_particle(p, axis, alpha);
#endif
}

Expand Down
Loading