Skip to content

TT-MetaD#1385

Open
strandn wants to merge 8 commits intoplumed:masterfrom
strandn:ttsketch
Open

TT-MetaD#1385
strandn wants to merge 8 commits intoplumed:masterfrom
strandn:ttsketch

Conversation

@strandn
Copy link
Copy Markdown

@strandn strandn commented Mar 26, 2026

Description

This pull request involves an implementation of tensor train (TT)-metadynamics (TT-MetaD). Details of the method can be found in https://arxiv.org/abs/2603.13549. The relevant code is in the "ttsketch" module (src/ttsketch). Instructions for setup and compilation are written in at the top of src/ttsketch/TTMetaD.cpp.

Target release

Latest possible version (master/v2.11?).

Type of contribution
  • changes to code or doc authored by PLUMED developers, or additions of code in the core or within the default modules
  • changes to a module not authored by you
  • new module contribution or edit of a module authored by you
Copyright
  • I agree to transfer the copyright of the code I have written to the PLUMED developers or to the author of the code I am modifying.

  • the module I added or modified contains a COPYRIGHT file with the correct license information. Code should be released under an open source license. I also used the command cd src && ./header.sh mymodulename in order to make sure the headers of the module are correct.

Tests
  • I added a new regtest or modified an existing regtest to validate my changes.
  • I verified that all regtests are passed successfully on GitHub Actions.

@GiovanniBussi
Copy link
Copy Markdown
Member

@strandn thanks for your contribution! I noticed there are changes in the core (src/tools) can you cross check? I guess you should rebase on master or anyway solve the conflicts

@Iximiel
Copy link
Copy Markdown
Member

Iximiel commented Mar 27, 2026

Thansk for your contribution!

I am commenting with a sledgehammer: brace for impact.

First of all, the Matrix change is not necessary-ish: usually compilers initialize to zero, but I appreciate your lack in trust about that: it is a good practice. But I think it needs to be refined a little (to be pedantic: T val=T(0);) and moved in a separate PR, to keep the scope of this PR narrow on your TT-metad.

The change in Tools.h has already been fixed, but it is not a problem: a rebase will remove the modification.

I would like to ask you to do a small compacting of your code history, so that it will be easier to rebase and apply the new astyle. Luckyly you never merged master so it should be smooth. How much are you confortable with git?

There are some smaller details, like the external dependency on ITensor, or the "using namespace x;" things, but we will walk you trough those later

@GiovanniBussi
Copy link
Copy Markdown
Member

First of all, the Matrix change is not necessary-ish: usually compilers initialize to zero, but I appreciate your lack in trust about that: it is a good practice. But I think it needs to be refined a little (to be pedantic: T val=T(0);) and moved in a separate PR, to keep the scope of this PR narrow on your TT-metad.

I am actually fixing this separately. I think it is a genuine bug, the point is that these functions are not regtested currently

GiovanniBussi added a commit that referenced this pull request Mar 27, 2026
See one of the changes in #1385

To be backported
some implementation progress

minor

minor

more progress

function declarations in ttsketch class

minor

change include orders

added keys and initializations to constructor

progress on plumed-related functions

all bare functionalities implemented (without parallelization)--compilation not attempted yet

edit .gitignore

Delete .vscode directory

replace eigen with internal plumed matrix functionality

few fixes

minor

typo

fix compilation errors

fix warnings

fix CONV flag related issue

change STRIDE to SAMPLESTRIDE

small bug

another small bug

debugging prints

use cout instead

gsl eps bug

more cout statements

flush stdout

small bug in update

minor

add couts back

add prints in parasketch

add prints in createttcoeff

reduce number of prints

clean prints

bugs with bias derivative and vshift

some more small fixes, plus more prints for debugging purposes

minor

comment out prints

add some extra debugging prints for visualizing bias potential

need to enable convolution immediately after sketch algorithm

minor

minor

add count feature

typo

bug fix

modify location of count print

another small bug

still more related bugs

small fix--hopefully works now

another tiny fix

fixes related to vshift

minor

prints in histogram file

revert prints

