def test_delta(fname=None): n_dims = [max_tier] * max_terms heom = Hierachy(n_dims, H, V, corr) # Adopt MCTDH root = simple_heom(rho_0, n_dims) leaves_dict = {leaf.name: leaf for leaf in root.leaves()} all_terms = [] for term in heom.diff(): all_terms.append([(leaves_dict[str(fst)], snd) for fst, snd in term]) solver = MultiLayer(root, all_terms) solver.ode_method = 'RK45' solver.snd_order = False solver.atol = 1.e-7 solver.rtol = 1.e-7 # Define the obersevable of interest logger = Logger(filename=fname, level='info').logger for n, (time, r) in enumerate( solver.propagator( steps=count, ode_inter=dt_unit, #split=False, )): if n % callback_interval == 0: rho = np.reshape(r.array, (-1, 4)) logger.info("{} {} {} {} {}".format(time, rho[0, 0], rho[0, 1], rho[0, 2], rho[0, 3])) return
def test_heom(fname=None): ph_dims = list(np.repeat(model.ph_dims, 2)) n_dims = ph_dims if model.bath_dims is None else ph_dims + model.bath_dims print(n_dims) root = tensor_tree_template(rho_0, n_dims, rank=rank_heom) leaves = root.leaves() h_list = model.heom_h_list(leaves[0], leaves[1], leaves[2:], beta=beta) solver = MultiLayer(root, h_list) solver.ode_method = 'RK45' solver.cmf_steps = solver.max_ode_steps # use constant mean-field solver.ps_method = 'split' #solver.svd_err = 1.0e-10 # Define the obersevable of interest logger = Logger(filename=prefix + fname, level='info').logger logger2 = Logger(filename=prefix + "en_" + fname, level='info').logger for n, (time, r) in enumerate( solver.propagator( steps=count, ode_inter=dt_unit, split=True, )): # renormalized by the trace of rho norm = np.trace(np.reshape(np.reshape(r.array, (4, -1))[:, 0], (2, 2))) r.set_array(r.array / norm) if n % callback_interval == 0: t = Quantity(time).convert_to(unit='fs').value rho = np.reshape(r.array, (4, -1))[:, 0] logger.info("{} {} {} {} {}".format(t, rho[0], rho[1], rho[2], rho[3])) en = np.trace(np.reshape(rho, (2, 2)) @ model.h) logger2.info('{} {}'.format(t, en)) return
def test_simple(fname=None): # Type settings corr.print() n_dims = [max_tier] * max_terms heom = Hierachy(n_dims, H, V, corr) # Adopt MCTDH root = simple_heom(rho_0, n_dims) leaves_dict = {leaf.name: leaf for leaf in root.leaves()} all_terms = [] for term in heom.diff(): all_terms.append([(leaves_dict[str(fst)], snd) for fst, snd in term]) #solver = ProjectorSplitting(root, all_terms) solver = MultiLayer(root, all_terms) solver.ode_method = 'RK45' solver.snd_order = False # Define the obersevable of interest logger = Logger(filename=fname, level='info').logger for n, (time, r) in enumerate(solver.propagator( steps=count, ode_inter=dt_unit, )): try: if n % callback_interval == 0: rho = np.reshape(r.array, (-1, 4))[0] logger.info("{} {} {} {} {}".format(time, rho[0], rho[1], rho[2], rho[3])) except: break return
def test_train(fname=None): # Type settings corr = Correlation(k_max=max_terms) corr.symm_coeff = np.diag(corr_dict['s'].toarray()) corr.asymm_coeff = np.diag(corr_dict['a'].toarray()) corr.exp_coeff = np.diag(corr_dict['gamma'].toarray()) corr.delta_coeff = 0.0 # delta_coeff corr.print() n_dims = [max_tier] * max_terms heom = Hierachy(n_dims, H, V, corr) # Adopt TT tensor_train = tensor_train_template(rho_0, n_dims) root = tensor_train[0] leaves_dict = {leaf.name: leaf for leaf in root.leaves()} all_terms = [] for term in heom.diff(): all_terms.append([(leaves_dict[str(fst)], snd) for fst, snd in term]) solver = MultiLayer(root, all_terms) #solver = ProjectorSplitting(root, all_terms) solver.ode_method = 'RK45' solver.snd_order = False solver.atol = 1.e-7 solver.rtol = 1.e-7 solver.ps_method = 'split-unite' projector = np.zeros((max_tier, 1)) projector[0] = 1.0 logger = Logger(filename=fname, level='info').logger for n, (time, _) in enumerate( solver.propagator(steps=count, ode_inter=dt_unit, split=False)): if n % callback_interval == 0: head = root.array for t in tensor_train[1:]: spf = Tensor.partial_product(t.array, 1, projector, 0) head = Tensor.partial_product(head, head.ndim - 1, spf, 0) rho = np.reshape(head, (4, -1))[:, 0] logger.info("{} {} {} {} {}".format(time, rho[0], rho[1], rho[2], rho[3])) return
def test_train(fname=None): # HEOM metas corr.print() n_dims = [max_tier] * max_terms heom = Hierachy(n_dims, H, V, corr) # 2-site TT tensor_train = tensor_train_template(rho_0, n_dims, rank=1) root = tensor_train[0] leaves_dict = {leaf.name: leaf for leaf in root.leaves()} all_terms = [] for term in heom.diff(): all_terms.append([(leaves_dict[str(fst)], snd) for fst, snd in term]) solver = MultiLayer(root, all_terms) solver.ode_method = 'RK45' solver.snd_order = False solver.svd_err = 1.e-8 solver.svd_rank = max_tier solver.ps_method = 'unite' projector = np.zeros((max_tier, 1)) projector[0] = 1.0 logger = Logger(filename=fname, level='info').logger logger2 = Logger(filename=fname + '_norm', level='info').logger for n, (time, _) in enumerate(solver.propagator(steps=count, ode_inter=dt_unit, split=True)): #print('n = {}: '.format(n)) #for t in tensor_train: # print('{}: {}'.format(t, t.shape)) if n % callback_interval == 0: head = root.array for t in tensor_train[1:]: spf = Tensor.partial_product(t.array, 1, projector, 0) head = Tensor.partial_product(head, head.ndim - 1, spf, 0) rho = np.reshape(head, (4, -1))[:, 0] logger2.warning("{} {}".format(time, rho[0] + rho[3])) logger.info("{} {} {} {} {}".format(time, rho[0], rho[1], rho[2], rho[3])) return
def test_drude(fname=None): # Type settings corr = Correlation(k_max=max_terms) corr.symm_coeff = np.diag(corr_dict['s'].toarray()) corr.asymm_coeff = np.diag(corr_dict['a'].toarray()) corr.exp_coeff = np.diag(corr_dict['gamma'].toarray()) corr.delta_coeff = 0.0 # delta_coeff corr.print() n_dims = [max_tier] * max_terms heom = Hierachy(n_dims, H, V, corr) # Adopt MCTDH root = simple_heom(rho_0, n_dims) leaves_dict = {leaf.name: leaf for leaf in root.leaves()} all_terms = [] for term in heom.diff(): all_terms.append([(leaves_dict[str(fst)], snd) for fst, snd in term]) solver = ProjectorSplitting(root, all_terms) solver.ode_method = 'DOP853' solver.snd_order = False solver.atol = 1.e-7 solver.rtol = 1.e-7 # Define the obersevable of interest logger = Logger(filename=fname, level='info').logger for n, (time, r) in enumerate(solver.propagator( steps=count, ode_inter=dt_unit, )): if n % callback_interval == 0: rho = np.reshape(r.array, (-1, 4)) logger.info("{} {} {} {} {}".format(time, rho[0, 0], rho[0, 1], rho[0, 2], rho[0, 3])) print("Time: {}; Tr rho_0: {}".format(time, rho[0, 0] + rho[0, -1])) return
def test_mctdh(fname=None): sys_leaf = Leaf(name='sys0') ph_leaves = [] for n, (omega, g) in enumerate(ph_parameters, 1): ph_leaf = Leaf(name='ph{}'.format(n)) ph_leaves.append(ph_leaf) def ph_spf(): t = Tensor(axis=0) t.name = 'spf' + str(hex(id(t)))[-4:] return t graph, root = huffman_tree(ph_leaves, obj_new=ph_spf, n_branch=2) try: graph[root].insert(0, sys_leaf) except KeyError: ph_leaf = root root = Tensor() graph[root] = [sys_leaf, ph_leaf] finally: root.name = 'wfn' root.axis = None stack = [root] while stack: parent = stack.pop() for child in graph[parent]: parent.link_to(parent.order, child, 0) if child in graph: stack.append(child) # Define the detailed parameters for the ML-MCTDH tree h_list = model.wfn_h_list(sys_leaf, ph_leaves) solver = MultiLayer(root, h_list) bond_dict = {} # Leaves for s, i, t, j in root.linkage_visitor(): if t.name.startswith('sys'): bond_dict[(s, i, t, j)] = 2 else: if isinstance(t, Leaf): bond_dict[(s, i, t, j)] = max_tier else: bond_dict[(s, i, t, j)] = rank_wfn solver.autocomplete(bond_dict) # set initial root array init_proj = np.array([[A, 0.0], [B, 0.0]]) / np.sqrt(A**2 + B**2) root_array = Tensor.partial_product(root.array, 0, init_proj, 1) root.set_array(root_array) solver = MultiLayer(root, h_list) solver.ode_method = 'RK45' solver.cmf_steps = solver.max_ode_steps # constant mean-field solver.ps_method = 'split' solver.svd_err = 1.0e-14 # Define the obersevable of interest logger = Logger(filename=prefix + fname, level='info').logger logger2 = Logger(filename=prefix + 'en_' + fname, level='info').logger for n, (time, r) in enumerate( solver.propagator( steps=count, ode_inter=dt_unit, split=True, )): if n % callback_interval == 0: t = Quantity(time).convert_to(unit='fs').value rho = r.partial_env(0, proper=False) logger.info("{} {} {} {} {}".format(t, rho[0, 0], rho[0, 1], rho[1, 0], rho[1, 1])) en = np.trace(rho @ model.h) logger2.info('{} {}'.format(t, en))
def ml(fname, e, v, primitive_dim, spf_dim, ph_parameters, steps=2000, ode_inter=0.1): logger = Logger(filename=fname).logger # define parameters sys_hamiltonian = np.array([[e, v], [v, -e]], dtype=DTYPE) projector = np.array([[1.0, 0.0], [0.0, -1.0]], dtype=DTYPE) # S in H_SB = S x B primitive_dim = primitive_dim spf_dim = spf_dim # Define all Leaf tensors and hamiltonian we need h_list = [] sys_leaf = Leaf(name='sys0') h_list.append([(sys_leaf, -1.0j * sys_hamiltonian)]) ph_parameters = ph_parameters leaves = [] for n, (omega, g) in enumerate(ph_parameters, 1): ph = Phonon(primitive_dim, omega) ph_leaf = Leaf(name='ph{}'.format(n)) leaves.append(ph_leaf) # hamiltonian ph part h_list.append([(ph_leaf, -1.0j * ph.hamiltonian)]) # e-ph part op = ph.annihilation_operator + ph.creation_operator h_list.append([(ph_leaf, g * op), (sys_leaf, -1.0j * projector)]) def ph_spf(): t = Tensor(axis=0, normalized=True) t.name = str(hex(id(t)))[-4:] return t graph, root = huffman_tree(leaves, obj_new=ph_spf, n_branch=2) try: graph[root].insert(0, sys_leaf) except KeyError: ph_leaf = root root = Tensor() graph[root] = [sys_leaf, ph_leaf] finally: root.name = 'wfn' root.axis = None root.normalized = True stack = [root] while stack: parent = stack.pop() for child in graph[parent]: parent.link_to(parent.order, child, 0) if child in graph: stack.append(child) logger.info(f"graph:{graph}") # Define the detailed parameters for the ML-MCTDH tree solver = MultiLayer(root, h_list) bond_dict = {} # Leaves for s, i, t, j in root.linkage_visitor(): if t.name and t.name.startswith('sys'): bond_dict[(s, i, t, j)] = 2 else: if isinstance(t, Leaf): bond_dict[(s, i, t, j)] = primitive_dim else: bond_dict[(s, i, t, j)] = spf_dim solver.autocomplete(bond_dict) logger.info(f"bond_dict:{bond_dict}") # set initial root array a, b = 1.0, 0 init_proj = np.array([[a, 0.0], [b, 0.0]]) / np.sqrt(a**2 + b**2) root_array = Tensor.partial_product(root.array, 0, init_proj, 1) root.set_array(root_array) # Define the computation details solver.ode_method = 'RK45' solver.snd_order = False solver.cmf_steps = 100 # Define the obersevable of interest logger.info('''# time rho00 rho01 rho10 rho11''') for time, _ in solver.propagator( steps=steps, ode_inter=ode_inter, split=True, ): t = time for tensor in root.visitor(axis=None): tensor.reset() tensor.normalize(forced=True) rho = root.partial_env(0, proper=False) for tensor in root.visitor(axis=None): tensor.reset() flat_data = [t] + list(np.reshape(rho, -1)) logger.info('{} {} {} {} {}'.format(*flat_data))
def ml(dof, e, v, eta, cutoff, scale=5, loc=None, steps=2000, ode_inter=0.1): f_ = 'dof{}-eta{}.log'.format(dof, eta) logger = Logger(filename=f_).logger # define parameters e = Quantity(e, 'cm-1').value_in_au v = Quantity(v, 'cm-1').value_in_au eta = Quantity(eta, 'cm-1').value_in_au omega0 = Quantity(cutoff, 'cm-1').value_in_au sys_hamiltonian = np.array([[-e / 2.0, v], [v, e / 2.0]], dtype=DTYPE) projector = np.array([[0.0, 0.0], [0.0, 1.0]], dtype=DTYPE) # S in H_SB = S x B primitive_dim = 100 spf_dim = 20 # Spectrum function def spec_func(omega): if 0 < omega < omega0: return eta else: return 0.0 # Define all Leaf tensors and hamiltonian we need h_list = [] sys_leaf = Leaf(name='sys0') h_list.append([(sys_leaf, -1.0j * sys_hamiltonian)]) ph_parameters = linear_discretization(spec_func, omega0, dof) if loc is not None: adj_pair = (ph_parameters[loc][0], ph_parameters[loc][1] * scale) ph_parameters[loc] = adj_pair leaves = [] for n, (omega, g) in enumerate(ph_parameters, 1): ph = Phonon(primitive_dim, omega) ph_leaf = Leaf(name='ph{}'.format(n)) leaves.append(ph_leaf) # hamiltonian ph part h_list.append([(ph_leaf, -1.0j * ph.hamiltonian)]) # e-ph part op = ph.annihilation_operator + ph.creation_operator h_list.append([(ph_leaf, g * op), (sys_leaf, -1.0j * projector)]) def ph_spf(n=0): n += 1 return Tensor(name='spf{}'.format(n), axis=0) graph, root = huffman_tree(leaves, obj_new=ph_spf, n_branch=2) try: graph[root].insert(0, sys_leaf) except KeyError: ph_leaf = root root = Tensor() graph[root] = [sys_leaf, ph_leaf] finally: root.name = 'wfn' root.axis = None print(graph) stack = [root] while stack: parent = stack.pop() for child in graph[parent]: parent.link_to(parent.order, child, 0) if child in graph: stack.append(child) # Define the detailed parameters for the ML-MCTDH tree solver = MultiLayer(root, h_list) bond_dict = {} # Leaves for s, i, t, j in root.linkage_visitor(): if t.name.startswith('sys'): bond_dict[(s, i, t, j)] = 2 else: if isinstance(t, Leaf): bond_dict[(s, i, t, j)] = primitive_dim else: bond_dict[(s, i, t, j)] = spf_dim solver.autocomplete(bond_dict) # set initial root array a, b = 1.0, 1.0 init_proj = np.array([[a, 0.0], [b, 0.0]]) / np.sqrt(a**2 + b**2) root_array = Tensor.partial_product(root.array, 0, init_proj, 1) root.set_array(root_array) # Define the computation details solver.ode_method = 'RK45' solver.snd_order = True solver.cmf_steps = 1 root.is_normalized = True # Define the obersevable of interest logger.info('''# time/fs rho00 rho01 rho10 rho11''') for time, _ in solver.propagator( steps=steps, ode_inter=Quantity(ode_inter, 'fs').value_in_au, split=True, ): t = Quantity(time).convert_to(unit='fs').value for tensor in root.visitor(axis=None): tensor.reset() rho = root.partial_env(0, proper=False) for tensor in root.visitor(axis=None): tensor.reset() flat_data = [t] + list(np.reshape(rho, -1)) logger.info('{} {} {} {} {}'.format(*flat_data))