Reweighing code query

62 views
Skip to first unread message

Praveen Ranganath Prabhakar

unread,
May 9, 2023, 6:05:22 AM5/9/23
to westpa...@googlegroups.com
Hi Westpa community, 

We are currently working on a reweighting WE scheme where using an algorithm we recalculate the weights of the walkers after every 5 (currently arbitrary for a simple system) iterations. The code we currently use (attached below), seems to make changes to the weights only in the west.h5 file, but WESTPA continues the simulation with earlier original weights. 

run = Run.open("west.h5")
n_iters = run.num_iterations
run.close()
run.h5file
with h5py.File("west.h5", "r+") as h5file:
    path = "iterations/iter_" + str(niters).zfill(8)
    group = h5file[path]['seg_index']
    weights = h5file[path]['seg_index']['weight']
    new_weight_data = np.loadtxt("file_weights.txt")  #Assuming the new weights is present in this file
    for j in range(nsegs):
        weights[j] = new_weight_data[j]
        group['weight', ...] = weights
    h5file.close()

The above code updates the weights of the walkers in the west.h5 file. Our reweighting scheme ensures the total probabilities add up to 1. We tried our algorithm on the NACl system and obtained the below graph 
new.png

Here we can see that when reweighting is happening (from the stripes), the weights are changing (from free energy), but WESTPA simulation continues the next iteration with older weights. Can you please provide any guidance on how we can make WESTPA consider the updated new weights?

Best, 
Praveen
  

Jeremy Leung

unread,
May 9, 2023, 11:17:22 AM5/9/23
to westpa-users
Hi Praveen,

At what point are you rewriting the weights? The next iteration is prepared at the end of the previous iteration, so if you're rewriting them after the next iteration is prepared, the old weights are used.

There are two different ways to do it:
    1) `w_truncate` the next iteration after you rewrite the weights. So if you're rewriting iter 20, then `w_truncate -n 21` will delete all existing data on iteration 21. They will be rebuilt after you restart the simulation with the new weights.
    2) This is more involved and require more intricate knowledge of WESTPA. Subclass the sim manager (and/or driver) and replace the pre-we step with the whatever reweighting step you have in mind. I would probably structure the code along these lines: A) import westpa.rc B) call the data_manager C) proceed to calculate and rewrite the weights dataset.  I expect some of the plugins (WEED/WESS/StringMethod) would be good reference for, although they are invoked at a different time points and does things slightly differently. That might be more desirable.

Also instead of calling `westpa.analysis` just for the number of iterations, doing a `len(h5file['summary'])-1` or reading the attribute `h5file.attrs['west_current_iteration']` would probably be sufficient with code snippet you have. :) You might have to subtract one from that number depending whether the next iteration is prepared or not.

-- JL

Praveen Ranganath Prabhakar

unread,
May 9, 2023, 6:31:33 PM5/9/23
to westpa...@googlegroups.com
Hi Jeremy and Anthony, 

Thank you for the quick response. 

Anthony, 

I am not sure if the way I am doing would be considered on the fly? So what I do is -- let's say I have to do 50 iterations, and I want to reweigh after every 5 iterations, I have a script which creates 10 job submission files and at the end of each job and before the next job is run, I run a python script which does the reweighing. I am not stopping the simulation at any point. Please let me know if you have the script you had mentioned available :) :) Thank you

Jeremy, 

Thank you so much for sharing the ways I could implement the reweighing. I think I would try the first one as the second way seems a bit complicated. 
Yes noted on the `len(h5file['summary'])-1` for finding the number of iterations :)

Best, 
Praveen
 

--
You received this message because you are subscribed to the Google Groups "westpa-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to westpa-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/westpa-users/12a68854-1851-4002-a473-de07fe494c5an%40googlegroups.com.

Jeremy Leung

unread,
May 10, 2023, 3:57:07 PM5/10/23
to westpa-users
Hi Praveen,

When you have 10 job submission files, you're technically stopping and restarting the simulations between each job, so yes, it's not what's considered on-the-fly. But again, w_truncating should fix whatever problem you're having. If this modified workflow works, there's no need to swap to any of the on-the-fly implementations. :)

Let us know how it goes!

-- JL
Reply all
Reply to author
Forward
0 new messages