clean up

additional output regarding gradtop

widen integration limits for Gaussian smoothing

bug with min and max CV values

typo

further fix Gaussian integration if variables are periodic

minor

debug 4D case

minor

less output

oops

output in envi R part

more refined output

more print adjustments

omit details from output

more output changes

fix bug related to rank

revert changes

switch back to new version

get rid of vbias dumping

change integration limits

update default quadrature params

minor

several changes; need to improve ACA algorithm

minor

massive changes

some more progress

further progress

ready to test code

compilation fixes

small fixes

minor

add parallelization

small output change

debugging output

minor bug fix

more debugging

minor

another fix

yet more debugging

more output

even more output

another fix

more prints

another fix?

minor

idk why gsl is failing

fixed issue with epsabs

that didnt fix the problem; lets examine the integrand function more closely

output integrand more finely

play around with key

try key=4 now

try relaxing tolerance

try epsabs=1e-7

change back to 1e-8, see if other things can cause improvement

try qags integration

switch to 1e-6

try decreasing epsrel

change epsabs too

will use plumed input file to tune params

disable gsl error handle

add include

new thing to debug

issue with update vb again

minor

debuggging prints

i think its fixed now

remove debugging prints

write vbias to separate file instead of stdout

temporarily disable omp

uncomment omp lines

separate gsl workspace for each thread

vbias append to file

minor

typo

ok should work now

output vbias with original basis

small fix

fix issue with vshift

minor clean up

modifications in vbias

fix minor issues

minor

change the way vbias is outputted

another version

revert to old version

minor

comment out vbias printing

try slight modification in f

added h5 support+write TT to disk

output arg labels to file

debug outputting

change prints

this should fix it

also need to store samples

minor fixes

minor

resolve bug--can only output ITensor objects to HDF5

typo

another typo

minor

yet another small fix

more debugging output

add more debugging statemement

should hopefully fix the problem

hangs--add print statements again

this should do the trick

different bug fix

clean up

change yet again to new version, include append change

add some log output

minor

nsamples should start from 0 instead of 1

fix potential indexing problem

minor

might not need in the end

progress on fes code

a bit more progress

first attempt implemented

fix compilation issues

issue with inheritance

try multiple inheritance

some more fix attempts

minor

add component

debugging output

some more debugging prints

typo

should not use default getStride()

oops

forgot to add key

fixed--remove debugging output

free energy must do -ktlog

minor

fix input stuff

minor

maybe better now?

small fix

minor

try update instead of calculate

minor

revert to old version

don't use all samples if not using final vb

missing comma

a few changes

add some debugging output

add parallelization

add include

remove output

modify f, i think vbias 0 introduces errors

change back

test disabling omp when iterating through samples

never mind

try nomax version

change f again

move parsing of keys elsewhere

print stuff to file to fix segfault

semicolon

dont check filename unless restart

minor

remove output stuff

fix file input

one more fix

confused about error--add some prints

hopefully this fixes it

minor

big revert back to old formalism

small fixes

add macros

move implementation to cpp file

commit cpp file

cpp not cc

bug with wt

try adding support for multiple walkers

fix minor issue

some improvements in restart part

debugging output

some more output

bug fix?

change output again

lets see what all_traj looks like

this should do it

change output back

one more fix

ok now it should work

minor

somethings wrong--add debugging output

minor

output to log instead of stdout

flush more often

changes to output

more detailed output about vector sizes

some more output

still more output

something is wrong with h5 write

output to cerr instead

serialize file read

write to file

minor

small fix

confusing

minor

exrta output

small

try using gdb

print to log instead

temporarily disable ttWrite and ttRead

typo

uncomment write

print contents of tt

move barrier

fix

typo

try putting h5 in sub directory

temporarily comment out h5_write

now comment out close

commenting out close(f) fixes it?

no real need for barriers

comment out Vbias printing

bug fix

make WIDTH a vector

added ability to output adjusted vbias with smaller values

bug in adj bias

more fixing

typo

forgot to multiply adj vmax by kt

fix bug

try new adj vbias version

