예제 #1
0
def compute_evaluate_wavepackets(iom, basis="eigen", blockid=0):
    """Evaluate a homogeneous Hagdorn wavepacket on a given grid for each timestep.
    :param iom: An ``IOManager`` instance providing the simulation data.
    :param basis: The basis where the evaluation is done. Can be 'eigen' or 'canonical'.
    :param blockid: The data block from which the values are read.
    """
    parameters = iom.load_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 = PotentialFactory().create_potential(parameters)

    # Retrieve simulation data
    if iom.has_grid(blockid=blockid):
        grid = iom.load_grid(blockid=blockid)
    else:
        grid = iom.load_grid(blockid="global")

    params = iom.load_wavepacket_parameters(blockid=blockid)
    coeffs = iom.load_wavepacket_coefficients(blockid=blockid)

    # A data transformation needed by API specification
    coeffs = [ [ coeffs[i,j,:] for j in xrange(parameters["ncomponents"]) ] for i in xrange(nrtimesteps)]

    # We want to save wavefunctions, thus add a data slot to the data file
    iom.add_wavefunction(parameters, timeslots=nrtimesteps, blockid=blockid)

    # Hack for allowing data blocks with different basis size than the global one
    # todo: remove when we got local parameter sets
    parameters.update_parameters({"basis_size": coeffs[0][0].shape[0]})

    HAWP = HagedornWavepacket(parameters)
    HAWP.set_quadrature(None)

    WF = WaveFunction(parameters)
    WF.set_grid(grid)

    # Iterate over all timesteps
    for i, step in enumerate(timesteps):
        print(" Evaluating homogeneous wavepacket at timestep "+str(step))

        # Configure the wavepacket
        HAWP.set_parameters(params[i])
        HAWP.set_coefficients(coeffs[i])

        # Project to the eigenbasis if desired
        if basis == "eigen":
            HAWP.project_to_eigen(Potential)

        # 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)
예제 #2
0
def aposteriori_spawning(fin, fout, pin, pout, bid1, bid2):
    """
    :param f: An ``IOManager`` instance providing the simulation data.
    :param datablock: The data block where the results are.
    """
    # Number of time steps we saved
    timesteps = fin.load_wavepacket_timegrid()
    nrtimesteps = timesteps.shape[0]

    params = fin.load_wavepacket_parameters()
    coeffs = fin.load_wavepacket_coefficients()

    # A data transformation needed by API specification
    coeffs = [ [ coeffs[i,j,:] for j in xrange(pin["ncomponents"]) ] for i in xrange(nrtimesteps) ]

    # Initialize a mother Hagedorn wavepacket with the data from another simulation
    HAWP = HagedornWavepacket(pin)
    HAWP.set_quadrature(None)

    # Initialize an empty wavepacket for spawning
    SWP = HagedornWavepacket(pout)
    SWP.set_quadrature(None)

    # Initialize a Spawner
    AS = AdiabaticSpawner(pout)

    # Iterate over all timesteps and spawn
    for i, step in enumerate(timesteps):
        print(" Try spawning at timestep "+str(step))

        # Configure the wave packet and project to the eigenbasis.
        HAWP.set_parameters(params[i])
        HAWP.set_coefficients(coeffs[i])
        #HAWP.project_to_eigen(Potential)

        # Try spawning a new packet
        ps = AS.estimate_parameters(HAWP, 0)

        if ps is not None:
            SWP.set_parameters(ps)
            AS.project_coefficients(HAWP, SWP)

            # Save the spawned packet
            fout.save_wavepacket_parameters(HAWP.get_parameters(), timestep=step, blockid=bid1)
            fout.save_wavepacket_coefficients(HAWP.get_coefficients(), timestep=step, blockid=bid1)

            fout.save_wavepacket_parameters(SWP.get_parameters(), timestep=step, blockid=bid2)
            fout.save_wavepacket_coefficients(SWP.get_coefficients(), timestep=step, blockid=bid2)
