Skip to content

Commit

Permalink
Merge pull request #1403 from virtualcell/dan-ss-random-seed
Browse files Browse the repository at this point in the history
numTrials and randomSeed for Langevin
  • Loading branch information
danv61 authored Dec 21, 2024
2 parents 4b1ed10 + c604e06 commit 1078b55
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package cbit.vcell.solver.ode.gui;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.*;
import java.math.BigInteger;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.*;
import javax.swing.border.Border;

import cbit.vcell.solver.NFsimSimulationOptions;
import org.vcell.solver.nfsim.gui.NFSimSimulationOptionsPanel;
import org.vcell.util.gui.CollapsiblePanel;
import org.vcell.util.gui.DialogUtils;

import cbit.vcell.client.PopupGenerator;
import cbit.vcell.solver.LangevinSimulationOptions;
import cbit.vcell.solver.OutputTimeSpec;
import cbit.vcell.solver.SolverTaskDescription;
import org.vcell.util.gui.DialogUtils;

@SuppressWarnings("serial")
public class LangevinOptionsPanel extends CollapsiblePanel {
Expand All @@ -39,6 +37,11 @@ public class LangevinOptionsPanel extends CollapsiblePanel {
private JLabel numPartitionsZLabel = null;
private JLabel numPartitionsLabel = null;

private JCheckBox randomSeedCheckBox;
private JTextField randomSeedTextField;
private JButton randomSeedHelpButton = null;


// private int[] npart = {LangevinSimulationOptions.DefaultNPart[0], LangevinSimulationOptions.DefaultNPart[1], LangevinSimulationOptions.DefaultNPart[2]}; // number of partitions on each axis

private JTextField intervalImageTextField = null;
Expand Down Expand Up @@ -67,11 +70,39 @@ public void actionPerformed(java.awt.event.ActionEvent e) {
if (e.getSource() == getTrajectoryButton()) {
getJTextFieldNumOfTrials().setEnabled(false);
getJTextFieldNumOfParallelLocalRuns().setEnabled(false);
setNewOptions();
getJTextFieldNumOfParallelLocalRuns().setText("");
solverTaskDescription.setNumTrials(1);
getJTextFieldNumOfTrials().setText("");
} else if (e.getSource() == getMultiRunButton()) {
getJTextFieldNumOfTrials().setEnabled(true);
getJTextFieldNumOfParallelLocalRuns().setEnabled(true);
setNewOptions();
getJTextFieldNumOfParallelLocalRuns().setEnabled(false);
getJTextFieldNumOfParallelLocalRuns().setText(solverTaskDescription.getLangevinSimulationOptions().getNumOfParallelLocalRuns()+"");
int numTrials = solverTaskDescription.getNumTrials();
if(numTrials > 1) { // a multi-trial number is already set
getJTextFieldNumOfTrials().setText(numTrials+"");
} else {
solverTaskDescription.setNumTrials(SolverTaskDescription.DefaultNumTrials);
getJTextFieldNumOfTrials().setText(SolverTaskDescription.DefaultNumTrials+"");
}
} else if(e.getSource() == randomSeedCheckBox) {
randomSeedTextField.setEditable(randomSeedCheckBox.isSelected());
if(randomSeedCheckBox.isSelected()) {

BigInteger rs = solverTaskDescription.getLangevinSimulationOptions().getRandomSeed();
if(rs == null) {
rs = LangevinSimulationOptions.DefaultRandomSeed;
solverTaskDescription.getLangevinSimulationOptions().setRandomSeed(rs);
}
randomSeedTextField.setText(rs.toString());
} else {
solverTaskDescription.getLangevinSimulationOptions().setRandomSeed(null);
randomSeedTextField.setText("");
}
} else if(e.getSource() == randomSeedHelpButton) {
DialogUtils.showInfoDialogAndResize(LangevinOptionsPanel.this, "Set specific seed",
"<html>Provide a seed to the Langevin solver's random number generator so exact "
+ "trajectories can be reproduced. If this value is not entered, the solver will "
+ "generate different sequences for each run.</html>");
}
}

Expand All @@ -82,14 +113,29 @@ public void focusLost(java.awt.event.FocusEvent e) {
if (e.isTemporary()) {
return;
}
if (e.getSource() == getJTextFieldNumOfTrials() ||
e.getSource() == getJTextFieldNumOfParallelLocalRuns() ||
if (e.getSource() == getJTextFieldNumOfParallelLocalRuns() ||
e.getSource() == getJTextFieldIntervalImage() ||
e.getSource() == getJTextFieldIntervalSpring() ||
e.getSource() == getNumPartitionsXTextField() ||
e.getSource() == getNumPartitionsYTextField() ||
e.getSource() == getNumPartitionsXTextField()) {
e.getSource() == getNumPartitionsXTextField() ||
e.getSource() == getRandomSeedTextField() ) {
setNewOptions();
} else if(e.getSource() == getJTextFieldNumOfTrials()) {
int numTrials;
try {
numTrials = Integer.parseInt(getJTextFieldNumOfTrials().getText());
if(numTrials < 2) {
numTrials = SolverTaskDescription.DefaultNumTrials;
}
} catch(NumberFormatException ex) {
numTrials = solverTaskDescription.getNumTrials();
if(numTrials < 2) {
numTrials = SolverTaskDescription.DefaultNumTrials;
}
}
solverTaskDescription.setNumTrials(numTrials);
getJTextFieldNumOfTrials().setText(numTrials+"");
}
}
}
Expand All @@ -111,6 +157,8 @@ private void initialize() {
centerPanel.setLayout(new GridBagLayout());
JPanel rightPanel = new JPanel(new GridBagLayout()); // fake panel, for looks
rightPanel.setLayout(new GridBagLayout());
JPanel bottomPanel = new JPanel(new GridBagLayout());
bottomPanel.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
Expand Down Expand Up @@ -139,6 +187,16 @@ private void initialize() {
gbc.insets = new Insets(1,1,1,1);
getContentPanel().add(rightPanel, gbc);

gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 3;
// gbc.weightx = 1.0;
// gbc.weighty = 1.0;
gbc.insets = new Insets(1,1,1,1);
getContentPanel().add(bottomPanel, gbc);

// ----- trialPanel --------------------------------------------------------------
gbc = new GridBagConstraints();
gbc.gridx = 0;
Expand Down Expand Up @@ -189,7 +247,6 @@ private void initialize() {
gbc.insets = new Insets(0,5,3,1);
trialPanel.add(getJTextFieldNumOfParallelLocalRuns(), gbc);

// ----- aici
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 3;
Expand Down Expand Up @@ -246,7 +303,6 @@ private void initialize() {
gbc.insets = new Insets(0,5,3,1);
trialPanel.add(getNumPartitionsZTextField(), gbc);


// gbc = new GridBagConstraints(); // --- empty panel (filler)
// gbc.gridx = 3;
// gbc.gridy = 1;
Expand Down Expand Up @@ -322,6 +378,32 @@ private void initialize() {
gbc.insets = new Insets(0,22,1,6);
rightPanel.add(new JLabel(""), gbc); // fake, just for looks

// ----- bottomPanel ------------------------------------------------------
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5,2,6,5);
bottomPanel.add(getRandomSeedCheckBox(), gbc);

gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.gridwidth = 2;
gbc.insets = new Insets(2, 2, 6, 6);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.LINE_START;
bottomPanel.add(getRandomSeedTextField(), gbc);

gbc = new GridBagConstraints();
gbc.gridx = 3;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(2, 2, 6, 6);
bottomPanel.add(getRandomSeedHelpButton(), gbc);

// ----------------------------------------------------------------------
getButtonGroupTrials().add(getTrajectoryButton());
getButtonGroupTrials().add(getMultiRunButton());
Expand Down Expand Up @@ -379,7 +461,7 @@ private javax.swing.JTextField getJTextFieldNumOfTrials() {
ivjJTextFieldNumOfTrials = new javax.swing.JTextField();
ivjJTextFieldNumOfTrials.setName("JTextFieldNumOfTrials");
ivjJTextFieldNumOfTrials.setColumns(9);
ivjJTextFieldNumOfTrials.setText("100");
ivjJTextFieldNumOfTrials.setText("");
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
Expand All @@ -398,6 +480,33 @@ private javax.swing.JLabel getNumOfTrialsLabel() {
}
return numOfTrialsLabel;
}
private JCheckBox getRandomSeedCheckBox() {
if(randomSeedCheckBox == null) {
randomSeedCheckBox = new JCheckBox();
randomSeedCheckBox.setName("RandomSeedCheckBox");
randomSeedCheckBox.setText("Set a seed to Langevin random number generator.");
}
return randomSeedCheckBox;
}
private JTextField getRandomSeedTextField() {
if(randomSeedTextField == null) {
randomSeedTextField = new JTextField();
randomSeedTextField.setName("RandomSeedTextField");
}
return randomSeedTextField;
}
private JButton getRandomSeedHelpButton() {
if(randomSeedHelpButton == null) {
Border border = BorderFactory.createEmptyBorder(1, 1, 1, 1);
randomSeedHelpButton = new JButton();
Font font = randomSeedHelpButton.getFont().deriveFont(Font.BOLD);
randomSeedHelpButton.setName("RandomSeedHelpButton");
randomSeedHelpButton.setBorder(border);
randomSeedHelpButton.setFont(font);
randomSeedHelpButton.setText("<html><b>&nbsp;&nbsp;?&nbsp;&nbsp;</b></html>");
}
return randomSeedHelpButton;
}

// ======================================================================= Number of Partitions
private javax.swing.JTextField getNumPartitionsXTextField() {
Expand Down Expand Up @@ -494,7 +603,7 @@ private javax.swing.JTextField getJTextFieldNumOfParallelLocalRuns() {
ivjJTextFieldNumOfParallelLocalRuns = new javax.swing.JTextField();
ivjJTextFieldNumOfParallelLocalRuns.setName("JTextFieldNumOfParallelLocalRuns");
ivjJTextFieldNumOfParallelLocalRuns.setColumns(3);
ivjJTextFieldNumOfParallelLocalRuns.setText("1");
ivjJTextFieldNumOfParallelLocalRuns.setText("");
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
Expand Down Expand Up @@ -569,14 +678,17 @@ public final void setSolverTaskDescription(SolverTaskDescription newValue) {
private void initConnections() {
getTrajectoryButton().addActionListener(ivjEventHandler);
getMultiRunButton().addActionListener(ivjEventHandler);

getRandomSeedCheckBox().addActionListener(ivjEventHandler);
getRandomSeedHelpButton().addActionListener(ivjEventHandler);

getJTextFieldNumOfTrials().addFocusListener(ivjEventHandler);
getJTextFieldNumOfParallelLocalRuns().addFocusListener(ivjEventHandler);
getJTextFieldIntervalImage().addFocusListener(ivjEventHandler);
getJTextFieldIntervalSpring().addFocusListener(ivjEventHandler);
getNumPartitionsXTextField().addFocusListener(ivjEventHandler);
getNumPartitionsYTextField().addFocusListener(ivjEventHandler);
getNumPartitionsZTextField().addFocusListener(ivjEventHandler);
getRandomSeedTextField().addFocusListener(ivjEventHandler);
}

private void refresh() {
Expand All @@ -588,19 +700,23 @@ private void refresh() {
return;
}
setVisible(true);


getMultiRunButton().setEnabled(true);

LangevinSimulationOptions lso = solverTaskDescription.getLangevinSimulationOptions();
int numTrials = lso.getNumOfTrials();
int numTrials = solverTaskDescription.getNumTrials();
int numOfParallelLocalRuns = lso.getNumOfParallelLocalRuns();
if(numTrials == 1) {
getTrajectoryButton().setSelected(true);
getJTextFieldNumOfTrials().setEnabled(false);
getJTextFieldNumOfTrials().setText("");
getJTextFieldNumOfParallelLocalRuns().setEnabled(false);
getJTextFieldNumOfParallelLocalRuns().setText("");
} else {
getMultiRunButton().setSelected(true);
getJTextFieldNumOfTrials().setEnabled(true);
getJTextFieldNumOfTrials().setText(numTrials+"");
getJTextFieldNumOfParallelLocalRuns().setEnabled(true);
getJTextFieldNumOfParallelLocalRuns().setEnabled(false);
getJTextFieldNumOfParallelLocalRuns().setText(numOfParallelLocalRuns + "");
}

Expand All @@ -611,14 +727,16 @@ private void refresh() {
getJTextFieldIntervalImage().setText(lso.getIntervalImage()+"");
getJTextFieldIntervalSpring().setText(lso.getIntervalSpring()+"");

// TODO: temporarily disable the button
// UNDO THIS WHEN DEVELOPMENT IS COMPLETE
if(solverTaskDescription.getSolverDescription().isLangevinSolver()) {
getMultiRunButton().setEnabled(false);
return;
BigInteger randomSeed = lso.getRandomSeed();
if (randomSeed == null) {
randomSeedTextField.setEditable(false);
randomSeedCheckBox.setSelected(false);
randomSeedTextField.setText("");
} else {
randomSeedTextField.setEditable(true);
randomSeedCheckBox.setSelected(true);
randomSeedTextField.setText(randomSeed.toString());
}


}

private void setNewOptions() {
Expand All @@ -627,19 +745,32 @@ private void setNewOptions() {
}
try {
LangevinSimulationOptions sso = solverTaskDescription.getLangevinSimulationOptions();
int numTrials = 1;
int numOfParallelLocalRuns = 1;
double intervalImage = solverTaskDescription.getLangevinSimulationOptions().getIntervalImage();
double intervalSpring = solverTaskDescription.getLangevinSimulationOptions().getIntervalSpring();
int[] npart = solverTaskDescription.getLangevinSimulationOptions().getNPart();

if(getMultiRunButton().isSelected()) {
numTrials = Integer.parseInt(getJTextFieldNumOfTrials().getText());
sso.setNumOfTrials(numTrials);
numOfParallelLocalRuns = Integer.parseInt(getJTextFieldNumOfParallelLocalRuns().getText());
sso.setNumOfParallelLocalRuns(numOfParallelLocalRuns);
if(getMultiRunButton().isSelected()) { // we can get here only on FocusLost event in the numOfTrials text field
try {
numOfParallelLocalRuns = Integer.parseInt(getJTextFieldNumOfParallelLocalRuns().getText());
} catch (NumberFormatException ex) {
numOfParallelLocalRuns = sso.getNumOfParallelLocalRuns();
}
}

BigInteger randomSeed = null;
if (randomSeedCheckBox.isSelected()) {
try {
randomSeed = new BigInteger(randomSeedTextField.getText());
} catch (NumberFormatException ex) {
randomSeed = solverTaskDescription.getLangevinSimulationOptions().getRandomSeed();
if(randomSeed == null) {
randomSeed = LangevinSimulationOptions.DefaultRandomSeed;
}
// randomSeedTextField.setText(randomSeed.toString());
}
}

intervalImage = Double.parseDouble(getJTextFieldIntervalImage().getText());
intervalSpring = Double.parseDouble(getJTextFieldIntervalSpring().getText());

Expand All @@ -649,6 +780,8 @@ private void setNewOptions() {

// make a copy
LangevinSimulationOptions lso = new LangevinSimulationOptions(sso);
lso.setNumOfParallelLocalRuns(numOfParallelLocalRuns);
lso.setRandomSeed(randomSeed);
lso.setIntervalImage(intervalImage);
lso.setIntervalSpring(intervalSpring);
lso.setNPart(npart);
Expand Down
8 changes: 5 additions & 3 deletions vcell-core/src/main/java/cbit/vcell/math/VCML.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ public class VCML {
public final static String ParticleComponentLocation = "Location"; // particle Langevin / SpringSaLaD
public final static String ParticleComponentCoordinate = "Coordinate"; // particle Langevin / SpringSaLaD
public final static String ParticleComponentColor = "NamedColor"; // particle Langevin / SpringSaLaD

// 12/19/2024 Dan - SolverTaskDescription.numTrials that we'll use in Langevin / SpringSaLaD
// will generalize the use for anything stochastic (Gibson, NFSim, etc) that will imply parallel runs
public final static String SolverTaskDescriptionNumTrials = "SolverTaskDescriptionNumTrials"; // scan for generated random seed series

public final static String ParticleMolecularComponent = "Component";
public final static String ParticleComponentAllowableState = "AllowableState";
public final static String ParticleMolecularTypePattern = "MolecularType";
Expand All @@ -244,8 +247,7 @@ public class VCML {
public final static String VolumeParticleSpeciesPatterns = "VolumeParticleSpeciesPatterns";

public final static String LangevinSimulationOptions = "LangevinSimulationOptions";
public final static String LangevinSimulationOptions_numOfTrials = "NumOfTrials";
public final static String LangevinSimulationOptions_runIndex = "RunIndex";
public final static String LangevinSimulationOptions_randomSeed = "LangevinRandomSeed";
public final static String LangevinSimulationOptions_intervalSpring = "IntervalSpring";
public final static String LangevinSimulationOptions_intervalImage = "IntervalImage";
public final static String LangevinSimulationOptions_Partition_Nx = "PartitionNx";
Expand Down
Loading

0 comments on commit 1078b55

Please sign in to comment.