
# -------------------------------------------------------------
# Initial Configuration
# -------------------------------------------------------------
bulk_configuration = nlread('Polystyrene.hdf5')[-1]

# -------------------------------------------------------------
# OPLS-AA Calculator
# -------------------------------------------------------------
potential_builder = OPLSPotentialBuilder()
calculator = potential_builder.createCalculator(bulk_configuration)
bulk_configuration.setCalculator(calculator)

# -------------------------------------------------------------
# Molecular Dynamics
# -------------------------------------------------------------
initial_velocity = ConfigurationVelocities(
    remove_center_of_mass_momentum=True
)

method = NPTMartynaTobiasKlein(
    time_step=1*femtoSecond,
    reservoir_temperature=300*Kelvin,
    reservoir_pressure=1*bar,
    thermostat_timescale=100*femtoSecond,
    barostat_timescale=500*femtoSecond,
    initial_velocity=initial_velocity,
    heating_rate=0*Kelvin/picoSecond,
)

constraints = [FixCenterOfMass()]

md_trajectory = MolecularDynamics(
    bulk_configuration,
    constraints=constraints,
    trajectory_filename='Polystyrene_Trajectory.hdf5',
    steps=100000,
    log_interval=10,
    trajectory_interval=1000,
    method=method
)

# -------------------------------------------------------------
# Calculate End to End Distance
# -------------------------------------------------------------
analyzer = PolymerSegmentAnalyzer(
      md_trajectory,
      start_time=0*ps,
      end_time=100*ps,
      end_tags=['HEAD_CONNECT', 'TAIL_CONNECT'],
      atoms_per_segment=2
)

number_of_segments = 64
number_of_chains = 8

bond_correlation = numpy.zeros(number_of_segments-1)
characteristic_ratio = numpy.zeros(number_of_segments)
segments = numpy.arange(number_of_segments)

for i in range(number_of_chains):
    bond_correlation += analyzer.bondCorrelation(i)
    characteristic_ratio += analyzer.characteristicRatio(i)

bond_correlation /= number_of_chains
characteristic_ratio /= number_of_chains

# -------------------------------------------------------------
# Plot The Results
# -------------------------------------------------------------
import pylab
pylab.figure()
pylab.plot(segments[:number_of_segments-1], bond_correlation)
pylab.xlabel('Segment')
pylab.ylabel('Bond Correlation')
pylab.savefig('Bond_Correlation_Plot.png')

pylab.figure()
pylab.plot(segments, characteristic_ratio)
pylab.xlabel('Segment')
pylab.ylabel('Characteristic Ratio')
pylab.savefig('Characteristic_Ratio_Plot.png')
