gel.scripts.get_bc_vtks

Generates bci.vtk, bco.vtk with FM-Track GPR-interpolation.

Must be run in the FM-Track environment. Will use a GPR model saved to a directory with a specific format readable by the functions in gel.scripts.get_displacements_from_gpr.py.

For all arguments, run ./get_bc_vtks.py --help

  1#!/usr/bin/env python3
  2"""Generates bci.vtk, bco.vtk with FM-Track GPR-interpolation.
  3
  4Must be run in the FM-Track environment. Will use a GPR model saved to
  5a directory with a specific format readable by the functions in
  6`gel.scripts.get_displacements_from_gpr.py`.
  7
  8For all arguments, run `./get_bc_vtks.py --help`
  9"""
 10import argparse
 11import os
 12import numpy as np
 13import meshio
 14from .get_displacement_from_gpr import get_predicted_u
 15
 16
 17def my_optimizer(obj_func, initial_theta, bounds):
 18    """Function sometimes used for kernel hyperparameter optimization"""
 19    import scipy
 20    opt_res = scipy.optimize.minimize(
 21        obj_func, initial_theta, method="L-BFGS-B", jac=True,
 22        bounds=bounds, options={"maxiter" : 3, "disp" : True})
 23    theta_opt, func_min = opt_res.x, opt_res.fun
 24    return theta_opt, func_min
 25
 26
 27def _write_bc_vtk(gpr_dir, gel_mesh_boundaries, boundary_ind, filepath):
 28    boundary_mask = np.zeros(len(gel_mesh_boundaries.points)).astype(bool)
 29    boundary_mask[
 30        gel_mesh_boundaries.cells[0].data[
 31            gel_mesh_boundaries.cell_data["boundaries"][0] == boundary_ind
 32        ]
 33    ] = True
 34
 35    boundary_points = gel_mesh_boundaries.points[boundary_mask]
 36
 37    u = get_predicted_u(gpr_dir, boundary_points)
 38
 39    mesh = meshio.Mesh(boundary_points, {}, point_data={"u":u})
 40    mesh.write(filepath)
 41
 42
 43def get_bc_vtks(
 44        cell_data_dir,
 45        gpr_dir,
 46        out_dir
 47    ):
 48    """Writes files `bci.vtk` and `bco.vtk` under `out_dir` for `gel.Geometry`
 49
 50    * `cell_data_dir`: str path to directory with
 51    `reference_domain_boundaries.xdmf`
 52    * `gpr_dir`: str path to directory with GPR model files
 53    * `out_dir`: str path to directory in which to place `bci.vtk` and 
 54    `bco.vtk`
 55
 56    Side-effects: writes files as specified
 57    """
 58    gel_mesh_boundaries = meshio.read(
 59        os.path.join(cell_data_dir, "reference_domain_boundaries.xdmf")
 60    )
 61
 62    _write_bc_vtk(
 63        gpr_dir,
 64        gel_mesh_boundaries,
 65        201,
 66        os.path.join(out_dir, "bco.vtk")
 67    )
 68    _write_bc_vtk(
 69        gpr_dir,
 70        gel_mesh_boundaries,
 71        202,
 72        os.path.join(out_dir, "bci.vtk")
 73    )
 74
 75
 76def get_bc_vtks_main():
 77    """The function invoked by the command. Parses arguments and passes
 78    to `get_bc_vtks`.
 79    """
 80    parser = argparse.ArgumentParser(
 81        description="Generates boundary condition files bci.vtk and "
 82        "bco.vtk with FM-Track GPR-interpolation. Must be run in "
 83        "FM-Track environment. Places files with those names in "
 84        "specified directory. Will determine boundaries by "
 85        "CELL_DATA_DIR/reference_domain_boundaries.xdmf"
 86    )
 87    parser.add_argument(
 88        "-c",
 89        type=str,
 90        metavar="CELL_DATA_DIR",
 91        help="directory containing gel geometry, namely "
 92        "reference_domain_boundaries.xdmf and .h5"
 93    )
 94    parser.add_argument(
 95        "-g",
 96        type=str,
 97        metavar="GPR_DIR",
 98        help="directory with GPR model files, i.e. gp_U_cleaned.sav etc."
 99    )
100    parser.add_argument(
101        "-o",
102        type=str,
103        metavar="OUT_DIR",
104        default=None,
105        help="output directory in which to put bci.vtk and bco.vtk, "
106        "default CELL_DATA_DIR"
107    )
108    args = parser.parse_args()
109
110    if args.o is None:
111        args.o = args.c
112
113    get_bc_vtks(args.c, args.g, args.o)
114
115
116if __name__=="__main__":
117    get_bc_vtks_main()
def my_optimizer(obj_func, initial_theta, bounds):
18def my_optimizer(obj_func, initial_theta, bounds):
19    """Function sometimes used for kernel hyperparameter optimization"""
20    import scipy
21    opt_res = scipy.optimize.minimize(
22        obj_func, initial_theta, method="L-BFGS-B", jac=True,
23        bounds=bounds, options={"maxiter" : 3, "disp" : True})
24    theta_opt, func_min = opt_res.x, opt_res.fun
25    return theta_opt, func_min

Function sometimes used for kernel hyperparameter optimization

def get_bc_vtks(cell_data_dir, gpr_dir, out_dir):
44def get_bc_vtks(
45        cell_data_dir,
46        gpr_dir,
47        out_dir
48    ):
49    """Writes files `bci.vtk` and `bco.vtk` under `out_dir` for `gel.Geometry`
50
51    * `cell_data_dir`: str path to directory with
52    `reference_domain_boundaries.xdmf`
53    * `gpr_dir`: str path to directory with GPR model files
54    * `out_dir`: str path to directory in which to place `bci.vtk` and 
55    `bco.vtk`
56
57    Side-effects: writes files as specified
58    """
59    gel_mesh_boundaries = meshio.read(
60        os.path.join(cell_data_dir, "reference_domain_boundaries.xdmf")
61    )
62
63    _write_bc_vtk(
64        gpr_dir,
65        gel_mesh_boundaries,
66        201,
67        os.path.join(out_dir, "bco.vtk")
68    )
69    _write_bc_vtk(
70        gpr_dir,
71        gel_mesh_boundaries,
72        202,
73        os.path.join(out_dir, "bci.vtk")
74    )

Writes files bci.vtk and bco.vtk under out_dir for gel.Geometry

  • cell_data_dir: str path to directory with reference_domain_boundaries.xdmf
  • gpr_dir: str path to directory with GPR model files
  • out_dir: str path to directory in which to place bci.vtk and bco.vtk

Side-effects: writes files as specified

def get_bc_vtks_main():
 77def get_bc_vtks_main():
 78    """The function invoked by the command. Parses arguments and passes
 79    to `get_bc_vtks`.
 80    """
 81    parser = argparse.ArgumentParser(
 82        description="Generates boundary condition files bci.vtk and "
 83        "bco.vtk with FM-Track GPR-interpolation. Must be run in "
 84        "FM-Track environment. Places files with those names in "
 85        "specified directory. Will determine boundaries by "
 86        "CELL_DATA_DIR/reference_domain_boundaries.xdmf"
 87    )
 88    parser.add_argument(
 89        "-c",
 90        type=str,
 91        metavar="CELL_DATA_DIR",
 92        help="directory containing gel geometry, namely "
 93        "reference_domain_boundaries.xdmf and .h5"
 94    )
 95    parser.add_argument(
 96        "-g",
 97        type=str,
 98        metavar="GPR_DIR",
 99        help="directory with GPR model files, i.e. gp_U_cleaned.sav etc."
100    )
101    parser.add_argument(
102        "-o",
103        type=str,
104        metavar="OUT_DIR",
105        default=None,
106        help="output directory in which to put bci.vtk and bco.vtk, "
107        "default CELL_DATA_DIR"
108    )
109    args = parser.parse_args()
110
111    if args.o is None:
112        args.o = args.c
113
114    get_bc_vtks(args.c, args.g, args.o)

The function invoked by the command. Parses arguments and passes to get_bc_vtks.