Hyperoptimization

class Hyperoptimization

Hyperoptimization implementation.

Main functionality is in initialization and design loop functions. This class assumes position vectors are set up in accordance to the settings used by whatever Petsc solver is being used.

The class is abstracted from Petsc as much as possible, other than the use of Petsc type variables. Wrappers are provided for all operations that are problem specific, i.e. for filtering, Lagrangian multiplier calculation, and sensitivity calculation. This is so that while the implementation provided is intended for topology optimization, these wrappers can be modified for any optimization problem that uses Petsc.

Public Functions

inline Hyperoptimization()

Empty constructor. Initialization functions are used to comply with Petsc error handling.

inline ~Hyperoptimization()

Destructor.

PetscErrorCode init(SensitivitiesWrapper &sensitivitiesWrapper, FilterWrapper &filter, LagrangeMultiplier &lagMult, PetscScalar temperature, Vec initialPositions, Vec initialVelocities, PetscInt NHChainOrder, PetscInt numIterations, PetscScalar timestep, FileManager *fileManager, std::vector<uint32_t> iterationSaveRange, bool saveHamiltonian, PetscScalar volumeFraction, uint32_t saveFrequency, verbosity printInfo, double maxSimTime = std::numeric_limits<double>::max())

Initialization funciton.

Sets up the design loop. This version initializes the Nose Hoover chain and is intended for new runs with no restart variables. Chains the initialization by calling the overloaded initialization function.

Parameters:
  • sensitivitiesWrapper – wrapper called to calculate sensitivities.

  • filter – wrapper called to calculate all filtering.

  • lagMult – Lagrangian Multiplier wrapper.

  • temperature – target temperature for the system.

  • initialPositions – position vectors initialized to desired distribution.

  • initialVelocities – velocity vectors initialized to desired distribution.

  • NHChainOrder – number of Nose Hoover particles.

  • numIterations – number of iterations to simulate.

  • timestep – timestep between iterations.

  • fileManager – contains helper functions for saving states and iterations.

  • iterationSaveRange – range between which to save position data.

  • saveHamiltonian – optional parameter to save Hamiltonian and Compliance data.

  • volumeFraction – target volume fraction.

  • saveFrequency – frequency at which to save position state within save range.

  • maxSimTime – if set, maxmimum amount of simulation time to iterate for.

Returns:

0 on success, PetscError otherwise.

PetscErrorCode init(SensitivitiesWrapper &sensitivitiesWrapper, LagrangeMultiplier &lagMult, FilterWrapper &filter, FileManager *fileManager, PetscInt NHChainOrder, PetscScalar timestep, PetscScalar temperature, PetscInt numIterations, std::vector<uint32_t> iterationSaveRange, Vec initialPositions, Vec initialVelocities, Vec initialEvenNoseHooverPosition, Vec initialEvenNoseHooverVelocity, Vec initialOddNoseHooverPosition, Vec initialOddNoseHooverVelocity, bool saveHamiltonian, PetscScalar volumeFraction, uint32_t saveFrequency, verbosity printInfo, double maxSimTime = std::numeric_limits<double>::max())

Overloaded initialization funciton.

Sets up the design loop. This version takes restarted Nose Hoover chain variables.

Parameters:
  • sensitivitiesWrapper – wrapper called to calculate sensitivities.

  • lagMult – Lagrangian Multiplier wrapper.

  • filter – wrapper called to calculate all filtering.

  • fileManager – contains helper functions for saving states and iterations.

  • NHChainOrder – number of Nose Hoover particles.

  • timestep – timestep between iterations.

  • temperature – target temperature for the system.

  • numIterations – number of iterations to simulate.

  • iterationSaveRange – range between which to save position data.

  • initialPositions – position vectors initialized to desired distribution.

  • initialVelocities – velocity vectors initialized to desired distribution.

  • initialEvenNoseHooverPosition – initial positions of Nose Hoover particles with even indices

  • initialEvenNoseHooverVelocity – initial velocities of Nose Hoover particles with even indices

  • initialOddNoseHooverPosition – initial positions of Nose Hoover particles with odd indices

  • initialOddNoseHooverVelocity – initial velocities of Nose Hoover particles with odd indices

  • saveHamiltonian – optional parameter to save Hamiltonian and Compliance data.

  • volumeFraction – target volume fraction.

  • saveFrequency – frequency at which to save position state within save range.

  • maxSimTime – if set, maxmimum amount of simulation time to iterate for.