def compute_energy(iom, blockid=0):
    p = iom.load_parameters()

    # Number of time steps we saved
    timesteps = iom.load_wavepacket_timegrid(blockid=blockid)
    nrtimesteps = timesteps.shape[0]

    # We want to save energies, thus add a data slot to the data file
    iom.add_energy(p, timeslots=nrtimesteps, blockid=blockid, total=True)

    Potential = PotentialFactory().create_potential(p)

    params = iom.load_wavepacket_parameters(blockid=blockid)
    coeffs = iom.load_wavepacket_coefficients(blockid=blockid)

    # A data transformation needed by API specification
    coeffs = [ [ coeffs[i,j,:] for j in xrange(p.ncomponents) ] for i in xrange(nrtimesteps) ]

    # Initialize a hagedorn wave packet with the data
    HAWP = HagedornWavepacket(p)
    HAWP.set_quadrature(None)

    # Iterate over all timesteps
    for i, step in enumerate(timesteps):
        print(" Computing energies of timestep "+str(step))

        # Configure the wave packet
        HAWP.set_parameters(params[i])
        HAWP.set_coefficients(coeffs[i])

        # Compute overall energy in canonical basis
        ekin = HAWP.kinetic_energy(summed=True)
        epot = HAWP.potential_energy(Potential.evaluate_at, summed=True)
        etot = ekin + epot

        iom.save_energy_total(etot, timestep=step, blockid=blockid)

        # Transform to eigenbasis
        HAWP.project_to_eigen(Potential)

        # Compute the components' energies in the eigenbasis
        ekin = HAWP.kinetic_energy()
        epot = HAWP.potential_energy(Potential.evaluate_eigenvalues_at)

        iom.save_energy((ekin, epot), timestep=step, blockid=blockid)
예제 #4
0
def compute_energy(iom, blockid=0):
    """
    :param iom: An ``IOManager`` instance providing the simulation data.
    :param blockid: The data block from which the values are read.
    """
    parameters = iom.load_parameters()

    # Number of time steps we saved
    timesteps = iom.load_wavepacket_timegrid(blockid=blockid)
    nrtimesteps = timesteps.shape[0]

    Potential = PotentialFactory().create_potential(parameters)

    # Retrieve simulation data
    params = iom.load_wavepacket_parameters(blockid=blockid)
    coeffs = iom.load_wavepacket_coefficients(blockid=blockid)

    # A data transformation needed by API specification
    coeffs = [ [ coeffs[i,j,:] for j in xrange(parameters["ncomponents"]) ] for i in xrange(nrtimesteps) ]

    # We want to save energies, thus add a data slot to the data file
    iom.add_energy(parameters, timeslots=nrtimesteps, blockid=blockid)

    # Hack for allowing data blocks with different basis size than the global one
    # todo: remove when we got local parameter sets
    parameters.update_parameters({"basis_size": coeffs[0][0].shape[0]})

    # Initialize a Hagedorn wave packet with the data
    HAWP = HagedornWavepacket(parameters)
    HAWP.set_quadrature(None)

    # Iterate over all timesteps
    for i, step in enumerate(timesteps):
        print(" Computing energies of timestep "+str(step))

        # Configure the wave packet and project to the eigenbasis.
        HAWP.set_parameters(params[i])
        HAWP.set_coefficients(coeffs[i])
        HAWP.project_to_eigen(Potential)

        # Compute the energies
        ekin = HAWP.kinetic_energy()
        epot = HAWP.potential_energy(Potential.evaluate_eigenvalues_at)

        iom.save_energy((ekin, epot), timestep=step, blockid=blockid)
