def compute_evaluate_wavepackets(pp, iom, blockid=0, eigentrafo=True): """Evaluate a homogeneous Hagedorn wavepacket on a given grid for each timestep. :param pp: An :py:class:`ParameterProvider` instance providing the grid data. :param iom: An :py:class:`IOManager` instance providing the simulation data. :param blockid: The data block from which the values are read. :param eigentrafo: Whether or not do an eigentransformation before evaluation is done. """ parameters = iom.load_parameters() if pp is None: pp = parameters # Number of time steps we saved timesteps = iom.load_wavepacket_timegrid(blockid=blockid) nrtimesteps = timesteps.shape[0] # Prepare the potential for basis transformations Potential = BlockFactory().create_potential(parameters) grid = BlockFactory().create_grid(pp) # We want to save wavefunctions, thus add a data slot to the data file d = {"ncomponents": parameters["ncomponents"], "number_nodes": pp["number_nodes"], "dimension": parameters["dimension"]} iom.add_grid(d, blockid=blockid) iom.add_wavefunction(d, timeslots=nrtimesteps, flat=True, blockid=blockid) iom.save_grid(grid.get_nodes(), blockid=blockid) # Initialize a Hagedorn wavepacket with the data descr = iom.load_wavepacket_description(blockid=blockid) HAWP = BlockFactory().create_wavepacket(descr) # Basis transformator if eigentrafo is True: BT = BasisTransformationHAWP(Potential) BT.set_matrix_builder(HAWP.get_innerproduct()) # Basis shapes BS_descr = iom.load_wavepacket_basisshapes(blockid=blockid) BS = {} for ahash, descr in BS_descr.items(): BS[ahash] = BlockFactory().create_basis_shape(descr) WF = WaveFunction(parameters) WF.set_grid(grid) # Iterate over all timesteps for i, step in enumerate(timesteps): print(" Evaluating homogeneous wavepacket at timestep %d" % step) # Retrieve simulation data params = iom.load_wavepacket_parameters(timestep=step, blockid=blockid, key=("q", "p", "Q", "P", "S", "adQ")) hashes, coeffs = iom.load_wavepacket_coefficients(timestep=step, get_hashes=True, blockid=blockid) # Configure the wavepacket HAWP.set_parameters(params, key=("q", "p", "Q", "P", "S", "adQ")) HAWP.set_basis_shapes([BS[int(ha)] for ha in hashes]) HAWP.set_coefficients(coeffs) # Transform to the eigenbasis. if eigentrafo is True: BT.transform_to_eigen(HAWP) # Evaluate the wavepacket values = HAWP.evaluate_at(grid, prefactor=True) WF.set_values(values) # Save the wave function iom.save_wavefunction(WF.get_values(), timestep=step, blockid=blockid)
def compute_energy(iom, blockid=0, eigentrafo=True, iseigen=True): """ :param iom: An :py:class:`IOManager: instance providing the simulation data. :param blockid: The data block from which the values are read. Default is `0`. :param eigentrafo: Whether to make a transformation into the eigenbasis. :type eigentrafo: Boolean, default is ``True``. :param iseigen: Whether the data is assumed to be in the eigenbasis. :type iseigen: Boolean, default is ``True`` """ parameters = iom.load_parameters() # Number of time steps we saved timesteps = iom.load_wavefunction_timegrid(blockid=blockid) nrtimesteps = timesteps.shape[0] # Construct grid from the parameters grid = BlockFactory().create_grid(parameters) # The potential used Potential = BlockFactory().create_potential(parameters) # The operators KO = KineticOperator(grid) KO.calculate_operator(parameters["eps"]) opT = KO if eigentrafo is True: opV = Potential.evaluate_at(grid) else: if iseigen is True: opV = Potential.evaluate_eigenvalues_at(grid, as_matrix=True) else: opV = Potential.evaluate_at(grid, as_matrix=True) # Basis transformator if eigentrafo is True: BT = BasisTransformationWF(Potential) BT.set_grid(grid) # And two empty wavefunctions WF = WaveFunction(parameters) WF.set_grid(grid) WF2 = WaveFunction(parameters) WF2.set_grid(grid) # We want to save norms, thus add a data slot to the data file iom.add_energy(parameters, timeslots=nrtimesteps, blockid=blockid) nst = Potential.get_number_components() if eigentrafo is True: # Iterate over all timesteps for i, step in enumerate(timesteps): print(" Computing energies of timestep # " + str(step)) # Retrieve simulation data values = iom.load_wavefunction(timestep=step, blockid=blockid) values = [ values[j,...] for j in xrange(parameters["ncomponents"]) ] WF.set_values(values) # Project wavefunction values to eigenbasis BT.transform_to_eigen(WF) ekinlist = [] epotlist = [] # For each component of |Psi> values = WF.get_values() for index, item in enumerate(values): # tmp is the Vector (0, 0, 0, \psi_i, 0, 0, ...) tmp = [ zeros(item.shape) for z in xrange(nst) ] tmp[index] = item WF2.set_values(tmp) # Project this vector to the canonical basis BT.transform_to_canonical(WF2) # And calculate the energies of these components ekinlist.append(WF2.kinetic_energy(opT, summed=True)) epotlist.append(WF2.potential_energy(opV, summed=True)) iom.save_energy((ekinlist, epotlist), timestep=step, blockid=blockid) else: # Iterate over all timesteps for i, step in enumerate(timesteps): print(" Computing energies of timestep # " + str(step)) # Retrieve simulation data values = iom.load_wavefunction(timestep=step, blockid=blockid) values = [ values[j,...] for j in xrange(parameters["ncomponents"]) ] WF.set_values(values) # And calculate the energies of these components ekinlist = WF.kinetic_energy(opT, summed=False) epotlist = WF.potential_energy(opV, summed=False) iom.save_energy((ekinlist, epotlist), timestep=step, blockid=blockid)
def compute_energy(iom, blockid=0, eigentrafo=True, iseigen=True): """ :param iom: An :py:class:`IOManager: instance providing the simulation data. :param blockid: The data block from which the values are read. Default is `0`. :param eigentrafo: Whether to make a transformation into the eigenbasis. :type eigentrafo: Boolean, default is ``True``. :param iseigen: Whether the data is assumed to be in the eigenbasis. :type iseigen: Boolean, default is ``True`` """ parameters = iom.load_parameters() # Number of time steps we saved timesteps = iom.load_wavefunction_timegrid(blockid=blockid) nrtimesteps = timesteps.shape[0] # Construct grid from the parameters grid = BlockFactory().create_grid(parameters) # The potential used Potential = BlockFactory().create_potential(parameters) # The operators KO = KineticOperator(grid) KO.calculate_operator(parameters["eps"]) opT = KO if eigentrafo is True: opV = Potential.evaluate_at(grid) else: if iseigen is True: opV = Potential.evaluate_eigenvalues_at(grid, as_matrix=True) else: opV = Potential.evaluate_at(grid, as_matrix=True) # Basis transformator if eigentrafo is True: BT = BasisTransformationWF(Potential) BT.set_grid(grid) # And two empty wavefunctions WF = WaveFunction(parameters) WF.set_grid(grid) WF2 = WaveFunction(parameters) WF2.set_grid(grid) # We want to save norms, thus add a data slot to the data file iom.add_energy(parameters, timeslots=nrtimesteps, blockid=blockid) nst = Potential.get_number_components() if eigentrafo is True: # Iterate over all timesteps for i, step in enumerate(timesteps): print(" Computing energies of timestep # " + str(step)) # Retrieve simulation data values = iom.load_wavefunction(timestep=step, blockid=blockid) values = [ values[j, ...] for j in xrange(parameters["ncomponents"]) ] WF.set_values(values) # Project wavefunction values to eigenbasis BT.transform_to_eigen(WF) ekinlist = [] epotlist = [] # For each component of |Psi> values = WF.get_values() for index, item in enumerate(values): # tmp is the Vector (0, 0, 0, \psi_i, 0, 0, ...) tmp = [zeros(item.shape) for z in xrange(nst)] tmp[index] = item WF2.set_values(tmp) # Project this vector to the canonical basis BT.transform_to_canonical(WF2) # And calculate the energies of these components ekinlist.append(WF2.kinetic_energy(opT, summed=True)) epotlist.append(WF2.potential_energy(opV, summed=True)) iom.save_energy((ekinlist, epotlist), timestep=step, blockid=blockid) else: # Iterate over all timesteps for i, step in enumerate(timesteps): print(" Computing energies of timestep # " + str(step)) # Retrieve simulation data values = iom.load_wavefunction(timestep=step, blockid=blockid) values = [ values[j, ...] for j in xrange(parameters["ncomponents"]) ] WF.set_values(values) # And calculate the energies of these components ekinlist = WF.kinetic_energy(opT, summed=False) epotlist = WF.potential_energy(opV, summed=False) iom.save_energy((ekinlist, epotlist), timestep=step, blockid=blockid)