def method(ts, dt=0, **kwargs): """ Analyze geometry in time.""" info_cyan("Analyzing the evolution of the geometry through time.") if not ts.get_parameter("enable_PF"): print "Phase field not enabled." return False f_mask = df.Function(ts.function_space) f_mask_x = [] f_mask_u = [] for d in range(ts.dim): f_mask_x.append(df.Function(ts.function_space)) f_mask_u.append(df.Function(ts.function_space)) length = np.zeros(len(ts)) area = np.zeros(len(ts)) com = np.zeros((len(ts), ts.dim)) u = np.zeros((len(ts), ts.dim)) makedirs_safe(os.path.join(ts.analysis_folder, "contour")) steps = get_steps(ts, dt) for step in steps: info("Step " + str(step) + " of " + str(len(ts))) phi = ts["phi", step][:, 0] mask = 0.5 * (1. - phi) # 0.5*(1.-np.sign(phi)) ts.set_val(f_mask, mask) for d in range(ts.dim): ts.set_val(f_mask_x[d], mask * ts.nodes[:, d]) ts.set_val(f_mask_u[d], mask * ts["u", step][:, d]) contour_file = os.path.join(ts.analysis_folder, "contour", "contour_{:06d}.dat".format(step)) paths = zero_level_set(ts.nodes, ts.elems, phi, save_file=contour_file) length[step] = path_length(paths) area[step] = df.assemble(f_mask * df.dx) for d in range(ts.dim): com[step, d] = df.assemble(f_mask_x[d] * df.dx) u[step, d] = df.assemble(f_mask_u[d] * df.dx) for d in range(ts.dim): com[:, d] /= area u[:, d] /= area if rank == 0: np.savetxt(os.path.join(ts.analysis_folder, "time_data.dat"), np.array( zip(np.arange(len(ts)), ts.times, length, area, com[:, 0], com[:, 1], u[:, 0], u[:, 1])), header=("Timestep\tTime\tLength\tArea\t" "CoM_x\tCoM_y\tU_x\tU_y"))
def __init__(self, folder, sought_fields=None, get_mesh_from=False, memory_modest=True): self.folder = folder self.settings_folder = os.path.join(folder, "Settings") self.timeseries_folder = os.path.join(folder, "Timeseries") self.statistics_folder = os.path.join(folder, "Statistics") self.analysis_folder = os.path.join(folder, "Analysis") self.plots_folder = os.path.join(folder, "Plots") self.tmp_folder = os.path.join(folder, ".tmp") self.memory_modest = memory_modest self.params_prefix = os.path.join(self.settings_folder, "parameters_from_tstep_") self.params_suffix = ".dat" self.parameters = dict() self.nodes = None self.elems = None self.times = dict() self.datasets = dict() self._load_timeseries(sought_fields) if len(self.fields) > 0: self._load_mesh(get_mesh_from) self.dummy_function = df.Function(self.function_space) makedirs_safe(self.analysis_folder) makedirs_safe(self.plots_folder) makedirs_safe(self.tmp_folder)
def method(ts, dx=0.1, line="[0.,0.]--[1.,1.]", time=None, dt=None, skip=0, **kwargs): """ Probe along a line. """ info_cyan("Probe along a line.") try: x_a, x_b = [tuple(eval(pt)) for pt in line.split("--")] assert (len(x_a) == ts.dim) assert (len(x_b) == ts.dim) assert (all([ bool(isinstance(xd, float) or isinstance(xd, int)) for xd in list(x_a) + list(x_b) ])) except: info_on_red("Faulty line format. Use 'line=[x1,y1]--[x2,y2]'.") exit() x = np.array(line_points(x_a, x_b, dx)) info("Probes {num} points from {a} to {b}".format(num=len(x), a=x_a, b=x_b)) if rank == 0: plot_probes(ts.nodes, ts.elems, x, colorbar=False, title="Probes") f = ts.functions() probes = dict() from fenicstools import Probes for field, func in f.iteritems(): probes[field] = Probes(x.flatten(), func.function_space()) steps = get_steps(ts, dt, time) for step in steps: info("Step " + str(step) + " of " + str(len(ts))) ts.update_all(f, step) for field, probe in probes.iteritems(): probe(f[field]) probe_arr = dict() for field, probe in probes.iteritems(): probe_arr[field] = probe.array() if rank == 0: for i, step in enumerate(steps): chunks = [x] header_list = [index2letter(d) for d in range(ts.dim)] for field, chunk in probe_arr.iteritems(): if chunk.ndim == 1: header_list.append(field) chunk = chunk[:].reshape(-1, 1) elif chunk.ndim == 2: header_list.append(field) chunk = chunk[:, i].reshape(-1, 1) elif chunk.ndim > 2: header_list.extend( [field + "_" + index2letter(d) for d in range(ts.dim)]) chunk = chunk[:, :, i] chunks.append(chunk) data = np.hstack(chunks) header = "\t".join(header_list) makedirs_safe(os.path.join(ts.analysis_folder, "probes")) np.savetxt(os.path.join(ts.analysis_folder, "probes", "probes_{:06d}.dat".format(step)), data, header=header)