many changes, including trimming now before linsolve, and added covmat calculations

some compilation fixes

this is better maybe?

flush log a few times

add a bunch of debugging prints

additional prints

possible fix

somethings wrong with Pinv?

more output

cant matrixout to cout

print vbias for aladip

small bug

check mu

quick check

minor

some clean up

comment out printing of vbias

print integrals

print tt

i think this fixes integrals

clean up

typo

fix bug with empirical cov

implementation attempt for fast aca

compilation fixes

minor

a few more small changes

shift to -2kT

separate conv for vb and g

minor

improvements to quadrature grid

minor

make grid finer

comment out printing of F

minor

minor output tweaks

never mind

add option to not include all samples in aca calculations

add feature to print ttcross errors

minor

another small fix

typo

try reducing number of grid points

bug in l2 error calculations

need to print some stuff

need to take sqrt

try 5x instead of 2x

shift f from -2kT to 0

minor

dont compute vshift on restart if aca

some clean up

forgot to add samples to aca object when restarting

if error, decrease count by 1

need to check for errors multiple times

needed to check reading of tt as well

major changes--mainly memorystride and vshift

minor

small fix

1-indexing

minor

fix gradient filenames

further fix io related stuff

only barrier if multiple walkers

some debugging output

bug fix

6d bias output for debugging

minor

finer print

change limits of integration for convolution

remove vbias output

uncomment vshift output

more vbias output

comment out output

enable output

small fix

comment out output

output for 3d and 4d bias

minor

comment out vbias output

something is wrong with restart, adding some debugging output

change location of output

somehow it works

minor output

flush output

some more output

add barrier maybe

more output

more changes to output

minor

maybe fix?

minor

this should do it

add vbias output for 6d calculations

adj vbias

minor

vbias output for aladip

all ranks should output vbias

minor

change position of vbias output

also print adj vbias

minor

fix restart portion

comment out vbias printing

slight change with adj vmax

vbias is now sum of log ttsketch again

bug fix

printing of 2d marginals

typo

debugging output

minor

typo

more output

more output

refined output

should fix

filename location change

filename change

small clean up

fix the way vshift and adj vshift is handled on restart

change convergence scheme for aca

oops

when aca, search through grid samples

minor revert

another small revert

never mind

small fix

another small fix

print additional files that have gaussian kernels included

also print 2d vbias

typos

move print code to right placee

modify metad file to print vbias cross sections

vscode is being weird

include fstream

clean up mtd

minor

only print vbias stuff for ala4

fix ttcross convergence check

minor

typo

somethings wrong

undo some small changes

confused

another fix attempt

minor

remove extra output again

oops

rearrange code

re rearrange

revert cross to fix issue

add erased function

small fix

big changes--revert ttcross and implement gaussian basis

fix redundant declaration

fix ttsketch conv output

gaussian marginals implementation

small fix+clean up

fix 2d marginal output + more gaussian basis clean up

minor fix

should be able to run with gaussian basis now

fix some more output

accidentally removed code

something is wrong

this should do it

minor

added feature to compute l2 errors for each aca rank

fix errors

more small fixes

set upper limit on aca rank

invert/pseudoinvert fix

add debugging output

indexing seems to be off by 1 in some places

comment out prints

rank scheme redundance--simplify

check if res is 0 and if so break

add a lot of debugging output for troubleshooting kernel basis

fix

only mpi rank 0 should print

rearrange some code

minor

also need to only print for one k

move code around

some more output

oops

nbasis fix

typo

another typo?

exclude redundant gaussian

int2 corrections

try pseudoinverse?

multiply by ginv after cross too

change res new criterion

sump p implementation

small fix

typo in keyword registering

initialize sump height

forgot to write vb to disk

some debugging output

oops

print tt

see if nonzero ranks are trying to read prematurely

try adding barrier right after ttsumwrite

move variable declarations around

lets try something

need to append to h5 file

something is wrong with pluseq--debug

match site indices

minor

add some more useful output about ranks

oops

forgot to account for vshift

oops

try diff aca version

debugging output

more debugging

oops

more detailed output

this should fix it

minor

revert to old aca

minor

output info from ala3 vbias, sketch and mtd

include fstream

some more fixes

comment out metad code

check quality of ginv

correct matrix mult

fix output

oops

try increasing kernel basis bandwidth

remove bandwidth increase

clean up

vshift got excluded from sump vbias for some reason

comment out some vbias printing code

fix bf output for sump

minor fix

add vbias printing for 4d

comment out mtd code

slight tweak in cross convergence

slightly more

revert

cross change seems to be problematic

try compiling ttmetad

fix issues

one more small thing

some more small changes

oops

some debugging output

fix

oops

cant print vectors

try again

minor

another possible fix

even more debugging output

minor typo

test with delta

oops

test something

go back to gaussians

hard code rank

try fourier basis

clean up

use gaussians instead of dirac delta for sump

typo

fix output

can still check empirical stats

try printing error info

minor

test with dirac again

go back to gaussian

try smoothing with tiny bandwidth

separate bandwidth for gaussian sum

sigma needs to be a vector

minor fix

this should likely fix

indexing oops

debugging output

oops

segfault fix

one more fix

test hard code false

fix ttmetad

small fixes

now force true

change back

small fix

try analytical formula for conv

typo

compare two methods

oops

missing factor

typo

never mind

damn integer division

remove all numerical integration

minor fix

freeze bias potential after some time

typo

small changes

minor

minor

add vbias outputting for 3d bias

check if vbias is the same across walkers

undo

oops

some more useful output

undo again

output metad vbias

also rct

clean up

minor

comment out code instead

minor

lots of metad testing

clean up

frozen param redundant

oops

clean up once again

oops again

clean up

actually keep modifications a bit longer

set up non mw

clean again

multithreading added

minor

minor

this is better

debugging output

typo

more test

oops

more detailed test

even more detail

this should fix it

multithreading for ttcross

fix

minor

minor

minor

comment out vbias output

output for 6d bias

minor

comment out temporarily

print metad bias for each walker every now and then

barrier

one more barrier

one more thing

comment out

6d vbias output for mtd

change output temporarily

undo

improved gaussian sketch

fixes

minor

slight restructuring

typo

another fix

some more debugging output

yet another fix

minor

clean up

minor

vbias output for 6d

oops

clean up

bug with kernel width

add restart option

temporarily comment out metad output

add back

oops

change pace

better randomization

corrections

minor

more small changes

seed rng

better seeding

clean up

clean up

major clean up

add stopwatch

minor

try kernel basis with ttmetad

oops

oops again

should not sketch vb with ginv

temporarily uncomment vbias printing

comment again

improvements in kernel sketching

print vbias

clean up

improvement in aca

minor

improved quadrature

oops

vbias printing again

revert nbins

improvements in sample choice for aca

add pivot I/O

should fix issue

minor

oops

clean

minor improvement to ttsketch

minor

another tweak

minor

some restart related tweaks

minor

add option to use diff basis for aca than sketch

oops

typo

major changes to ttsketch

minor fixes

oops

initialize files earlier

try closing file before opening again

oops

reset each count

flush and close?

some debugging

minor

more detailed debugging output

rearrange code

some clean up

flush and close

more changes

minor

several other changes

minor fix

tiny fix

more improvements

weird error

this should fix it

improvements to restart part

one more thing in restart

minor

revert ttcross

need to initialize value ptrs

debugging output

empty traj if restarting from step 0

never mind

stopwatch fix

fix

clean up

file I/O for TTMetaD

typo

minor

kernel smoothing option for TTMetaD

fixes

only rank 0 process should write to file

minor

try rewind file

this should fix?

this should be better

oops

minor

roll back

fix ttread

debugging ttcross

some more debugging

minor fix

better version

lots of debugging output

some more output

oops

try rewind?

slight improvement

further improvements to file io

minor fix

clean up + nbasis for ttcross

minor

minor

fix file io

debugging output

oops

more detailed output

same oops

ugh

fix

revert

this is best i think

minor

enable vbias printing for ala4

option to specify kernel basis width

minor fix

