Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option for writing to XTC file #36

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions openmmsetup/openmmsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def showSimulationOptions():
def setSimulationOptions():
for key in request.form:
session[key] = request.form[key]
session['writeDCD'] = 'writeDCD' in request.form
session['writeTrajectory'] = 'writeTrajectory' in request.form
session['writeData'] = 'writeData' in request.form
session['writeCheckpoint'] = 'writeCheckpoint' in request.form
session['dataFields'] = request.form.getlist('dataFields')
Expand Down Expand Up @@ -421,9 +421,10 @@ def configureDefaultOptions():
session['pressure'] = '1.0'
session['barostatInterval'] = '25'
session['nonbondedMethod'] = 'CutoffNonPeriodic' if implicitWater else 'PME'
session['writeDCD'] = True
session['dcdFilename'] = 'trajectory.dcd'
session['dcdInterval'] = '10000'
session['writeTrajectory'] = True
session['trajFormat'] = 'dcd'
session['trajFilename'] = 'trajectory.dcd'
session['trajInterval'] = '10000'
session['writeData'] = True
session['dataFilename'] = 'log.txt'
session['dataInterval'] = '1000'
Expand Down Expand Up @@ -548,8 +549,11 @@ def write(self, string):
script.append("platform = Platform.getPlatformByName('%s')" % session['platform'])
if session['platform'] in ('CUDA', 'OpenCL'):
script.append("platformProperties = {'Precision': '%s'}" % session['precision'])
if session['writeDCD']:
script.append("dcdReporter = DCDReporter('%s', %s)" % (session['dcdFilename'], session['dcdInterval']))
if session['writeTrajectory']:
if session['trajFormat'] == 'dcd':
script.append("dcdReporter = DCDReporter('%s', %s)" % (session['trajFilename'], session['trajInterval']))
else:
script.append("xtcReporter = XTCReporter('%s', %s)" % (session['trajFilename'], session['trajInterval']))
if session['writeData']:
args = ', '.join('%s=True' % field for field in session['dataFields'])
script.append("dataReporter = StateDataReporter('%s', %s, totalSteps=steps," % (session['dataFilename'], session['dataInterval']))
Expand Down Expand Up @@ -643,8 +647,11 @@ def _xml_script_segment(to_serialize, target_file):

script.append('\n# Simulate\n')
script.append("print('Simulating...')")
if session['writeDCD']:
script.append('simulation.reporters.append(dcdReporter)')
if session['writeTrajectory']:
if session['trajFormat'] == 'dcd':
script.append('simulation.reporters.append(dcdReporter)')
else:
script.append('simulation.reporters.append(xtcReporter)')
if session['writeData']:
script.append('simulation.reporters.append(dataReporter)')
if isInternal:
Expand Down
35 changes: 27 additions & 8 deletions openmmsetup/templates/simulationOptions.html
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,22 @@
<div id="output" class="tab-pane fade">
<p/>
<div class="form-group">
<label><input type="checkbox" name="writeDCD" id="writeDCD" oninput="optionChanged()" {{ 'checked' if session['writeDCD'] else '' }}> Save trajectory to a DCD file</label>
<label><input type="checkbox" name="writeTrajectory" id="writeTrajectory" oninput="optionChanged()" {{ 'checked' if session['writeTrajectory'] else '' }}> Save trajectory to a file</label>
</div>
<div id="dcdFileOptions">
<div id="trajFileOptions">
<div class="form-group">
<label for="dcdFilename">DCD Filename</label>
{{ textfield('dcdFilename', 'The filename for the trajectory file.') }}
<label for="trajFormat">Trajectory Format</label>
{{ choice('trajFormat', 'Select format in which to save the trajectory.', [
('dcd', 'DCD', 'This format stores coordinates most precisely.'),
('xtc', 'XTC', 'This format stores coordinates with reduced precision, leading to smaller files.')], 'trajectoryFormatChanged') }}
</div>
<div class="form-group">
<label for="dcdInterval">DCD Output Interval (steps)</label>
{{ textfield('dcdInterval', 'The interval at which to write frames to the trajectory, measured in time steps.') }}
<label for="trajFilename">Trajectory Filename</label>
{{ textfield('trajFilename', 'The filename for the trajectory file.') }}
</div>
<div class="form-group">
<label for="trajInterval">Trajectory Output Interval (steps)</label>
{{ textfield('trajInterval', 'The interval at which to write frames to the trajectory, measured in time steps.') }}
</div>
</div>
<div class="form-group">
Expand Down Expand Up @@ -264,6 +270,19 @@ <h4 class="modal-title">Platform Not Available</h4>
</form>

<script>
function trajectoryFormatChanged() {
// Used when <select> for trajFormat changes. This changes the
// extension of the file depending on the selected format.
var fileLabel = document.getElementById("trajFilename");
var asSplit = fileLabel.value.split(".");
if (asSplit.length > 1) asSplit.pop();
if (asSplit[0] == "") asSplit[0] = "trajectory"; // set default if empty
asSplit.push(document.getElementById("trajFormat").value);
fileLabel.value = asSplit.join(".");

optionChanged();
}

function outputFiletypeChanged() {
// Used when <select> for finalStateFileType changes. This changes the
// extension of the file depending on the selected output filetype.
Expand Down Expand Up @@ -297,8 +316,8 @@ <h4 class="modal-title">Platform Not Available</h4>
document.getElementById("barostatIntervalRow").hidden = (ensemble != 'npt');
platform = document.getElementById("platform").value;
document.getElementById("precisionRow").hidden = (platform != 'CUDA' && platform != 'OpenCL');
writeDCD = document.getElementById("writeDCD").checked;
document.getElementById("dcdFileOptions").hidden = !writeDCD;
writeTrajectory = document.getElementById("writeTrajectory").checked;
document.getElementById("trajFileOptions").hidden = !writeTrajectory;
writeData = document.getElementById("writeData").checked;
document.getElementById("logFileOptions").hidden = !writeData;
writeCheckpoint = document.getElementById("writeCheckpoint").checked;
Expand Down