Returns:

0 on success, PetscError otherwise.

void enableVariableTimestep(PetscScalar timestepConstantAlpha, PetscScalar timestepConstantBeta, PetscScalar diffusionConstant)

Sets up variable timestepping.

Todo:

fill out info on how the algorithm works when fully implemented.

Parameters:
  • timestepConstantAlpha –

  • timestepConstantBeta –

  • timestepConstantK –

  • diffusionConstant –

PetscErrorCode runDesignLoop()

Main iteration loop which implements the hyperoptimization design algorithm.

Todo:

basic info on the loop here?

inline std::vector<PetscScalar> getHamiltonians()

Accessor for final vector of Hamiltonians after running design loop.

Returns:

vector of Hamiltonians for each iteration.

inline std::vector<PetscScalar> getCompliance()

Accessor for final vector of Compliance after running design loop.

Returns:

vector of Compliance for each iteration.

inline std::vector<PetscScalar> getTemperatures()

Accessor for final vector of Hamiltonians after running design loop.

Returns:

vector of temperatures for each iteration.

inline std::vector<PetscScalar> getLagrangeMultipliers()

Accessor for final vector of Hamiltonians after running design loop.

Returns:

vector of Lagrange Multipliers for each iteration.

inline std::vector<PetscScalar> getIterationTimes()

Accessor for final vector of Hamiltonians after running design loop.

Returns:

vector of iteration times for each iteration.

inline std::vector<PetscInt> getSolverIterationsHamiltonian()

Accessor for final vector of Hamiltonians after running design loop.

Returns:

vector of iteration times for each iteration.

inline std::vector<PetscInt> getSolverIterationsSensitivity()

Accessor for final vector of Hamiltonians after running design loop.

Returns:

vector of iteration times for each iteration.

inline HypOptParameters getFinalState()

Accessor for final vector of Hamiltonians after running design loop.

Returns:

vector of positions for the final iteration.

inline bool getSaveHamiltonian()

Accessor for final vector of Hamiltonians after running design loop.

Returns:

save hamiltonian option.

inline std::vector<PetscScalar> getTimesteps()

Accessor for timestep value at each iteration.

Returns:

a vector of the timestep used at each iteration.

inline std::vector<PetscScalar> getEnergyErrors()

Accessor for energy error at each timestep.

Todo:

update description when variable timestep is finished.

Returns:

a vector of the energy error at each iteration.

inline std::vector<PetscScalar> getVolFracs()

Accessor for volume fraction at each iteration.

Returns:

a vector of the systems volume fraction calculated at each iteration

Private Functions

PetscErrorCode calculateFirstNoseHooverAcceleration(Vec allVelocities, Vec *accelerations)

the acceleration of the first Nose Hoover particle. Solves the equation:

\[ a_{s_1} = \frac{\sum_i(v_i^2) - (N - N_c)T}{Q_1} \]

where:

  • \( a_{s1} \) is the first Nose Hoover particle’s acceleration

  • \( v_i \) is the i’th design particle’s velocity

  • \( N \) is number of particles

  • \( N_c \) is number of constraints

  • \( T \) is target temperature

  • \( Q_1 \) is mass of the first Nose Hoover particle

Parameters:
  • allVelocities – vector of particle velocities with which to calculate the accelerations

  • accelerations – [out] vector of Nose Hoover accelerations (only first index populated)

Returns:

0 on success, PetscError otherwise.

PetscErrorCode calculateRemainingNoseHooverAccelerations(Vec noseHooverVelocities, bool evenOutput, Vec *result)

Computes the acceleration of the all Nose Hoover particles beyond the first. Solves the equation:

\[ a_{s_i} = \frac{Q_{i-1} v_{s_{i-1}}^2 - T}{Q_i} \]

where:

  • \( a_{s_i} \) is the i’th Nose Hoover acceleration

  • \( v_{s_i} \) is the i’th Nose Hoover velocity

  • \( Q{i} \) is the i’th Nose Hoover mass

  • \( T \) is the target temperature

Parameters:
  • noseHooverVelocities – \( v_{s_{i-1}} \), Vector of the Nose Hoover velocities. This is either the odd or even velocities depending on the flag evenOutput.

  • evenOutput – flag to indicate if even or odd acclerations are being calculated.

  • result – [out] resulting accelerations. They are populated in the correct locations in the array. I.e. for odd output, the positions populated are \( i = 3, 5, 7, ... \).

Returns:

0 on success, PetscError otherwise.

PetscErrorCode calculatePositionIncrement(Vec previousPosition, Vec previousVelocity, PetscScalar timeStep, Vec *result)

Calculates particle positions for the provided timestep. Computes the equation:

\[ x(t + \Delta t) = x(t_a) + \Delta t v(t_b), \]

where:

  • \( x(t + \Delta t) \) is the incremented position,

  • \( x(t_a) \) is the position at a previous time \( t_a \),

  • \( \Delta t \) is the timestep,

  • \( v(t_b) \) is the velocity at a previous time \( t_b \).

where the positions and velocities can be for either design particles or Nose Hoover particles. Note that \( t_a \) and \( t_b \) are typically either the same or off by a half timestep.

Parameters:
  • previousPosition – \( x(t_a) \)

  • previousVelocity – \( v(t_b) \)

  • timeStep – \( \Delta t \)

  • result – [out] array in which to add the return resulting position vector. Must match shape of input vectors.

Returns:

0 on success, PetscError otherwise.

PetscErrorCode calculateVelocityIncrement(Vec velocityOne, Vec velocityTwo, Vec acceleration, PetscScalar timeStep, Vec *result)

Calculates the next velocity at the given time step. Completes the equation:

\[ v_1(t + \Delta t) = v_1(t) e^{-\Delta t v_2(t_a)} + \Delta t a(t_a) e^{-\frac{\Delta t}{2} v_2(t_a)}, \]

where:

  • \( v_1(t + \Delta t) \) is the incremented primary velocity,

  • \( v_1(t) \) is the previous primary velocity,

  • \( \Delta t \) is the timestep

  • \( v_2(t_a) \) is a secondary velocity term (odd Nose Hoover if the primary is even, and vice versa) at a time \( t_a \)

  • \( a(t_a) \) is the primary velcities’ first derivative at the time \( t_a \).

Parameters:
  • velocityOne – \( v_1(t) \), vector of the velocity to be incremented.

  • velocityTwo – \( v_2(t_a) \), vector of the second velocities. Typically even velocities if the incremented are odd, and vice-versa.

  • acceleration – \( a(t_a) \), vector of the acceleration of the velocity to be incremented.

  • timeStep – \( \Delta t \), the timestep at which to calculate the new velocity.

  • result – [out] array in which to add the return result. Must match shape of input vectors.

Returns:

0 on success, PetscError otherwise.

PetscErrorCode calculateVelocityIncrement(Vec velocityOne, PetscScalar velocityTwo, Vec acceleration, PetscScalar timeStep, Vec *result)

Overloaded function to calculates the next velocity at the given time step.

The only difference from the base function is that \( v_2(t_a) \) is allowed to be a scalar value. This is necessary as one step requires using the first Nose Hoover velocity for this term for every element in the resulting vector.

Parameters:
  • velocityOne – \( v_1(t) \), vector of the velocity to be incremented.

  • velocityTwo – \( v_2(t_a) \), scalar second velocity.

  • acceleration – \( a(t_a) \), vector of the acceleration of the velocity to be incremented.

  • timeStep – \( \Delta t \), the timestep at which to calculate the new velocity.

  • result – [out] array in which to add the return result. Must match shape of input vectors.

Returns:

0 on success, PetscError otherwise.

PetscErrorCode assembleNewPositions(PetscScalar firstNoseHooverVelocity, PetscScalar *LagrangeMultiplier)

Calcualtes the final incremented position vector. This step is somewhat complicated, but can be broken down as follows:

  1. Truncate the positions at \( t + \frac{\Delta t}{2} \) to be within the range \( [0,1] \).

  2. Scale the constraint sensitivities by a calculated amount.

  3. Calculate Lagrangian multiplier.

  4. Scale the step 2 result by the Lagrangian multiplier

  5. Pointwise add the new position to the result of step 4

  6. Truncate the result of step 5 to get a meaningful value to the new positions.

The final value for the new positions is saved in the parameter Hyperoptimization::newPosition.

Note

The full derivation of this step is more complicated, and the hyperoptimization paper should be consulted for more details.

Parameters:
  • firstNoseHooverVelocity – the first Nose Hoover velocity at time \( t + \frac{\Delta t}{2} \)

  • LagrangeMultiplier – [out] the Lagrangian multiplier is calculated as a byproduct of this function, and is returned here. Can be NULL if not desired.

