You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classDisk(geometry.Geometry):
...
...
defsdf_func(self, points: np.ndarray) ->np.ndarray:
"""Compute signed distance field. Args: points (np.ndarray): The coordinate points used to calculate the SDF value, the shape is [N, 2] Returns: np.ndarray: Unsquared SDF values of input points, the shape is [N, 1]. NOTE: This function usually returns ndarray with negative values, because according to the definition of SDF, the SDF value of the coordinate point inside the object(interior points) is negative, the outside is positive, and the edge is 0. Therefore, when used for weighting, a negative sign is often added before the result of this function. """# 在此处编写代码,为传入的 points,计算对应的 sdf_value 并返回returnsdf_value
还需修改一下:ppsci/geometry/geometry.py 的代码,
defsample_interior(self, n, random="pseudo", criteria=None, evenly=False):
"""Sample random points in the geometry and return those meet criteria."""x=np.empty(shape=(n, self.ndim), dtype=paddle.get_default_dtype())
_size, _ntry, _nsuc=0, 0, 0while_size<n:
ifevenly:
points=self.uniform_points(n)
else:
ifmisc.typename(self) =="TimeXGeometry":
points=self.random_points(n, random, criteria)
else:
points=self.random_points(n, random)
ifcriteriaisnotNone:
criteria_mask=criteria(*np.split(points, self.ndim, axis=1)).flatten()
points=points[criteria_mask]
iflen(points) >n-_size:
points=points[: n-_size]
x[_size : _size+len(points)] =points_size+=len(points)
_ntry+=1iflen(points) >0:
_nsuc+=1if_ntry>=1000and_nsuc==0:
raiseValueError(
"Sample interior points failed, ""please check correctness of geometry and given creteria."
)
# 以下为新增代码:内部点 x 采样完之后计算其 sdf 值,并和 x 一起转换为字典返回ifhasattr(self, 'sdf_func'):
sdf=-self.sdf_func(x)
sdf_dict=misc.convert_to_dict(sdf, ("sdf", ))
x_dict=misc.convert_to_dict(x, self.dim_keys)
return {**x_dict, **sdf_dict}
else:
returnmisc.convert_to_dict(x, self.dim_keys)
1. 背景
坐标点是科学计算问题求解时常用的输入数据对象,这些坐标点从指定的几何计算域中采样得到。从训练优化的角度来看,给所有样本点(坐标点)设置一样的权重并不是一个最优的方法,因此在一些优化 trick 中会将每个点的 SDF(signed distance function) 值作为其权重,以此来加强难样本(靠近计算域中心)的权重,减少简单样本(靠近计算域边界)的权重,最终进一步提升模型训练的精度。
PaddleScience 目前的几何模块由简单几何和复杂几何组成,复杂几何由 ppsci.geometry.Mesh 类来表示,并基于 pymesh 库进行 SDF 值的计算;而简单几何(如 ppsci.geometry.Disk)还未具备 SDF 计算功能,因此我们希望给所有的简单几何类,添加 SDF 计算功能。
Tips: SDF 函数一般用于计算一个点(多个点同理)到某一个几何边界的最短(欧式)距离(内部为负,外部为正),而在科学计算套件中,这些计算点通常来自几何内部,因此根据 SDF 的定义,其值往往是负数。如$[l, r]$ 的一维线段内部一个点 $x$ 的 sdf 函数就是
2. 收益 (整体进度:7/7)
使得简单几何具备 SDF 加权功能,从功能上与复杂几何更为统一,提升基于简单几何的案例优化空间,熟悉 PaddleScience 套件
3. 开发流程
3.1 安装 PaddleScience
下载安装 PaddleScience 套件:https://paddlescience-docs.readthedocs.io/zh/latest/zh/install_setup/#121-git
3.2 代码开发
以为 ppsci.geometry.Disk 模块添加 SDF 函数为例
还需修改一下:
ppsci/geometry/geometry.py
的代码,3.4 SDF 可视化验证
在编写完 SDF 代码之后,我们需要对 SDF 计算结果进行验证,可以按照以下代码手动实例化一个几何对象,然后在其内部进行采样,再调用 sdf_func 对内部采样点计算其 sdf 值,最后用 ppsci 的可视化接口保存为 vtu 文件。用 ParaView 软件打开查看。vtu 文件生成的示例代码如下:
3.5 整理代码并提交PR
参考 PaddleScience文档-贡献指南-整理代码并提交
4. 参考资料
The text was updated successfully, but these errors were encountered: