def number_density(job): dim = 1 box_range = [0.5, 1.5] pore_center = (box_range[1] - box_range[0]) / 2 + box_range[0] o_densities = list() h_densities = list() fig, ax = plt.subplots() for trj in md.iterload( os.path.join(job.ws, "carbon_water-pos-1.pdb"), top=os.path.join(job.ws, "init.mol2"), chunk=5000, skip=6000, ): water_o = trj.atom_slice(trj.topology.select("name O")) water_h = trj.atom_slice(trj.topology.select("name H")) area = trj.unitcell_lengths[0][0] * trj.unitcell_lengths[0][2] for water_trj in (water_o, water_h): bins, density = compute_density(water_trj, area, dim, pore_center=pore_center, bin_width=0.01) label_name = list(set([i.name for i in water_trj.topology.atoms])) if label_name[0] == "O": o_densities.append(density) else: h_densities.append(density) o_mean = np.mean(o_densities, axis=0) h_mean = np.mean(h_densities, axis=0) o_std = np.std(o_densities, axis=0) h_std = np.std(h_densities, axis=0) plt.plot(bins, o_mean, label="O") plt.fill_between(bins, o_mean + o_std, o_mean - o_std, alpha=0.2) plt.plot(bins, h_mean, label="H") plt.fill_between(bins, h_mean + h_std, h_mean - h_std, alpha=0.2) plt.xlabel("z-position (nm)") plt.ylabel("Number Density ($nm^-3$)") plt.legend() with job: np.savetxt( project.root_directory() + "/data/{}/o_density.txt".format(str(job.sp.nwater) + "water_data"), np.transpose(np.vstack([bins, o_mean, o_std])), header="Bins\tDensity_mean\tDensity_std", ) np.savetxt( project.root_directory() + "/data/{}/h_density.txt".format(str(job.sp.nwater) + "water_data"), np.transpose(np.vstack([bins, h_mean, h_std])), header="Bins\tDensity_mean\tDensity_std", ) plt.savefig(project.root_directory() + "/data/{}/numberdensity.pdf".format( str(job.sp.nwater) + "water_data"))
def number_density(job): dim = 1 box_range = [0.837, 2.837] pore_center = (box_range[1] - box_range[0]) / 2 + box_range[0] o_densities = list() h_densities = list() fig, ax = plt.subplots() for trj in md.iterload(os.path.join(job.ws, 'nvt.dcd'), top=os.path.join(job.ws, 'init.gro'), chunk=1000, skip=5001): water_o = trj.atom_slice(trj.topology.select('name O')) water_h = trj.atom_slice(trj.topology.select('name H')) area = trj.unitcell_lengths[0][0] * trj.unitcell_lengths[0][2] for water_trj in (water_o, water_h): bins, density = compute_density(water_trj, area, dim, pore_center=pore_center, bin_width=0.01) label_name = list(set([i.name for i in water_trj.topology.atoms])) if label_name[0] == 'O': o_densities.append(density) else: h_densities.append(density) o_mean = np.mean(o_densities, axis=0) h_mean = np.mean(h_densities, axis=0) o_std = np.std(o_densities, axis=0) h_std = np.std(h_densities, axis=0) plt.plot(bins, o_mean, label='O') plt.fill_between(bins, o_mean + o_std, o_mean - o_std, alpha=0.2) plt.plot(bins, h_mean, label='H') plt.fill_between(bins, h_mean + h_std, h_mean - h_std, alpha=0.2) plt.xlabel('z-position (nm)') plt.ylabel('Number Density ($nm^-3$)') plt.legend() with job: np.savetxt('o_density.txt', np.transpose(np.vstack([bins, o_mean, o_std])), header='Bins\tDensity_mean\tDensity_std') np.savetxt('h_density.txt', np.transpose(np.vstack([bins, h_mean, h_std])), header='Bins\tDensity_mean\tDensity_std') plt.savefig('numberdensity.pdf') np.savetxt(f'data/{job.sp.nwater}_mol_o_density.txt', np.transpose(np.vstack([bins, o_mean, o_std])), header='Bins\tDensity_mean\tDensity_std') np.savetxt(f'data/{job.sp.nwater}_mol_h_density.txt', np.transpose(np.vstack([bins, h_mean, h_std])), header='Bins\tDensity_mean\tDensity_std')
def number_density(job, symmetrize=False): dim = 1 box_range = [0.167, 1.167] pore_center = (box_range[1] - box_range[0]) / 2 + box_range[0] o_densities = list() h_densities = list() fig, ax = plt.subplots() if job.sp.nwater == 1: skip = 1 else: skip = 5001 for trj in md.iterload( os.path.join(job.ws, "nvt.trr"), top=os.path.join(job.ws, "nvt.gro"), chunk=5000, skip=skip, ): water_o = trj.atom_slice(trj.topology.select("name O")) water_h = trj.atom_slice(trj.topology.select("name H")) area = trj.unitcell_lengths[0][0] * trj.unitcell_lengths[0][2] for water_trj in (water_o, water_h): bins, density = compute_density( water_trj, area, dim, pore_center=pore_center, bin_width=0.01, symmetrize=symmetrize, ) label_name = list(set([i.name for i in water_trj.topology.atoms])) if label_name[0] == "O": o_densities.append(density) else: h_densities.append(density) o_mean = np.mean(o_densities, axis=0) h_mean = np.mean(h_densities, axis=0) o_std = np.std(o_densities, axis=0) h_std = np.std(h_densities, axis=0) plt.plot(bins, o_mean, label="O") plt.fill_between(bins, o_mean + o_std, o_mean - o_std, alpha=0.2) plt.plot(bins, h_mean, label="H") plt.fill_between(bins, h_mean + h_std, h_mean - h_std, alpha=0.2) plt.xlabel("z-position (nm)") plt.ylabel("Number Density ($nm^-3$)") plt.legend() if symmetrize == True: extension = "_symmetrize" else: extension = "" with job: np.savetxt( f"o_density{extension}.txt", np.transpose(np.vstack([bins, o_mean, o_std])), header="Bins\tDensity_mean\tDensity_std", ) np.savetxt( f"h_density{extension}.txt", np.transpose(np.vstack([bins, h_mean, h_std])), header="Bins\tDensity_mean\tDensity_std", ) plt.savefig(f"numberdensity{extension}.pdf") np.savetxt( f"data/{job.sp.nwater}_mol_o_density{extension}.txt", np.transpose(np.vstack([bins, o_mean, o_std])), header="Bins\tDensity_mean\tDensity_std", ) np.savetxt( f"data/{job.sp.nwater}_mol_h_density{extension}.txt", np.transpose(np.vstack([bins, h_mean, h_std])), header="Bins\tDensity_mean\tDensity_std", )
def main(): project = signac.get_project("../") for nwater, group in project.groupby("nwater"): # New dataframe to save the results df = pd.DataFrame() # Create pore system pore_width = 1.0 * u.nm filled_pore = mbuild.recipes.GraphenePoreSolvent( pore_length=1.0, pore_depth=1.1, pore_width=pore_width.to_value("nm"), n_sheets=1, slit_pore_dim=2, x_bulk=0, solvent=spce_water, n_solvent=nwater, ) # Translate to centered at 0,0,0 and make box larger in z box_center = filled_pore.periodicity / 2.0 filled_pore.translate(-box_center) filled_pore.periodicity[2] = 2.0 xy_area = filled_pore.periodicity[0] * filled_pore.periodicity[1] top = filled_pore.to_trajectory(residues=["RES", "SOL"]) # Load all trajectories and combine for job in group: run = job.sp.run # Load in full trajectory full_traj = md.load(job.fn("prod.nvt.out.xyz"), top=top) # Add unit cell information full_traj = md.Trajectory( full_traj.xyz, full_traj.top, unitcell_lengths=np.tile(filled_pore.periodicity, (full_traj.n_frames, 1)), unitcell_angles=np.tile([90., 90., 90.], (full_traj.n_frames, 1)), ) # Keep only water slice_water = full_traj.top.select("water and name O H") traj = full_traj.atom_slice(slice_water) slice_ow = traj.top.select("name O") slice_hw = traj.top.select("name H") traj_ow = traj.atom_slice(slice_ow) traj_hw = traj.atom_slice(slice_hw) # Compute the density bin_centers_ow, density_ow = compute_density(traj_ow, xy_area, bin_width=0.005) bin_centers_hw, density_hw = compute_density(traj_hw, xy_area, bin_width=0.005) # Compute the s order parameter bin_centers_s, s_results = compute_s(traj, bin_width=0.005) assert np.allclose(bin_centers_ow, bin_centers_hw) assert np.allclose(bin_centers_ow, bin_centers_s) # Save results tmp_df = pd.DataFrame() tmp_df[f"run"] = run tmp_df[f"z-loc_nm"] = bin_centers_ow tmp_df[f"density-ow_nm^-3"] = density_ow tmp_df[f"density-hw_nm^-3"] = density_hw tmp_df[f"s_value"] = s_results df = df.append(tmp_df) # Compute mean/stdev and save to file means = df.groupby("z-loc_nm").mean().drop( columns=["run"]).add_suffix("_mean") stds = df.groupby("z-loc_nm").std().drop( columns=["run"]).add_suffix("_std") combined = means.merge(stds, on="z-loc_nm") combined.to_csv(f"results_{nwater}-water.csv")