Returns:

0 on success, PetscError otherwise.

PetscErrorCode calculateTemperature(Vec velocities, PetscScalar *temperature)

Calcualtes the system temperature given the vector of design particle velocities.

The temperature is calculated as:

\[ T = \frac{\sum_{i=0}^{N}{v_i^2}}{N} \]

where:

  • \( T \) is the system temperature.

  • \( N \) is the number of design particles.

  • \( v_i \) is the i’th design particle velocity.

Parameters:
  • velocities – the design particle velocities from which to calculate temperature.

  • temperature – [out] resulting temperature to populate.

Returns:

0 on success, PetscError otherwise.

PetscErrorCode calculateHamiltonian(Vec velocities, Vec positions, PetscScalar *hamiltonian)

Calculates the Hamiltonian and objective function of the system.

The Hamiltonian is calculated as:

\[ H = O + \frac{v^2}{2}, \]

where:

  • \( H \) is the Hamiltonian,

  • \( O \) is the objective function, which is abstracted and calculated by the problem-specific SensitivitiesWrapper,

  • \( v \) is the vector of disgn particle velocities.

Parameters:
  • velocities – vector of disgn particle velocities.

  • positions – vector of disgn particle positions (used for calculating the objective function).

  • hamiltonian – [out] resulting Hamiltonian to populate.

Returns:

0 on success, PetscError otherwise.

PetscErrorCode calculateSensitvities(Vec positions)

Calculates both constraint and objective sensitivities.

The provided design particle positions are first filtered, before calculating the sensitivities using the abstracted-sensitivity wrapper. Finally, the sensitivities are filtered. The results are stored in Hyperoptimization::sensitivities and Hyperoptimization::constraintSensitivities respectively.

Parameters:

positions – vector of disgn particle positions.

Returns:

0 on success, PetscError otherwise.

PetscErrorCode truncatePositions(Vec *positions)

Truncates the design particle positions to be real-valued, within the range [0,1].

All values greater than 1 are truncated to exactly 1, and all values less than 0 are truncated to exactly 0. While this does add some small error to the resulting volume fraction and velocity distributions, it is sufficiently small as to not affect the design iteration process. It is interesting to note that low temperature systems will tend to have many particles close to zero, meaning that more particles are truncated from negative values to 0 than from high values to 1. This gives a high tendancy to slightly err high on the objective function.

void calculateNextTimeStep()

Calculates the next timestep for the variable timestep algorithm.

Uses the following equation:

\[ \Delta t_i = \alpha \beta^k \Delta t_{i=i} \]

where:

  • \( \Delta t_i \) is the timestep at step \( i \)

Private Members

FileManager *fileManager
SensitivitiesWrapper sensitivitiesWrapper
FilterWrapper filter
LagrangeMultiplier lagMult
PetscScalar temperature
PetscInt NHChainOrder
PetscInt numParticles
PetscInt numIterations
PetscInt numConstraints
PetscScalar timestep
PetscScalar halfTimestep
Vec evenNoseHooverMass
Vec oddNoseHooverMass

Mass of Nose Hoover particles.

Note

This is initialized automatically to 1 for now, can be exposed to python if necessary.

PetscScalar volumeFraction
HypOptParameters prevState
Vec newPosition
Vec sensitivities
Vec constraintSensitivities
std::vector<PetscScalar> LagrangeMultipliers
std::vector<PetscScalar> hamiltonians
std::vector<PetscScalar> compliance
std::vector<PetscScalar> temperatures
std::vector<PetscScalar> iterationTimes
std::vector<PetscInt> sensitivitySolverIterations
std::vector<PetscInt> hamiltonianSolverIterations
std::vector<PetscScalar> timesteps
std::vector<PetscScalar> energyErrors
std::vector<PetscScalar> volfracs
verbosity printInfo = INFO
bool inSimulation = false
bool saveHamiltonian = false
bool variableTimestep = false
double maxSimTime
uint32_t saveFrequency
std::vector<uint32_t> iterationSaveRange
PetscScalar previousTimestep = 0
PetscScalar timestepConstantAlpha = 1.1
PetscScalar timestepConstantBeta = 0.99
PetscScalar timestepConstantK = 1
PetscScalar diffusionConstant = 0.00000001
PetscScalar previousTemperature = 0