POD from pointclouds
We will now proceed to explain how to perform POD from point clouds. In this instance, we test only for POD in serial, as to perform in parallel, a parallel reader/writer is needed.
If you have saved information in hdf5 and have habilitated mpi4py compilation of it, then you could use this code in parallel.
Import general modules
[1]:
# Import required modules
from mpi4py import MPI #equivalent to the use of MPI_init() in C
import matplotlib.pyplot as plt
import numpy as np
import h5py
# Get mpi info
comm = MPI.COMM_WORLD
# Hide the log for the notebook. Not recommended when running in clusters as it is better you see what happens
import os
os.environ["PYSEMTOOLS_HIDE_LOG"] = 'true'
Set up the input parameters
[2]:
file_sequence = [f"../4-interpolation/interpolated_fields{str(1+i).zfill(5)}.hdf5" for i in range(0, 48)]
pod_fields = ["u", "v", "w"]
mesh_fname = "../4-interpolation/points.hdf5"
mass_matrix_fname = "../4-interpolation/points.hdf5"
mass_matrix_key = "mass"
k = len(file_sequence)
p = len(file_sequence)
Call the pysemtools routines
[3]:
from pysemtools.rom.phy_pod_wrappers import pod_from_files
# Output
from pyevtk.hl import gridToVTK
pod, ioh, field3d_shape = pod_from_files(comm, file_sequence, pod_fields, mass_matrix_fname, mass_matrix_key, k, p)
Write out the data
In this case we can easily write the modes from vtk
[4]:
# Load the mesh
with h5py.File(mesh_fname, 'r') as f:
x = f["x"][:]
y = f["y"][:]
z = f["z"][:]
from pysemtools.rom.phy_pod_wrappers import write_3dfield_to_file
# Load the mesh
with h5py.File(mesh_fname, 'r') as f:
x = f["x"][:]
y = f["y"][:]
z = f["z"][:]
# Write out 5 modes
write_3dfield_to_file("phys_pod.vtk", x, y, z, pod, ioh, modes=[i for i in range(0, 5)], field_shape=field3d_shape, field_names=pod_fields)
# Reconstruct all the snapshots with 5 modes
write_3dfield_to_file("phys_pod.vtk", x, y, z, pod, ioh, modes=[i for i in range(0, 5)], field_shape=field3d_shape, field_names=pod_fields, snapshots=[i for i in range(0, len(file_sequence))])
Writing ./phys_pod__mode0.vtk
Writing ./phys_pod__mode1.vtk
Writing ./phys_pod__mode2.vtk
Writing ./phys_pod__mode3.vtk
Writing ./phys_pod__mode4.vtk
Writing ./phys_pod_reconstructed_data_0
Writing ./phys_pod_reconstructed_data_1
Writing ./phys_pod_reconstructed_data_2
Writing ./phys_pod_reconstructed_data_3
Writing ./phys_pod_reconstructed_data_4
Writing ./phys_pod_reconstructed_data_5
Writing ./phys_pod_reconstructed_data_6
Writing ./phys_pod_reconstructed_data_7
Writing ./phys_pod_reconstructed_data_8
Writing ./phys_pod_reconstructed_data_9
Writing ./phys_pod_reconstructed_data_10
Writing ./phys_pod_reconstructed_data_11
Writing ./phys_pod_reconstructed_data_12
Writing ./phys_pod_reconstructed_data_13
Writing ./phys_pod_reconstructed_data_14
Writing ./phys_pod_reconstructed_data_15
Writing ./phys_pod_reconstructed_data_16
Writing ./phys_pod_reconstructed_data_17
Writing ./phys_pod_reconstructed_data_18
Writing ./phys_pod_reconstructed_data_19
Writing ./phys_pod_reconstructed_data_20
Writing ./phys_pod_reconstructed_data_21
Writing ./phys_pod_reconstructed_data_22
Writing ./phys_pod_reconstructed_data_23
Writing ./phys_pod_reconstructed_data_24
Writing ./phys_pod_reconstructed_data_25
Writing ./phys_pod_reconstructed_data_26
Writing ./phys_pod_reconstructed_data_27
Writing ./phys_pod_reconstructed_data_28
Writing ./phys_pod_reconstructed_data_29
Writing ./phys_pod_reconstructed_data_30
Writing ./phys_pod_reconstructed_data_31
Writing ./phys_pod_reconstructed_data_32
Writing ./phys_pod_reconstructed_data_33
Writing ./phys_pod_reconstructed_data_34
Writing ./phys_pod_reconstructed_data_35
Writing ./phys_pod_reconstructed_data_36
Writing ./phys_pod_reconstructed_data_37
Writing ./phys_pod_reconstructed_data_38
Writing ./phys_pod_reconstructed_data_39
Writing ./phys_pod_reconstructed_data_40
Writing ./phys_pod_reconstructed_data_41
Writing ./phys_pod_reconstructed_data_42
Writing ./phys_pod_reconstructed_data_43
Writing ./phys_pod_reconstructed_data_44
Writing ./phys_pod_reconstructed_data_45
Writing ./phys_pod_reconstructed_data_46
Writing ./phys_pod_reconstructed_data_47
Save the data as numpy arrays
[5]:
from pysemtools.rom.phy_pod_wrappers import save_pod_state
save_pod_state("pod_state.hdf5", pod)
Perform tests
[6]:
from pysemtools.rom.phy_pod_wrappers import physical_space
# Load the mass matrix
with h5py.File(mass_matrix_fname, 'r') as f:
bm_v = f["mass"][:]
# Go through the snapshots in the file sequence
total_snapshot_energy = 0
for j, fname in enumerate(file_sequence):
print(f"Testing snapshot {j}: {fname}")
with h5py.File(fname, 'r') as f:
# Check one snapshot at a time
# Here one could also just use the phys that has been previously computed.
phys = physical_space(pod, ioh, modes=[i for i in range(0, k)], field_shape=field3d_shape, field_names=pod_fields, snapshots=[j])
# Check if the reconstruction is accurate
for field in pod_fields:
passed = np.allclose(phys[j][field], f[field][:])
print(f"Field {field} passed: {passed}")
# Check if the energy was accurately captured
snapshot_energy = 0
for field in pod_fields:
snapshot_energy += np.sum(f[field][:]**2*bm_v)
total_snapshot_energy += snapshot_energy
reconstruction_energy = 0
a_i = np.diag(pod.d_1t)@pod.vt_1t[:,j]
reconstruction_energy += np.sum(np.abs(a_i)**2)
print(f"Snapshot energy: {snapshot_energy}")
print(f"Reconstruction energy: {reconstruction_energy}")
print("=======================================")
# Check if the total energy is kept in the singular values
print(f"total snapshot energy: {total_snapshot_energy}")
total_pod_energy = 0
total_pod_energy += np.sum(pod.d_1t**2)
print(f"Total POD energy: {total_pod_energy}")
print("=======================================")
Testing snapshot 0: ../4-interpolation/interpolated_fields00001.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 3.698958237844183e-05
Reconstruction energy: 3.698959791995519e-05
=======================================
Testing snapshot 1: ../4-interpolation/interpolated_fields00002.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 3.8898814924962444e-05
Reconstruction energy: 3.8898829611720724e-05
=======================================
Testing snapshot 2: ../4-interpolation/interpolated_fields00003.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 4.153454569587408e-05
Reconstruction energy: 4.153456122670148e-05
=======================================
Testing snapshot 3: ../4-interpolation/interpolated_fields00004.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 4.4425076415412405e-05
Reconstruction energy: 4.442509363710398e-05
=======================================
Testing snapshot 4: ../4-interpolation/interpolated_fields00005.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 4.688317219673442e-05
Reconstruction energy: 4.688319417681433e-05
=======================================
Testing snapshot 5: ../4-interpolation/interpolated_fields00006.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 4.878401448156997e-05
Reconstruction energy: 4.8784037017177103e-05
=======================================
Testing snapshot 6: ../4-interpolation/interpolated_fields00007.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 5.026805630070726e-05
Reconstruction energy: 5.026807862059571e-05
=======================================
Testing snapshot 7: ../4-interpolation/interpolated_fields00008.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 5.149475955390006e-05
Reconstruction energy: 5.149478153993227e-05
=======================================
Testing snapshot 8: ../4-interpolation/interpolated_fields00009.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 5.237488647324383e-05
Reconstruction energy: 5.237490538075368e-05
=======================================
Testing snapshot 9: ../4-interpolation/interpolated_fields00010.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 5.263423351722945e-05
Reconstruction energy: 5.263424808861043e-05
=======================================
Testing snapshot 10: ../4-interpolation/interpolated_fields00011.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 5.187294605439943e-05
Reconstruction energy: 5.187296300851487e-05
=======================================
Testing snapshot 11: ../4-interpolation/interpolated_fields00012.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 4.988241310699623e-05
Reconstruction energy: 4.98824339321701e-05
=======================================
Testing snapshot 12: ../4-interpolation/interpolated_fields00013.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 4.697643854683935e-05
Reconstruction energy: 4.697645923683903e-05
=======================================
Testing snapshot 13: ../4-interpolation/interpolated_fields00014.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 4.3863242905911426e-05
Reconstruction energy: 4.386325835715201e-05
=======================================
Testing snapshot 14: ../4-interpolation/interpolated_fields00015.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 4.04654975068024e-05
Reconstruction energy: 4.046551073804368e-05
=======================================
Testing snapshot 15: ../4-interpolation/interpolated_fields00016.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 3.708331604360593e-05
Reconstruction energy: 3.70833306496177e-05
=======================================
Testing snapshot 16: ../4-interpolation/interpolated_fields00017.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 3.361937142993228e-05
Reconstruction energy: 3.3619385479765e-05
=======================================
Testing snapshot 17: ../4-interpolation/interpolated_fields00018.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 3.042199960394419e-05
Reconstruction energy: 3.042201288840405e-05
=======================================
Testing snapshot 18: ../4-interpolation/interpolated_fields00019.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.764625401122964e-05
Reconstruction energy: 2.7646265699721833e-05
=======================================
Testing snapshot 19: ../4-interpolation/interpolated_fields00020.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.5278143358328515e-05
Reconstruction energy: 2.527815340611091e-05
=======================================
Testing snapshot 20: ../4-interpolation/interpolated_fields00021.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.3436562845034242e-05
Reconstruction energy: 2.3436571745893377e-05
=======================================
Testing snapshot 21: ../4-interpolation/interpolated_fields00022.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.2002654602291965e-05
Reconstruction energy: 2.200266188237421e-05
=======================================
Testing snapshot 22: ../4-interpolation/interpolated_fields00023.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.0920245682595327e-05
Reconstruction energy: 2.0920252797345817e-05
=======================================
Testing snapshot 23: ../4-interpolation/interpolated_fields00024.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.013654268431107e-05
Reconstruction energy: 2.0136550044303895e-05
=======================================
Testing snapshot 24: ../4-interpolation/interpolated_fields00025.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 1.9632578477660025e-05
Reconstruction energy: 1.9632586126642394e-05
=======================================
Testing snapshot 25: ../4-interpolation/interpolated_fields00026.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 1.933656650853822e-05
Reconstruction energy: 1.9336575494338164e-05
=======================================
Testing snapshot 26: ../4-interpolation/interpolated_fields00027.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 1.9193896285463495e-05
Reconstruction energy: 1.9193905632897007e-05
=======================================
Testing snapshot 27: ../4-interpolation/interpolated_fields00028.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 1.9209913696544347e-05
Reconstruction energy: 1.9209922552457982e-05
=======================================
Testing snapshot 28: ../4-interpolation/interpolated_fields00029.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 1.9433633251162603e-05
Reconstruction energy: 1.9433641564702733e-05
=======================================
Testing snapshot 29: ../4-interpolation/interpolated_fields00030.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 1.9843626264053833e-05
Reconstruction energy: 1.984363462420495e-05
=======================================
Testing snapshot 30: ../4-interpolation/interpolated_fields00031.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.0264037570696985e-05
Reconstruction energy: 2.026404726721621e-05
=======================================
Testing snapshot 31: ../4-interpolation/interpolated_fields00032.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.061492121971811e-05
Reconstruction energy: 2.0614930665485913e-05
=======================================
Testing snapshot 32: ../4-interpolation/interpolated_fields00033.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.0873392702196558e-05
Reconstruction energy: 2.0873401695293363e-05
=======================================
Testing snapshot 33: ../4-interpolation/interpolated_fields00034.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.100146581923816e-05
Reconstruction energy: 2.1001474879010305e-05
=======================================
Testing snapshot 34: ../4-interpolation/interpolated_fields00035.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.1002631484979843e-05
Reconstruction energy: 2.1002640205096e-05
=======================================
Testing snapshot 35: ../4-interpolation/interpolated_fields00036.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.0926017391762055e-05
Reconstruction energy: 2.0926025698532318e-05
=======================================
Testing snapshot 36: ../4-interpolation/interpolated_fields00037.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.0846074698639117e-05
Reconstruction energy: 2.0846082378820277e-05
=======================================
Testing snapshot 37: ../4-interpolation/interpolated_fields00038.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.0866571618859654e-05
Reconstruction energy: 2.086657965562247e-05
=======================================
Testing snapshot 38: ../4-interpolation/interpolated_fields00039.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.101439294141517e-05
Reconstruction energy: 2.1014401790563164e-05
=======================================
Testing snapshot 39: ../4-interpolation/interpolated_fields00040.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.1135889588706728e-05
Reconstruction energy: 2.1135897678024467e-05
=======================================
Testing snapshot 40: ../4-interpolation/interpolated_fields00041.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.1184459915354247e-05
Reconstruction energy: 2.1184468317008704e-05
=======================================
Testing snapshot 41: ../4-interpolation/interpolated_fields00042.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.1250999040067878e-05
Reconstruction energy: 2.1251006836458486e-05
=======================================
Testing snapshot 42: ../4-interpolation/interpolated_fields00043.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.1377532850036896e-05
Reconstruction energy: 2.1377540941763505e-05
=======================================
Testing snapshot 43: ../4-interpolation/interpolated_fields00044.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.1529543422705357e-05
Reconstruction energy: 2.1529553102361603e-05
=======================================
Testing snapshot 44: ../4-interpolation/interpolated_fields00045.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.161399267113895e-05
Reconstruction energy: 2.1614003298549693e-05
=======================================
Testing snapshot 45: ../4-interpolation/interpolated_fields00046.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.1637133597399283e-05
Reconstruction energy: 2.163714439898288e-05
=======================================
Testing snapshot 46: ../4-interpolation/interpolated_fields00047.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.1440817225395135e-05
Reconstruction energy: 2.1440827260570388e-05
=======================================
Testing snapshot 47: ../4-interpolation/interpolated_fields00048.hdf5
Field u passed: True
Field v passed: True
Field w passed: True
Snapshot energy: 2.0948984478946528e-05
Reconstruction energy: 2.0948993077000546e-05
=======================================
total snapshot energy: 0.001434071843040977
Total POD energy: 0.0014340724222272252
=======================================