예제 #5
0
def plot_frames_homogeneous(iom, blockid=0, view=None):
    """
    :param iom: An ``IOManager`` instance providing the simulation data.
    """
    parameters = iom.load_parameters()

    # Number of time steps we saved
    timesteps = iom.load_wavepacket_timegrid(blockid=blockid)
    nrtimesteps = timesteps.shape[0]

    # Initialize a Hagedorn wavepacket with the data
    Potential = PotentialFactory().create_potential(parameters)

    # Retrieve simulation data
    grid = iom.load_grid(blockid="global")
    params = iom.load_wavepacket_parameters(blockid=blockid)
    coeffs = iom.load_wavepacket_coefficients(blockid=blockid)

    # A data transformation needed by API specification
    coeffs = [ [ coeffs[i,j,:] for j in xrange(parameters["ncomponents"]) ] for i in xrange(nrtimesteps)]

    # Hack for allowing data blocks with different basis size than the global one
    # todo: remove when we got local parameter sets
    parameters.update_parameters({"basis_size": coeffs[0][0].shape[0]})

    HAWP = HagedornWavepacket(parameters)
    HAWP.set_quadrature(None)

    # Iterate over all timesteps
    for i, step in enumerate(timesteps):
        print(" Plotting frame of timestep "+str(step))

        # Configure the wavepacket and project to the eigenbasis.
        HAWP.set_parameters(params[i])
        HAWP.set_coefficients(coeffs[i])
        HAWP.project_to_eigen(Potential)

        values = HAWP.evaluate_at(grid, prefactor=True)
        coeffi = HAWP.get_coefficients()

        plot_frame(step, parameters, grid, values, coeffi, index=blockid, view=view)

    print(" Plotting frames finished")
def aposteriori_spawning(fin, fout, pin, pout, save_canonical=False):
    """
    :param f: An ``IOManager`` instance providing the simulation data.
    :param datablock: The data block where the results are.
    """
    # Number of time steps we saved
    timesteps = fin.load_wavepacket_timegrid()
    nrtimesteps = timesteps.shape[0]

    params = fin.load_wavepacket_parameters()
    coeffs = fin.load_wavepacket_coefficients()

    # A data transformation needed by API specification
    coeffs = [ [ coeffs[i,j,:] for j in xrange(pin["ncomponents"]) ] for i in xrange(nrtimesteps) ]

    # The potential
    Potential = PotentialFactory().create_potential(pin)

    # Initialize a mother Hagedorn wavepacket with the data from another simulation
    HAWP = HagedornWavepacket(pin)
    HAWP.set_quadrature(None)

    # Initialize an empty wavepacket for spawning
    SWP = HagedornWavepacket(pout)
    SWP.set_quadrature(None)

    # Initialize a Spawner
    NAS = NonAdiabaticSpawnerKF(pout)

    # Try spawning for these components, if none is given, try it for all.
    if not "spawn_components" in parametersout:
        components = range(pin["ncomponents"])
    else:
        components = parametersout["spawn_components"]

    # Iterate over all timesteps and spawn
    for i, step in enumerate(timesteps):
        print(" Try spawning at timestep "+str(step))

        # Configure the wave packet and project to the eigenbasis.
        HAWP.set_parameters(params[i])
        HAWP.set_coefficients(coeffs[i])

        # Project to the eigenbasis as the parameter estimation
        # has to happen there because of coupling.
        T = HAWP.clone()
        T.project_to_eigen(Potential)

        # Try spawning a new packet for each component
        estps = [ NAS.estimate_parameters(T, component=acomp) for acomp in components ]

        # The quadrature
        quadrature = InhomogeneousQuadrature()

        # Quadrature, assume same quadrature order for both packets
        # Assure the "right" quadrature is choosen if mother and child have
        # different basis sizes
        if max(HAWP.get_basis_size()) > max(SWP.get_basis_size()):
            quadrature.set_qr(HAWP.get_quadrature().get_qr())
        else:
            quadrature.set_qr(SWP.get_quadrature().get_qr())

        for index, ps in enumerate(estps):
            if ps is not None:
                # One choice of the sign
                U = SWP.clone()
                U.set_parameters(ps)
                # Project the coefficients to the spawned packet
                tmp = T.clone()
                NAS.project_coefficients(tmp, U, component=components[index])

                # Other choice of the sign
                V = SWP.clone()
                # Transform parameters
                psm = list(ps)
                B = ps[0]
                Bm = -np.real(B)+1.0j*np.imag(B)
                psm[0] = Bm
                V.set_parameters(psm)
                # Project the coefficients to the spawned packet
                tmp = T.clone()
                NAS.project_coefficients(tmp, V, component=components[index])

                # Compute some inner products to finally determine which parameter set we use
                ou = abs(quadrature.quadrature(T,U, component=components[index]))
                ov = abs(quadrature.quadrature(T,V, component=components[index]))

                # Choose the packet which maximizes the inner product. This is the main point!
                if ou >= ov:
                    U = U
                else:
                    U = V

                # Finally do the spawning, this is essentially to get the remainder T right
                # The packet U is already ok by now.
                NAS.project_coefficients(T, U, component=components[index])

                # Transform back
                if save_canonical is True:
                    T.project_to_canonical(Potential)
                    U.project_to_canonical(Potential)

                # Save the mother packet rest
                fout.save_wavepacket_parameters(T.get_parameters(), timestep=step, blockid=2*index)
                fout.save_wavepacket_coefficients(T.get_coefficients(), timestep=step, blockid=2*index)

                # Save the spawned packet
                fout.save_wavepacket_parameters(U.get_parameters(), timestep=step, blockid=2*index+1)
                fout.save_wavepacket_coefficients(U.get_coefficients(), timestep=step, blockid=2*index+1)
예제 #7
0
from matplotlib.pyplot import *

from WaveBlocks import HagedornWavepacket
from WaveBlocks import HomogeneousQuadrature
from WaveBlocks import InhomogeneousQuadrature

nmax = 6

params = {}
params["eps"] = 0.2
params["basis_size"] = 4
params["ncomponents"] = 1


WP1 = HagedornWavepacket(params)
WP1.set_parameters((1.0j, 1.0, 0.0, 0.0, 0.85))
WP1.set_coefficient(0, 0, 1)
WP1.set_quadrature(None)

params["basis_size"] = 6

WP2 = HagedornWavepacket(params)
WP2.set_coefficient(0, 0, 1)
WP2.set_quadrature(None)


HQ1 = HomogeneousQuadrature()
HQ1.build_qr(max(WP1.get_basis_size()))
HQ2 = HomogeneousQuadrature()
HQ2.build_qr(max(WP2.get_basis_size()))
x = params["f"] * pi * arange(-1, 1, 2.0/params["ngn"], dtype=np.complexfloating)

# Fourierspace grid
omega_1 = arange(-params["ngn"]/2.0, 0, 1)
omega_2 = arange(0, params["ngn"]/2.0)
omega = hstack([omega_1, omega_2])

k = omega / (2*params["f"] * pi)
#k = omega / params["ngn"]


# A HAWP
HAWP = HagedornWavepacket(params)
HAWP.set_quadrature(None)

HAWP.set_parameters((1j, 1, 0, 3, -2))
HAWP.set_coefficient(0,2,1)

# Evaluate in real space
y = HAWP.evaluate_at(x)[0]

# Transform to Fourier space
HAWP.to_fourier_space()
# Evaluate in Fourier space
w = HAWP.evaluate_at(k)[0]

HAWP.to_real_space()
# Evaluate in real space again
z = HAWP.evaluate_at(x)[0]

예제 #9
0
@copyright: Copyright (C) 2010, 2011 R. Bourquin
@license: Modified BSD License
"""

from numpy import *
from matplotlib.pyplot import *

from WaveBlocks import HagedornWavepacket
from WaveBlocks.Plot import plotcf

nmax = 10
amp0 = 0.5

params = {}
params["eps"] = 0.2
params["basis_size"] = nmax
params["ncomponents"] = 1

HAWP = HagedornWavepacket(params)
HAWP.set_parameters((1.0j, 1.0, 0.0, -1.0, 0.0))

for i in range(0,nmax):
    HAWP.set_coefficient(0, i, amp0/ 2**i)

x = linspace(-4,4,4000)
y = HAWP.evaluate_at(x, prefactor=True, component=0)

figure()
plotcf(x, angle(y), conj(y)*y)
show()