vbias printing for aladip

oops

minor

oops

silly error

disable output

stopwatch stuff

minor

stopwatch fix

experiment with no gaussians in getbias

temporarily uncomment

fix A0

oops

indexing issue

revert

prepare for aladip

enable stopwatch

initial ttopes implementation

fixes

minor

output vbias during opes

add include

vbias output for ttopes

need to register outputs

debugging output

diff output

more refined intbasis output

some traj output

fix?

clean up

print density

fix

add grad output

minor

minor

nonintrusive version implemented

fix segfault?

debugging output

matching index fix attempt

more debugging output

this should do it?

wrong function names

fix A matrices too

1 indexed?

vbias printing for ala4

non negative least squares attempt

namespace clash

minor fixes

oops

minor

debugging output

more debugging

fix

more output

refined debugging

another fix

check matrices

oops

output extra info

minor

small fix

print nnls info

oops

typo

more detailed output

try positive sketch

try ommitting ginv

try least squares without positivity constraint

declaration issue

oops

least squares using full matrices

fixes

minor

try this?

should be good now

ok now

hmm

small revert

clean up

minor

regularize gram matrix

added clipping

pseudoinverse of ginv

try regularizing ginv

adjust lambda

try smaller lambda now

revert to 1e-6

revert to pinv for ginv and solve ls for first core instead

oops

minor

try fourier

revert

oops

try kernel without gram

revert again

try slightly different scheme

try fourier again

correct bias based on gradient magnitude

change grad threshold

1e3

try 100

revert

bias needs to be something else at the start

1e4

1e3

revert again

output some bias and gradient info

step too

typo

new p threshold

1e-3

oops

minor

set up ttmetad branch

itensor config file

minor

started cleanup

comments added

copyright stuff

ttmetad regtest

ran regtest

minor

Update src/ttsketch/TTMetaD.cpp

assign instead of resize

Co-authored-by: Daniele <Iximiel@users.noreply.github.com>
} else if(pos % 2 == 0) {
return -pow(1 / this->L_, 1.5) * M_PI * (pos / 2) * sin(M_PI * (x - this->shift_) * (pos / 2) / this->L_);
} else {
return pow(1 / this->L_, 1.5) * M_PI * (pos / 2) * cos(M_PI * (x - this->shift_) * (pos / 2) / this->L_);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

pos/2 if odd should be floored or should be something.5?

in case it should be something.5 use *0.5 for speed (multiplication is generally faster that division) and correctness ((3/2)=1, (3/2.0)=1.5, 3*0.5=1.5)
ad the .5 multiplication can also be used in the sin expression (and in other places in the file, check if you want a floating point result or an integer result case per case)
consider also to precompute 1/L, and/or the whole pow constant pow(1 / this->L_, 1.5) * M_PI : you will save quite a lot time

@Iximiel
Copy link
Copy Markdown
Member

Iximiel commented Mar 31, 2026

thanks @strandn! While we wait for the workflow to be approved I would like to ask you some cosmetic changes, (we need to update this part in the developer manual...):

  • You should add a few files (see for example metatomic):
    • you should move the installation instructions in a module.md
    • you should create a module.yml with some information about you and your TT-metad
  • Plumed has a "cite" function you can use to cite your paper and your sources (like ITensor and so on): this->cite(ref) in the ctor of the action
  • be prepared to write std::vector a lot. You are using using namespace std; and codecheck will not pass it
  • we will probably ask you to modify the configure.ac with the necessary settings to setup plumed with ITensor and add a small script to install it on the CI, so that your TT-metad can be tested there. I'll help you with those, if needed

@Iximiel
Copy link
Copy Markdown
Member

Iximiel commented Apr 1, 2026

@strandn

The ci will not work until we setup the installation process for ITensor.

please run make astyle

First of all:

You should add a few files (see for example metatomic):

* you should move the installation instructions in a `module.md`

* you should create a `module.yml` with some information about you and your TT-metad

You should start by adding in the .ci directory a script for installing ITensor (see the arrayfire one), maybe write an ad-hoc options.mk for that. Then you should make the linux runner of the ci call it in the -mpi- jobs (you'll need to correct the varous /path/to).
A possible step to add (below the pytorch one) is the following (untested):

    - name: Configure ITensor
      if: ${{ contains( matrix.variant, '-mpi-' ) }} 
      run: |
        echo "LDFLAGS=$LDFLAGS -L/path/to/ITensor/lib" >> $GITHUB_ENV
        echo "CPPFLAGS=$CPPFLAGS -I/path/to/ITensor -DITENSOR_USE_HDF5 -DITENSOR_USE_OMP -D__ITENSOR_LAPACK_WRAP_h" >> $GITHUB_ENV

        #temporary, the following lines will be addresses in configure.ac
        echo "CXXFLAGS=\"-O3 -fconcepts\"" >> $GITHUB_ENV
        echo "LIBS=\"-litensor -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -lhdf5 -lhdf5_hl\"" >> $GITHUB_ENV
        # this will be uncommented after the configure.ac modifications
        #echo "PLUMED_CONFIG=$PLUMED_CONFIG --enable-libitensor" >> $GITHUB_ENV

Probably the mkl libraries will need to be removed from the CI, since those are configuration dependent.

Tell me if you need any help to setup this

Edit:
https://rhysd.github.io/actionlint/ is a good tool for checking if the workflow is setup in a semantically correct way

@strandn strandn force-pushed the ttsketch branch 2 times, most recently from b8b213a to 6e64c97 Compare April 3, 2026 03:52
@Iximiel
Copy link
Copy Markdown
Member

Iximiel commented Apr 3, 2026

@strandn

DO NOT SQUASH YOUR CHANGES ON A MERGE COMMIT

if you need the changes on master: rebase on master, do not merge and then squash-rebase, or if you have merged, do an interactive rebase and squash your commits, leaving alone the the merge commit
if an AI made you do so make clear to it that it was a wrong move, a very wrong one.

if you do not have a backup branch you can reset to, make a backup branch (for example use git branch ttsketch-bk) use use git reflog to get to the hash before the merge and reset to it, rebase on master and, if needed, squash your commits

when you change the history (like doing a rebase or something similar) always create a backup branch to reset to in case of something goes wrong

@strandn
Copy link
Copy Markdown
Author

strandn commented Apr 3, 2026

@Iximiel First of all, thank you very much for your messages and your assistance so far. I am very sorry for the inconvenience. This is my first time dealing with git rebasing. I think the culprit of the issue is the fact that I have two separate local "ttsketch" branches syncing to the same remote (GitHub) branch (one in my personal laptop and the other in my university's cluster). A few days ago, I successfully rebased (I thought) from one of my local branches but since then, blindly did git pull from the other local branch. That seems to have done a merge, not a rebase. This created a merge commit that brought back my entire commit history (the 800+ commits I thought I'd squashed away). When I pushed that merge commit, it rewrote the remote and my squash, and seemed to have resulted in some very undesired behavior (such as my branch deviating from plumed2/master in unexpected ways). I will be more mindful in the future. Let me roll back some changes to recover a recent stable version. Again, apologies.

@Iximiel
Copy link
Copy Markdown
Member

Iximiel commented Apr 3, 2026

@strandn do not worry, git is a difficult but necessary evil (see the famous readme of the first commit of git for the opinion of Linus).
I get in similar situations often. Force pushing a branch cause problems if is synced on other computers. In your case a good practice could be to treat the ttsketch branch as the "Truth", and try things on a secondary branches on each machine that will be merged onto ttsketch when you are satisfied with the changes, or simply throw them away when you do not like the new things. Trust me on this: it will save you some scares and headaches! The extra branches can stay local unless needed on the other computer.

A good idea for moving things around could be to trying to use a TUI or GUI for git, I use lazygit, but there are good free addons for vscode and the usual gitk.

Before rebasing/resetting or any other thing that change the history, remember to backup the branch you are going to work with by creating a new one, that will also help in case of missteps. Sorry for repeating this.

Nils Strand and others added 2 commits April 3, 2026 09:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants