IMLCV.base.CV#

Module Contents#

Classes#

SystemParams

NeighbourList

CV

CvMetric

class to keep track of topology of given CV. Identifies the periodicitie of CVs and maps to unit square with correct peridicities

CvFunInput

CvFunBase

CvFun

CvFunNn

used to instantiate flax linen CvTrans

CvFunDistrax

creates bijective CV function based on a distrax flow. The seup function should initialize the bijector

CombinedCvFun

CvTrans

f can either be a single CV tranformation or a list of transformations

CvTransNN

Base class for all neural network modules. Layers and models should subclass this class.

NormalizingFlow

normalizing flow. _ProtoCvTransNN are stored separately because they need to be initialized by this module in setup

CvFlow

CollectiveVariable

class IMLCV.base.CV.SystemParams[source]#
property batched[source]#
property shape[source]#
coordinates: jax.Array[source]#
cell: Array | None[source]#
__post_init__()[source]#
__getitem__(slices) SystemParams[source]#
__iter__()[source]#
__add__(other)[source]#
__str__()[source]#

Return str(self).

batch() SystemParams[source]#
unbatch() SystemParams[source]#
angles(deg=True) jax.Array[source]#
_get_neighbour_list(r_cut, r_skin: float = 0.0, z_array: tuple[int] | None = None, z_unique: tuple[int] | None = None, num_z_unique: tuple[int] | None = None, num_neighs: int | None = None, nxyz: tuple[int] | None = None) tuple[bool, NeighbourList | None][source]#
get_neighbour_list(r_cut, z_array: list[int] | Array, r_skin=0.0) NeighbourList | None[source]#
minkowski_reduce() tuple[SystemParams, jax.Array][source]#

base on code from ASE: https://wiki.fysik.dtu.dk/ase/_modules/ase/geometry/minkowski_reductsp.cellion.html#minkowski_reduce

wrap_positions(min=False) tuple[SystemParams, jax.Array][source]#

wrap pos to lie within unit cell

static _wrap_pos(cell: Array | None, coordinates: jax.Array, min=False) tuple[jax.Array, jax.Array][source]#
canoncialize(min=False) tuple[SystemParams, jax.Array, jax.Array][source]#
min_distance(index_1, index_2)[source]#
class IMLCV.base.CV.NeighbourList[source]#
property batched[source]#
property shape[source]#
property num_neigh[source]#
r_cut: jax_dataclasses.Static[jax.numpy.floating][source]#
atom_indices: jax.Array[source]#
op_cell: Array | None[source]#
op_coor: Array | None[source]#
op_center: Array | None[source]#
r_skin: jax_dataclasses.Static[jax.numpy.floating][source]#
sp_orig: SystemParams | None[source]#
ijk_indices: Array | None[source]#
nxyz: jax_dataclasses.Static[tuple[int] | None][source]#
z_array: jax_dataclasses.Static[tuple[int] | None][source]#
z_unique: jax_dataclasses.Static[tuple[int] | None][source]#
num_z_unique: jax_dataclasses.Static[tuple[int] | None][source]#
_pos(sp_orig)[source]#
apply_fun_neighbour(sp: SystemParams, func, r_cut=None, fill_value=0, reduce='full', split_z=False, exclude_self=False)[source]#
apply_fun_neighbour_pair(sp: SystemParams, func_double, func_single=None, r_cut=None, fill_value=0, reduce='full', split_z=False, exclude_self=True, unique=True)[source]#

Args:#

func_single=lambda r_ij, atom_index_j: (1,), func_double=lambda r_ij, atom_index_j, data_j, r_ik, atom_index_k, data_k: (

r_ij, atom_index_j, r_ik, atom_index_k,

),

__getitem__(slices)[source]#
update(sp: SystemParams) tuple[bool, NeighbourList][source]#
nl_split_z(p)[source]#
static match_kernel(p1: jax.Array, p2: jax.Array, nl1: NeighbourList, nl2: NeighbourList, matching='REMatch', alpha=0.01, mode='divergence', jit=True)[source]#
__add__(other)[source]#
static stack(*nls: NeighbourList) NeighbourList[source]#
class IMLCV.base.CV.CV[source]#
property batched[source]#
property batch_dim[source]#
property dim[source]#
property size[source]#
property shape[source]#
property combine_dims[source]#
property stack_dims[source]#
cv: jax.Array[source]#
mapped: jax_dataclasses.Static[bool] = False[source]#
atomic: jax_dataclasses.Static[bool] = False[source]#
_combine_dims: jax_dataclasses.Static[list | None][source]#
_stack_dims: jax_dataclasses.Static[list | None][source]#
__add__(other) CV[source]#
__radd__(other) CV[source]#
__sub__(other) CV[source]#
__rsub__(other) CV[source]#
__mul__(other) CV[source]#
__rmul__(other) CV[source]#
__matmul__(other) CV[source]#
__rmatmul__(other) CV[source]#
__div__(other) CV[source]#
__rdiv__(other) CV[source]#
batch() CV[source]#
__iter__()[source]#
__getitem__(idx)[source]#
unbatch() CV[source]#
static stack(*cvs: CV) CV[source]#

stacks a list of CVs into a single CV. The dimenisions are stored such that it can later be unstacked into separated CVs. The CVs are stacked over the batch dimension

unstack() list[CV][source]#
split(flatten=False) list[CV][source]#

inverse operation of combine

static combine(*cvs: CV, flatten=False) CV[source]#

merges a list of CVs into a single CV. The dimenisions are stored such that it can later be split into separated CVs. The CVs are combined over the last dimension

class IMLCV.base.CV.CvMetric(periodicities, bounding_box=None)[source]#

class to keep track of topology of given CV. Identifies the periodicitie of CVs and maps to unit square with correct peridicities

property ndim[source]#
norm(x1: CV, x2: CV, k=1.0)[source]#
periodic_wrap(x: CV, min=False) CV[source]#
difference(x1: CV, x2: CV) jax.Array[source]#
min_cv(cv: jax.Array)[source]#
__periodic_wrap(xs: jax.Array, min=False)[source]#

Translate cvs such over unit cell.

min=True calculates distances, False translates one vector inside box

map(x: jax.Array, displace=True) jax.Array[source]#

transform CVs to lie in unit square.

unmap(x: jax.Array, displace=True) jax.Array[source]#

transform CVs to lie in unit square.

__add__(other)[source]#
grid(n, endpoints=None, margin=None)[source]#

forms regular grid in mapped space. If coordinate is periodic, last rows are ommited.

Parameters:
  • n – number of points in each dim

  • map – boolean. True: work in mapped space (default), False: calculate grid in space without metric

  • endpoints – if

Returns:

meshgrid and vector with distances between points

class IMLCV.base.CV.CvFunInput[source]#
input: int[source]#
conditioners: list[int] | None[source]#
split(x: CV)[source]#
combine(x: CV, res: CV)[source]#
class IMLCV.base.CV.CvFunBase[source]#
_: dataclasses.KW_ONLY[source]#
cv_input: CvFunInput | None[source]#
calc(x: CV, nl: NeighbourList | None, reverse=False, log_det=False) tuple[CV, Array | None][source]#
abstract _calc(x: CV, nl: NeighbourList | None, reverse=False, conditioners: list[CV] | None = None) CV[source]#
_log_Jf(x: CV, nl: NeighbourList | None, reverse=False, conditioners: list[CV] | None = None) tuple[CV, Array | None][source]#

naive automated implementation, overrride this

class IMLCV.base.CV.CvFun[source]#

Bases: CvFunBase

forward: Callable[[CV, NeighbourList | None, CV | None], CV] | None[source]#
backward: Callable[[CV, NeighbourList | None, CV | None], CV] | None[source]#
_calc(x: CV, nl: NeighbourList | None, reverse=False, conditioners: list[CV] | None = None) CV[source]#
class IMLCV.base.CV.CvFunNn[source]#

Bases: flax.linen.Module, CvFunBase

used to instantiate flax linen CvTrans

abstract setup()[source]#

Initializes a Module lazily (similar to a lazy __init__).

setup is called once lazily on a module instance when a module is bound, immediately before any other methods like __call__ are invoked, or before a setup-defined attribute on self is accessed.

This can happen in three cases:

  1. Immediately when invoking apply(), init() or init_and_output().

  2. Once the module is given a name by being assigned to an attribute of another module inside the other module’s setup method (see __setattr__()):

    class MyModule(nn.Module):
      def setup(self):
        submodule = Conv(...)
    
        # Accessing `submodule` attributes does not yet work here.
    
        # The following line invokes `self.__setattr__`, which gives
        # `submodule` the name "conv1".
        self.conv1 = submodule
    
        # Accessing `submodule` attributes or methods is now safe and
        # either causes setup() to be called once.
    
  3. Once a module is constructed inside a method wrapped with compact(), immediately before another method is called or setup defined attribute is accessed.

_calc(x: CV, nl: NeighbourList | None, reverse=False, conditioners: list[CV] | None = None) CV[source]#
abstract forward(x: CV, nl: NeighbourList | None, conditioners: list[CV] | None = None) CV[source]#
abstract backward(x: CV, nl: NeighbourList | None, conditioners: list[CV] | None = None) CV[source]#
class IMLCV.base.CV.CvFunDistrax[source]#

Bases: flax.linen.Module, CvFunBase

creates bijective CV function based on a distrax flow. The seup function should initialize the bijector

class RealNVP(CvFunDistrax):

_: dataclasses.KW_ONLY latent_dim: int

def setup(self):

self.s = Dense(features=self.latent_dim) self.t = Dense(features=self.latent_dim)

# Alternating binary mask. self.bijector = distrax.as_bijector(

tfp.bijectors.RealNVP(

fraction_masked=0.5, shift_and_log_scale_fn=self.shift_and_scale,

)

)

def shift_and_scale(self, x0, input_depth, **condition_kwargs):

return self.s(x0), self.t(x0)

bijector: distrax.Bijector[source]#
abstract setup()[source]#

setups self.bijector

_calc(x: CV, nl: NeighbourList | None, reverse=False, log_det=False, conditioners: list[CV] | None = None) CV[source]#
_log_Jf(x: CV, nl: NeighbourList | None, reverse=False, conditioners: list[CV] | None = None) tuple[CV, Array | None][source]#

naive implementation, overrride this

class IMLCV.base.CV.CombinedCvFun[source]#

Bases: CvFunBase

classes: list[list[CvFunBase]][source]#
calc(x: CV, nl: NeighbourList | None, reverse=False, log_det=False) tuple[CV, Array | None][source]#
abstract _log_Jf(x: CV, nl: NeighbourList | None, reverse=False, conditioners: list[CV] | None = None) tuple[CV, Array | None][source]#

naive automated implementation, overrride this

class IMLCV.base.CV.CvTrans[source]#

f can either be a single CV tranformation or a list of transformations

trans: list[CvFunBase][source]#
static from_array_function(f: collections.abc.Callable[[jax.Array, NeighbourList | None, None], jax.Array])[source]#
static from_cv_function(f: collections.abc.Callable[[CV, NeighbourList | None, CV | None], CV]) CvTrans[source]#
static from_cv_fun(proto: CvFunBase)[source]#
compute_cv_trans(x: CV, nl: NeighbourList | None = None, reverse=False, log_Jf=False) tuple[CV, Array | None][source]#

result is always batched arg: CV

__mul__(other)[source]#
__add__(other: CvTrans) CvTrans[source]#
static stack(*cv_trans: CvTrans)[source]#
class IMLCV.base.CV.CvTransNN[source]#

Bases: flax.linen.Module, CvTrans

Base class for all neural network modules. Layers and models should subclass this class.

All Flax Modules are Python 3.7 dataclasses. Since dataclasses take over __init__, you should instead override setup(), which is automatically called to initialize the module.

Modules can contain submodules, and in this way can be nested in a tree structure. Submodels can be assigned as regular attributes inside the setup() method.

You can define arbitrary “forward pass” methods on your Module subclass. While no methods are special-cased, __call__ is a popular choice because it allows you to use module instances as if they are functions:

from flax import linen as nn

class Module(nn.Module):
  features: Tuple[int, ...] = (16, 4)

  def setup(self):
    self.dense1 = Dense(self.features[0])
    self.dense2 = Dense(self.features[1])

  def __call__(self, x):
    return self.dense2(nn.relu(self.dense1(x)))

Optionally, for more concise module implementations where submodules definitions are co-located with their usage, you can use the compact() wrapper.

trans: list[CvFunBase][source]#
setup() None[source]#

Initializes a Module lazily (similar to a lazy __init__).

setup is called once lazily on a module instance when a module is bound, immediately before any other methods like __call__ are invoked, or before a setup-defined attribute on self is accessed.

This can happen in three cases:

  1. Immediately when invoking apply(), init() or init_and_output().

  2. Once the module is given a name by being assigned to an attribute of another module inside the other module’s setup method (see __setattr__()):

    class MyModule(nn.Module):
      def setup(self):
        submodule = Conv(...)
    
        # Accessing `submodule` attributes does not yet work here.
    
        # The following line invokes `self.__setattr__`, which gives
        # `submodule` the name "conv1".
        self.conv1 = submodule
    
        # Accessing `submodule` attributes or methods is now safe and
        # either causes setup() to be called once.
    
  3. Once a module is constructed inside a method wrapped with compact(), immediately before another method is called or setup defined attribute is accessed.

compute_cv_trans(x: CV, nl: NeighbourList | None, reverse=False, log_Jf=False) tuple[CV, Array | None][source]#

result is always batched arg: CV

__mul__(other)[source]#
class IMLCV.base.CV.NormalizingFlow[source]#

Bases: flax.linen.Module

normalizing flow. _ProtoCvTransNN are stored separately because they need to be initialized by this module in setup

flow: CvTransNN | CvTransNN[source]#
setup() None[source]#

Initializes a Module lazily (similar to a lazy __init__).

setup is called once lazily on a module instance when a module is bound, immediately before any other methods like __call__ are invoked, or before a setup-defined attribute on self is accessed.

This can happen in three cases:

  1. Immediately when invoking apply(), init() or init_and_output().

  2. Once the module is given a name by being assigned to an attribute of another module inside the other module’s setup method (see __setattr__()):

    class MyModule(nn.Module):
      def setup(self):
        submodule = Conv(...)
    
        # Accessing `submodule` attributes does not yet work here.
    
        # The following line invokes `self.__setattr__`, which gives
        # `submodule` the name "conv1".
        self.conv1 = submodule
    
        # Accessing `submodule` attributes or methods is now safe and
        # either causes setup() to be called once.
    
  3. Once a module is constructed inside a method wrapped with compact(), immediately before another method is called or setup defined attribute is accessed.

calc(x: CV, nl: NeighbourList | None, reverse: bool, test_log_det=False)[source]#
class IMLCV.base.CV.CvFlow(func: collections.abc.Callable[[SystemParams, NeighbourList | None], CV], trans: CvTrans | None = None)[source]#
static from_function(f: collections.abc.Callable[[SystemParams, NeighbourList | None], jax.Array], atomic=False) CvFlow[source]#
compute_cv_flow(x: SystemParams, nl: NeighbourList | None = None, jit=True, chunk_size: int | None = None) CV[source]#
__add__(other)[source]#
__mul__(other)[source]#
find_sp(x0: SystemParams, target: CV, target_nl: NeighbourList, nl0: NeighbourList | None = None, maxiter=10000, tol=0.0001, norm=lambda cv1, cv2, nl1, nl2: ..., solver=jaxopt.GradientDescent) SystemParams[source]#
class IMLCV.base.CV.CollectiveVariable(f: CvFlow, metric: CvMetric, jac=jacrev)[source]#
property n[source]#
_jit_f(sp, nl)[source]#
_jit_df(sp, nl)[source]#
compute_cv(sp: SystemParams, nl: NeighbourList | None = None, jacobian=False, jit=True, chunk_size: int | None = None) tuple[CV, CV][source]#