Optimize¶
The optimize module contains geometry optimization algorithms.
Base¶
Base classes for geometry optimization.
OptimizationResult
dataclass
¶
OptimizationResult(structure, energy, fmax, nsteps, converged, reason)
Bases: Generic[S]
Result of a geometry optimization.
Attributes:
| Name | Type | Description |
|---|---|---|
structure |
S
|
Final optimized structure (same object if inplace=True) |
energy |
float
|
Final potential energy |
fmax |
float
|
Final maximum force component |
nsteps |
int
|
Number of optimization steps taken |
converged |
bool
|
Whether convergence criteria were met |
reason |
str
|
Human-readable termination reason |
Optimizer ¶
Optimizer(potential, *, entity_type=Entity)
Bases: ABC, Generic[S]
Base class for structure optimizers.
Works with any StructLike (Struct, Atomistic, CoarseGrain, etc.) that:
- Has .entities (TypeBucket) containing entities with "xyz" field
- Has .to_frame() method to convert to Frame format
The optimizer calls potential.calc_energy(frame) and potential.calc_forces(frame) directly - each potential is responsible for extracting what it needs from Frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
potential
|
PotentialLike
|
Potential with calc_energy/calc_forces methods |
required |
entity_type
|
type[Entity]
|
Type of entity to optimize (default: Entity for all) |
Entity
|
Example
from molpy.optimize import LBFGS from molpy.potential.bond import Harmonic
potential = Harmonic(k=100.0, r0=1.5) opt = LBFGS(potential, maxstep=0.04, memory=20) result = opt.run(struct, fmax=0.01, steps=500)
Source code in src/molpy/optimize/base.py
67 68 69 70 71 72 73 74 75 | |
attach ¶
attach(func, interval=1, **kwargs)
Attach a callback function.
The callback will be called every interval steps with the optimizer
instance and current structure as arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
Callback function(optimizer, structure, **kwargs) |
required |
interval
|
int
|
Call every N steps |
1
|
**kwargs
|
Any
|
Additional arguments for callback |
{}
|
Source code in src/molpy/optimize/base.py
242 243 244 245 246 247 248 249 250 251 252 253 | |
get_energy ¶
get_energy(structure)
Compute energy for structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
S
|
Structure to evaluate |
required |
Returns:
| Type | Description |
|---|---|
float
|
Potential energy |
Source code in src/molpy/optimize/base.py
139 140 141 142 143 144 145 146 147 148 149 | |
get_energy_and_forces ¶
get_energy_and_forces(structure)
Compute energy and forces via Frame interface.
Converts structure to Frame and calls potential methods directly. Each potential is responsible for extracting what it needs from Frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
S
|
Structure to evaluate |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, ndarray]
|
(energy, forces) where forces is (N, 3) array |
Source code in src/molpy/optimize/base.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
get_forces ¶
get_forces(structure)
Compute forces for structure as (N, 3) array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
S
|
Structure to evaluate |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
(N, 3) array of forces |
Source code in src/molpy/optimize/base.py
151 152 153 154 155 156 157 158 159 160 161 | |
get_positions ¶
get_positions(structure)
Extract positions as (N, 3) array from entities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
S
|
Structure to extract positions from |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
(N, 3) numpy array of positions |
Source code in src/molpy/optimize/base.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
run ¶
run(structure, fmax=0.01, steps=1000, *, inplace=True)
Run optimization until convergence or max steps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
S
|
Structure to optimize |
required |
fmax
|
float
|
Convergence threshold (max force component) |
0.01
|
steps
|
int
|
Maximum number of steps |
1000
|
inplace
|
bool
|
If True, modify structure in-place; if False, work on copy |
True
|
Returns:
| Type | Description |
|---|---|
OptimizationResult[S]
|
OptimizationResult with final state |
Source code in src/molpy/optimize/base.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
set_positions ¶
set_positions(structure, positions)
Write positions back into structure entities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
S
|
Structure to update |
required |
positions
|
ndarray
|
(N, 3) array of new positions |
required |
Source code in src/molpy/optimize/base.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
step
abstractmethod
¶
step(structure)
Perform one optimization step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
S
|
Structure to optimize (modified in-place) |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, float]
|
(energy, fmax) tuple where: energy: potential energy after step fmax: maximum force component after step |
Source code in src/molpy/optimize/base.py
165 166 167 168 169 170 171 172 173 174 175 176 177 | |
PotentialLike ¶
Bases: Protocol
Protocol for potential functions compatible with calc_energy_from_frame.
LBFGS¶
L-BFGS geometry optimizer.
LBFGS ¶
LBFGS(potential, *, maxstep=0.04, memory=20, damping=1.0, entity_type=None)
Bases: Optimizer[S]
Limited-memory BFGS geometry optimizer.
Implements the L-BFGS algorithm for quasi-Newton optimization with limited memory storage. Uses two-loop recursion to compute search directions efficiently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
potential
|
Potential with calc_energy/calc_forces methods |
required | |
maxstep
|
float
|
Maximum step size (as displacement norm) |
0.04
|
memory
|
int
|
Number of previous steps to store for Hessian approximation |
20
|
damping
|
float
|
Damping factor for step size |
1.0
|
entity_type
|
Type of entity to optimize |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
maxstep |
Maximum allowed step size |
|
memory |
LBFGS memory size |
|
damping |
Step damping factor |
|
s_history |
list[ndarray]
|
Position difference history |
y_history |
list[ndarray]
|
Gradient difference history |
rho_history |
list[float]
|
Curvature history (1 / y·s) |
Example
from molpy.core.atomistic import Atomistic from molpy.potential.bond import Harmonic from molpy.optimize import LBFGS
struct = Atomistic()
... add atoms and bonds ...¶
potential = Harmonic(k=100.0, r0=1.5) opt = LBFGS(potential, maxstep=0.04, memory=20) result = opt.run(struct, fmax=0.01, steps=500)
Source code in src/molpy/optimize/lbfgs.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
step ¶
step(structure)
Perform one L-BFGS optimization step.
Args: structure: Structure to optimize (modified in-place)
Returns:
| Type | Description |
|---|---|
tuple[float, float]
|
(energy, fmax) tuple where: energy: potential energy after step fmax: maximum force component after step |
Source code in src/molpy/optimize/lbfgs.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
Potential Wrappers¶
Potential wrappers for optimizer compatibility.
Since potentials in molpy work with raw arrays (positions, indices, types), not Frame objects, we need simple wrappers to extract data from Frames.
Each wrapper implements calc_energy(frame) and calc_forces(frame) by extracting the appropriate data and calling the underlying potential.
AnglePotentialWrapper ¶
AnglePotentialWrapper(potential)
Wrapper for angle potentials to work with Frame interface.
Extracts (r, angle_idx, angle_types) from Frame and calls underlying potential.
Source code in src/molpy/optimize/potential_wrappers.py
55 56 57 58 | |
calc_energy ¶
calc_energy(frame)
Extract angle data from Frame and compute energy.
Source code in src/molpy/optimize/potential_wrappers.py
60 61 62 63 64 65 66 67 68 69 70 | |
calc_forces ¶
calc_forces(frame)
Extract angle data from Frame and compute forces.
Source code in src/molpy/optimize/potential_wrappers.py
72 73 74 75 76 77 78 79 80 81 82 | |
BondPotentialWrapper ¶
BondPotentialWrapper(potential)
Wrapper for bond potentials to work with Frame interface.
Extracts (r, bond_idx, bond_types) from Frame and calls underlying potential.
Source code in src/molpy/optimize/potential_wrappers.py
19 20 21 22 | |
calc_energy ¶
calc_energy(frame)
Extract bond data from Frame and compute energy.
Source code in src/molpy/optimize/potential_wrappers.py
24 25 26 27 28 29 30 31 32 33 34 | |
calc_forces ¶
calc_forces(frame)
Extract bond data from Frame and compute forces.
Source code in src/molpy/optimize/potential_wrappers.py
36 37 38 39 40 41 42 43 44 45 46 | |