Mesh
Mesh
from pathlib import Path
import cadquery as cq
from microgen import Neper, Phase, mesh
# # We import the Polyhedra from Neper tessellation file
# listPolyhedra, seed, vertices, edges, faces, polys = parseNeper("test1")
# Revel = Rve(dim=1)
# phases = []
# for polyhedron in listPolyhedra:
# elem = Polyhedron(
# center=(
# polyhedron["original"][0],
# polyhedron["original"][1],
# polyhedron["original"][2],
# ),
# dic=polyhedron,
# )
# phases.append(Phase(shape=elem.generate()))
# compound = cq.Compound.makeCompound([phase.shape for phase in phases])
# cq.exporters.export(compound, "compound.step")
# mesh(
# mesh_file="compound.step",
# listPhases=phases,
# size=0.05,
# order=1,
# output_file="Voronoi.vtk",
# )
tess_file = str(Path(__file__).parent / "test1.tess")
polyhedra = Neper.generateVoronoiFromTessFile(tess_file)
shapes = [poly.generate() for poly in polyhedra]
compound = cq.Compound.makeCompound(shapes)
step_file = str(Path(__file__).parent / "compound.step")
cq.exporters.export(compound, step_file)
phases = [Phase(shape=shape) for shape in shapes]
vtk_file = str(Path(__file__).parent / "Voronoi.vtk")
mesh(
mesh_file=step_file,
listPhases=phases,
size=0.05,
order=1,
output_file=vtk_file,
)
Periodic Mesh
from pathlib import Path
import cadquery as cq
import numpy as np
from microgen import Cylinder, Phase, Rve, cutPhases, meshPeriodic, periodic
# ----------LOADTXT------------------------------------------------------------------------------------------#
# dir = os.path.dirname(os.path.realpath("__file__"))
# # path
# path_data = dir + "/"
# Ngeomphase_file = "test_octet.dat"
# # fichier
# NPhases_file = path_data + Ngeomphase_file
NPhases_file = str(Path(__file__).parent / "test_octet.dat")
dt = np.dtype(
[
("number", int),
("shape", np.str_, 10),
("xc", np.float64),
("yc", np.float64),
("zc", np.float64),
("psi", np.float64),
("theta", np.float64),
("phi", np.float64),
("a1", np.float64),
("a2", np.float64),
]
)
# précision du type des données
DATA = np.loadtxt(
NPhases_file,
dtype=dt,
usecols=(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
skiprows=1,
unpack=True,
ndmin=1,
)
xc = DATA[2]
yc = DATA[3]
zc = DATA[4]
psi = DATA[5]
theta = DATA[6]
phi = DATA[7]
height = DATA[8]
radius = DATA[9]
# sections = read_sections(path_data,section_file)
rve = Rve(dim=1)
listPhases = []
listPeriodicPhases = []
n = len(xc)
for i in range(0, n):
elem = Cylinder(
center=(xc[i] - 0.5, yc[i] - 0.5, zc[i] - 0.5),
orientation=(psi[i], theta[i], phi[i]),
height=height[i],
radius=radius[i],
)
listPhases.append(Phase(shape=elem.generate()))
for phase_elem in listPhases:
periodicPhase = periodic(phase=phase_elem, rve=rve)
listPeriodicPhases.append(periodicPhase)
phases_cut = cutPhases(phaseList=listPeriodicPhases, reverseOrder=False)
compound = cq.Compound.makeCompound([phase.shape for phase in phases_cut])
step_file = str(Path(__file__).parent / "octettruss.step")
stl_file = str(Path(__file__).parent / "octettruss.stl")
cq.exporters.export(compound, step_file)
cq.exporters.export(compound, stl_file)
vtk_file = str(Path(__file__).parent / "octettruss.vtk")
meshPeriodic(
mesh_file=step_file,
rve=rve,
listPhases=phases_cut,
order=1,
size=0.03,
output_file=vtk_file,
)
mmg
import os
from pathlib import Path
import meshio
import microgen
data_dir = Path(__file__).parent / "data"
os.makedirs(data_dir, exist_ok=True)
msh_file = str(Path(__file__).parent / "Mesh.msh")
mesh = meshio.read(msh_file)
mesh.write(str(data_dir / "meshIni.mesh"))
microgen.external.Mmg.mmg3d(
input=str(data_dir / "meshIni.mesh"),
output=str(data_dir / "intermesh.mesh"),
)
microgen.external.Mmg.mmg3d(
input=str(data_dir / "intermesh.mesh"),
output=str(data_dir / "finalmesh.mesh"),
ls=True,
hsiz=0.03,
)
meshFinal = meshio.read(str(data_dir / "finalmesh.mesh"))
final_msh = str(Path(__file__).parent / "finalmesh.msh")
final_vtk = str(Path(__file__).parent / "finalmesh.vtk")
meshFinal.write(final_msh, file_format="gmsh22")
meshFinal.write(final_vtk)
mmg Voronoi
import os
from pathlib import Path
import meshio
import microgen
data_dir = Path(__file__).parent / "data"
os.makedirs(data_dir, exist_ok=True)
msh_file = str(Path(__file__).parent / "Mesh.msh")
mesh = meshio.read(msh_file)
mesh.write(str(data_dir / "meshIni.mesh"))
microgen.external.Mmg.mmg3d(
input=str(data_dir / "meshIni.mesh"),
output=str(data_dir / "intermesh.mesh"),
)
microgen.external.Mmg.mmg3d(
input=str(data_dir / "intermesh.mesh"),
output=str(data_dir / "finalmesh.mesh"),
ls=True,
hgrad=1.1,
hsiz=0.02,
)
meshFinal = meshio.read(str(data_dir / "finalmesh.mesh"))
final_msh = str(Path(__file__).parent / "finalmesh.msh")
final_vtk = str(Path(__file__).parent / "finalmesh.vtk")
meshFinal.write(final_msh, file_format="gmsh22")
meshFinal.write(final_vtk)
Periodic remeshing
"""Remesh a gyroid surface keeping periodicity for FEM simulations."""
from pathlib import Path
from microgen import Tpms
from microgen.remesh import remesh_keeping_periodicity_for_fem
from microgen.shape.surface_functions import gyroid
data_dir = Path(__file__).parent / "data"
Path.mkdir(data_dir, exist_ok=True)
tpms = Tpms(surface_function=gyroid, offset=1.0, resolution=50)
initial_gyroid = tpms.grid_sheet
initial_gyroid.save(data_dir / "initial_gyroid_mesh.vtk")
max_element_edge_length = 0.02
remeshed_gyroid = remesh_keeping_periodicity_for_fem(
initial_gyroid,
hmax=max_element_edge_length,
)
remeshed_gyroid.save(data_dir / "remeshed_gyroid_mesh.vtk")