def modal_decomposition(obj, kind='pod', obj2=None, wanted_modes='all', max_vecs_per_node=1000, verbose=True): """ Compute POD modes of the given fields using the snapshot method. Parameters ---------- obj : TemporalFields, Profile or Points object Fields to extract modes from obj2 : same as obj Only used as second dataset for BPOD kind : string, optional Kind of decomposition, can be 'pod' (default), 'bpod' or 'dmd'. wanted_modes : string or number or array of numbers If 'all', extract all modes, If a number, extract first modes, If an array, extract the associated modes. max_vecs_per_node : integer, optional Number of fields that can be charged in memory. (More is faster but can lead to MemoryError) verbose : boolean, optional If 'True', display information. Returns ------- modal_field : ModalField object . Notes ----- You can use partially masked fields as input. If so, the asked values are lineary interpolated before doing the decomposition. """ # check if modred is available if not MODRED: raise Exception("This feature need 'modred' to be installed") # Test parameters if not isinstance(obj, (TemporalFields)): raise TypeError() if kind == "bpod": if obj2 is None: raise ValueError() if not isinstance(obj2, obj.__class__): raise TypeError() if not obj2.shape == obj.shape: raise ValueError() if not isinstance(kind, STRINGTYPES): raise TypeError() if kind not in ['pod', 'bpod', 'dmd']: raise ValueError() if isinstance(wanted_modes, STRINGTYPES): if not wanted_modes == 'all': raise ValueError() wanted_modes = np.arange(len(obj.fields)) elif isinstance(wanted_modes, NUMBERTYPES): wanted_modes = np.arange(wanted_modes) elif isinstance(wanted_modes, ARRAYTYPES): wanted_modes = np.array(wanted_modes) if wanted_modes.min() < 0 or wanted_modes.max() > len(obj.fields): raise ValueError() else: raise TypeError() try: max_vecs_per_node = int(max_vecs_per_node) except: raise TypeError() # getting datas if isinstance(obj, TemporalScalarFields): obj_type = 'TSF' snaps, props = _tsf_to_POD(obj) if kind == "bpod": snaps2, _ = _tsf_to_POD(obj2) elif isinstance(obj, TemporalVectorFields): obj_type = 'TVF' snaps, props = _tvf_to_POD(obj) if kind == "bpod": snaps2, _ = _tvf_to_POD(obj2) else: raise TypeError() globals().update(props) # Setting the decomposition mode eigvals = None eigvect = None ritz_vals = None mode_norms = None growth_rate = None pulsation = None if kind == 'pod': my_decomp = modred.PODHandles(np.vdot, max_vecs_per_node=max_vecs_per_node, verbosity=verbose) eigvals, eigvect = my_decomp.compute_decomp(snaps) del snaps wanted_modes = wanted_modes[wanted_modes < len(eigvals)] eigvect = np.array(eigvect) eigvect = eigvect[:, wanted_modes] eigvals = Profile(wanted_modes, eigvals[wanted_modes], mask=False, unit_x=props['unit_times'], unit_y='') elif kind == 'bpod': my_decomp = modred.BPODHandles(np.vdot, max_vecs_per_node=max_vecs_per_node, verbosity=verbose) eigvect, eigvals, eigvect_l = my_decomp.compute_decomp(snaps, snaps2) del snaps del snaps2 wanted_modes = wanted_modes[wanted_modes < len(eigvals)] eigvect = np.array(eigvect) eigvect = eigvect[:, wanted_modes] eigvals = Profile(wanted_modes, eigvals[wanted_modes], mask=False, unit_x=props['unit_times'], unit_y='') elif kind == 'dmd': my_decomp = modred.DMDHandles(np.vdot, max_vecs_per_node=max_vecs_per_node, verbosity=verbose) ritz_vals, mode_norms, build_coeffs = my_decomp.compute_decomp(snaps) del snaps wanted_modes = wanted_modes[wanted_modes < len(ritz_vals)] # supplementary charac delta_t = props['times'][1] - props['times'][0] lambd_i = np.imag(ritz_vals) lambd_r = np.real(ritz_vals) lambd_mod = np.abs(ritz_vals) lambd_arg = np.zeros((len(ritz_vals))) mask = np.logical_and(lambd_i == 0, lambd_r <= 0) filt = np.logical_not(mask) lambd_arg[mask] = np.pi lambd_arg[filt] = 2*np.arctan(lambd_i[filt]/(lambd_r[filt] + lambd_mod[filt])) sigma = np.log(lambd_mod)/delta_t omega = lambd_arg/delta_t # creating profiles unit_times = props['unit_times'] ritz_vals = Profile(wanted_modes, ritz_vals[wanted_modes], mask=False, unit_x=unit_times, unit_y='') mode_norms = Profile(wanted_modes, mode_norms[wanted_modes], mask=False, unit_x=unit_times, unit_y='') growth_rate = Profile(wanted_modes, sigma[wanted_modes], mask=False, unit_x=unit_times, unit_y=1./unit_times) pulsation = Profile(wanted_modes, omega[wanted_modes], mask=False, unit_x=unit_times, unit_y=make_unit('rad')/unit_times) else: raise ValueError("Unknown kind of decomposition : {}".format(kind)) f_shape = props['f_shape'] unit_values = props['unit_values'] unit_times = props['unit_times'] times = props['times'] ind_fields = props['ind_fields'] # Decomposing and getting modes if kind in ['pod', 'dmd']: modes = [modred.VecHandleInMemory(np.zeros(f_shape)) for i in np.arange(len(wanted_modes))] my_decomp.compute_modes(wanted_modes, modes) elif kind in ['bpod']: modes = [modred.VecHandleInMemory(np.zeros(f_shape)) for i in np.arange(len(wanted_modes))] adj_modes = [modred.VecHandleInMemory(np.zeros(f_shape)) for i in np.arange(len(wanted_modes))] my_decomp.compute_direct_modes(wanted_modes, modes) my_decomp.compute_adjoint_modes(wanted_modes, adj_modes) # Getting temporal evolution temporal_prof = [] if kind in ['pod']: for n in np.arange(len(modes)): tmp_prof = eigvect[:, n]*eigvals.y[n]**.5 tmp_prof = Profile(times, tmp_prof, mask=False, unit_x=unit_times, unit_y=unit_values) temporal_prof.append(tmp_prof) if kind in ['bpod']: for n in np.arange(len(modes)): tmp_prof = eigvect[:, n]*eigvals.y[n]**.5 tmp_times = times[0:len(tmp_prof)] tmp_prof = Profile(tmp_times, tmp_prof, mask=False, unit_x=unit_times, unit_y=unit_values) temporal_prof.append(tmp_prof) elif kind == 'dmd': for n in np.arange(len(modes)): tmp_prof = np.real([ritz_vals.y[n]**(k) for k in ind_fields]) tmp_prof = Profile(times, tmp_prof, mask=False, unit_x=unit_times, unit_y=obj.unit_values) temporal_prof.append(tmp_prof) # Returning if obj_type == "TSF": modes_f = _POD_to_tsf(modes, props) elif obj_type == "TVF": modes_f = _POD_to_tvf(modes, props) del modes modal_field = ModalFields(kind, props['mean_field'], modes_f, wanted_modes, temporal_prof, eigvals=eigvals, eigvects=eigvect, ritz_vals=ritz_vals, mode_norms=mode_norms, growth_rate=growth_rate, pulsation=pulsation) return modal_field
base_vec_handle = mr.VecHandlePickle('base_vec.pkl') snapshots = [ mr.VecHandlePickle('vec%d.pkl' % i, base_vec_handle=base_vec_handle, scale=quad_weights[i]) for i in range(num_vecs) ] # Save arbitrary data, normally unnecessary. num_elements = 2000 parallel = mr.parallel_default_instance if parallel.is_rank_zero(): for snap in snapshots + [base_vec_handle]: snap.put(np.random.random(num_elements)) parallel.barrier() # Compute and save POD modes. my_POD = mr.PODHandles(np.vdot) my_POD.compute_decomp(snapshots) my_POD.put_decomp('sing_vals.txt', 'sing_vecs.txt') my_POD.put_correlation_mat('correlation_mat.txt') mode_indices = [1, 4, 5, 0, 10] modes = [mr.VecHandleArrayText('mode%d.txt' % i) for i in mode_indices] my_POD.compute_modes(mode_indices, modes) # Check that modes are orthonormal vec_space = mr.VectorSpaceHandles(inner_product=np.vdot) IP_mat = vec_space.compute_symmetric_inner_product_mat(modes) if not np.allclose(IP_mat, np.eye(len(mode_indices))): print('Warning: modes are not orthonormal', IP_mat)