Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bc and mat will be erroneous merged in netgen.csg #49

Closed
liubenyuan opened this issue Dec 5, 2019 · 2 comments
Closed

bc and mat will be erroneous merged in netgen.csg #49

liubenyuan opened this issue Dec 5, 2019 · 2 comments

Comments

@liubenyuan
Copy link

liubenyuan commented Dec 5, 2019

Hi, I want to merge/combine some cylinders in netgen.csg, where 1 big cylinder is the domain to be investigated and two small cylinders are electrodes (voltage/current boundaries). I specify the bcs on the top surface of a cylinder primitive. However, the bc will be erroneous merged. Here is a minimal example that recreates this problem. Moreover, the material properties of the small cylinders (electrodes) are also merged, which can be seen in mesh.GetMaterials().

Can I preserve the bc and mat of the small shapes when using + in netgen.csg?

https://github.com/liubenyuan/netgen-note/blob/master/examples/csg_issue_bc_cyl.py

At line 78, if you comment this line p0[0] += 0.01, the bc="e1" will be removed, if uncomment this line, bc="e1" will be visible. The material properties of electrodes (which should be 'agcl') is gone, where only 'g0' is preserved.

import numpy as np
from netgen import csg
from ngsolve.comp import Mesh


def cylinder(p0, p1, unit_vec, r, bc="bc"):
    """ a pointed cylinder (numpy interface) """
    # generate CSG primitives
    csg_p0 = csg.Pnt(p0[0], p0[1], p0[2])
    csg_p1 = csg.Pnt(p1[0], p1[1], p1[2])
    csg_vec = csg.Vec(unit_vec[0], unit_vec[1], unit_vec[2])

    # generate cylinder shape
    bot = csg.Plane(csg_p0, -csg_vec)
    top = csg.Plane(csg_p1, csg_vec)
    cy_surface = csg.Cylinder(csg_p0, csg_p1, r)
    
    # specify bc on top
    top.bc(bc)

    # geo
    cy = top * cy_surface * bot
    
    return cy


def unit_cylinder(h=1.0, r=0.5, bc="cyl"):
    """ scaled unit z-axis cylinder """
    p0 = [0, 0, 0]
    p1 = [0, 0, h]
    unit_vec = [0, 0, 1]
    return cylinder(p0, p1, unit_vec, r, bc)


def electrode_cylinder(p0=[0, 0, 0], unit_vec=[1, 0, 0], shape=[0.1, 0.1],
                       bc="e0"):
    """
    cylinder electrode (numpy interface)

    shape: NDArray
        [radius, thickness]
    """
    r, thick = shape
    # calculate the center of the other face
    p1 = np.asarray(p0) + thick * np.asarray(unit_vec)

    # generate cylinder electrode with annotation
    ele = cylinder(p0, p1, unit_vec, r, bc)
    ele.mat('agcl')

    return ele


def cylinder_with_electrodes():
    """ [test only, will be deleted] """
    h = 1.0
    r = 0.5
    g0 = unit_cylinder(h=h, r=r)
    g0.mat('g0')

    # generate cylinder electrode
    ele_shape = [0.1, 0.1]  # radius, thick
    thick = ele_shape[1]
    ele_pos = [30, h/2.0]  # angle (degree), z
    
    # infer locations
    rad = ele_pos[0] * 2 * np.pi / 360.0
    unit_vec = np.array([np.cos(rad), np.sin(rad), 0])
    # warning: a thin contact of electrode with domain will cause
    # netgen failed to generate proper mesh
    p0 = (r - thick/2.0)*unit_vec
    p0[2] = ele_pos[1]

    # place 1st electrode
    ele = electrode_cylinder(p0, unit_vec, shape=ele_shape, bc="e0")
    geo = g0 + ele
    
    # place 2nd electrode
    p0[2] += 0.25
    p0[0] += 0.01  # <-- https://github.com/NGSolve/netgen/issues/49
    ele2 = electrode_cylinder(p0, unit_vec, shape=ele_shape, bc="e1")
    geo = geo + ele2
    
    return geo


if __name__ == "__main__":
    g = cylinder_with_electrodes()
    geo = csg.CSGeometry()
    geo.Add(g)

    ngmesh = geo.GenerateMesh(maxh=0.1)

    mesh = Mesh(ngmesh)
    # mesh.Curve(3)
    
    print(mesh.GetBoundaries())
    print(mesh.GetMaterials())
@liubenyuan liubenyuan changed the title bc will be automatically merged in CSG mode bc will be automatically merged in netgen.csg Dec 5, 2019
@liubenyuan liubenyuan changed the title bc will be automatically merged in netgen.csg bc and mat will be erroneous merged in netgen.csg Dec 5, 2019
@liubenyuan
Copy link
Author

Updated, add these small geometries separately can solve the problem of specifying the materials, see
https://github.com/liubenyuan/netgen-note/blob/master/examples/csg_issue_bc_cyl.py

but the boundaries (bc) are still merging.

@liubenyuan
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant