summaryrefslogtreecommitdiff
path: root/openEMS/python/openEMS/automesh.py
blob: 93472faf298fd68819785a421f0d934cd1511deb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 19 20:29:25 2017

@author: thorsten
"""

import sys
import numpy as np

from CSXCAD import CSPrimitives
from CSXCAD.Utilities import CheckNyDir, GetMultiDirs

def mesh_hint_from_primitive(primitive, dirs, **kw):
    if primitive.GetType() is CSPrimitives.POINT:
        return mesh_hint_from_point(primitive, dirs, **kw)
    if primitive.GetType() is CSPrimitives.BOX:
        return mesh_hint_from_box(primitive, dirs, **kw)
    else:
        return None

def mesh_hint_from_point(point, dirs, **kw):
    """ mesh_hint_from_point(point, dirs)

    Get a grid hint for the coordinates of the point.

    :param dirs: str -- 'x','y','z' or 'xy', 'yz' or 'xyz' or 'all'
    :returns: (3,) list of mesh hints
    """
    hint = [None, None, None]
    coord = point.GetCoord()
    for ny in GetMultiDirs(dirs):
        hint[ny] = [coord[ny],]
    return hint

def mesh_hint_from_box(box, dirs, **kw):
    """ mesh_hint_from_box(box, dirs, metal_edge_res=None, **kw)

    Get a grid hint for the edges of the given box with an an optional 2D metal
    edge resolution.

    :param dirs: str -- 'x','y','z' or 'xy', 'yz' or 'xyz' or 'all'
    :param metal_edge_res: float -- 2D flat edge resolution
    :returns: (3,) list of mesh hints
    """
    metal_edge_res = kw.get('metal_edge_res', None)
    up_dir   = kw.get('up_dir'  , True)
    down_dir = kw.get('down_dir', True)

    if metal_edge_res is None:
        mer = 0
    else:
        mer = np.array([-1.0, 2.0])/3 * metal_edge_res
    if box.HasTransform():
        sys.stderr.write('FDTD::automesh: Warning, cannot add edges to grid with transformations enabled\n')
        return
    hint = [None, None, None]
    start = np.fmin(box.GetStart(), box.GetStop())
    stop  = np.fmax(box.GetStart(), box.GetStop())
    for ny in GetMultiDirs(dirs):
        hint[ny] = []
        if metal_edge_res is not None and stop[ny]-start[ny]>metal_edge_res:
            if down_dir:
                hint[ny].append(start[ny]-mer[0])
                hint[ny].append(start[ny]-mer[1])
            if up_dir:
                hint[ny].append(stop[ny]+mer[0])
                hint[ny].append(stop[ny]+mer[1])
        elif stop[ny]-start[ny]:
            if down_dir:
                hint[ny].append(start[ny])
            if up_dir:
                hint[ny].append(stop[ny])
        else:
            hint[ny].append(start[ny])
    return hint