[docs]defcell_arearise_face(cell,fkey,target_area,tol=1e-3,callback=None,callback_args=None):ifcallback:ifnotcallable(callback):raiseException('Callback is not callable.')cell_split_indet_face_vertices(cell,fkey)area=cell.face_oriented_area(fkey)normal=cell.face_oriented_normal(fkey)center=cell.face_centroid(fkey)# --------------------------------------------------------------------------# get move direction# --------------------------------------------------------------------------sign=1# if it needs to get larger...ifarea-target_area>0:sign=-1# if it needs to be smaller...ifarea*target_area<0:# if it needs to flip...sign=-1move_dir=_get_move_direction(cell,fkey)*sign# --------------------------------------------------------------------------# evaluation function# --------------------------------------------------------------------------defevaluation(x):xyz=add_vectors(center,scale_vector(normal,x*move_dir))new_area=_evaluate_new_face_area(cell,fkey,xyz,normal)# callback / conduit ---------------------------------------------------ifcallback:callback(cell,callback_args)returnabs(new_area-target_area)# --------------------------------------------------------------------------# golden section search# --------------------------------------------------------------------------a=0b=abs(area-target_area)z=golden_section_search(evaluation,a,b,tol=tol)# --------------------------------------------------------------------------# update cell# --------------------------------------------------------------------------xyz=add_vectors(center,scale_vector(normal,z*move_dir))cell_relocate_face(cell,fkey,xyz,normal)
# ******************************************************************************# ******************************************************************************# ******************************************************************************## helpers## ******************************************************************************# ******************************************************************************# ******************************************************************************def_get_move_direction(cell,fkey):normal=cell.face_oriented_normal(fkey)area=cell.face_oriented_area(fkey)center=cell.face_center(fkey)new_center=add_vectors(center,normal)new_area=_evaluate_new_face_area(cell,fkey,new_center)ifnew_area>area:move_dir=1ifnew_area<area:move_dir=-1cell_relocate_face(cell,fkey,center,normal)ifnew_area==area:raiseException('Pulling this face will not change its area!')returnmove_dirdef_evaluate_new_face_area(cell,fkey,xyz,init_normal=None):ifnotinit_normal:init_normal=cell.face_normal(fkey)cell_relocate_face(cell,fkey,xyz,init_normal)new_area=cell.face_oriented_area(fkey)new_normal=cell.face_oriented_normal(fkey)ifdot_vectors(init_normal,new_normal)<0:new_area*=-1returnnew_area# ******************************************************************************# ******************************************************************************# ******************************************************************************## Main## ******************************************************************************# ******************************************************************************# ******************************************************************************if__name__=='__main__':pass