Coverage for fxdgm/RefinedMesh1D.py: 85%

27 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-13 13:26 +0000

1''' 

2Jan Habscheid 

3Jan.Habscheid@rwth-aachen.de 

4 

5This script implements the fenics solver for a ternary electrolyte (N=3, A,C,S) 

6''' 

7 

8import numpy as np 

9from mpi4py import MPI 

10from dolfinx import mesh 

11from ufl import Mesh 

12from basix.ufl import element 

13 

14 

15# Define mesh 

16def create_refined_mesh(refinement_style:str, number_cells:int) -> Mesh: 

17 ''' 

18 Creates a one-dimensional mesh with a refined region at the left boundary 

19 

20 Parameters 

21 ---------- 

22 refinement_style : str 

23 How the mesh should be refined. Options are 'log', 'hard_log', 'hard_hard_log' 

24 number_cells : int 

25 Number of cells in the mesh 

26 

27 Returns 

28 ------- 

29 Mesh 

30 One-dimensional mesh, ready for use in FEniCSx 

31 ''' 

32 if refinement_style == 'log': 

33 coordinates_np = (np.logspace(0, 1, number_cells+1) - 1) / 9 

34 elif refinement_style == 'hard_log': 

35 coordinates_np1 = (np.logspace(0,1,int(number_cells*0.9)+1,endpoint=False)-1)/9 * 0.1 

36 coordinates_np2 = 0.1 + (np.logspace(0,1,int(number_cells*0.1)+1)-1)/9 * 0.9 

37 coordinates_np = np.concatenate((coordinates_np1, coordinates_np2), axis=0) 

38 elif refinement_style == 'hard_hard_log': 

39 coordinates_np1 = (np.logspace(0,1,int(number_cells*0.9)+1,endpoint=False)-1)/9 * 0.004 

40 coordinates_np2 = 0.004 + (np.logspace(0,1,int(number_cells*0.1)+1)-1)/9 * 0.996 

41 coordinates_np = np.concatenate((coordinates_np1, coordinates_np2), axis=0) 

42 num_vertices = len(coordinates_np) 

43 num_cells = num_vertices - 1 

44 cells_np = np.column_stack((np.arange(num_cells), np.arange(1, num_cells+1))) 

45 gdim = 1 

46 shape = 'interval' # 'interval', 'triangle', 'quadrilateral', 'tetrahedron', 'hexahedron' 

47 degree = 1 

48 domain = Mesh(element("Lagrange", shape, 1, shape=(1,))) 

49 coordinates_np_ = [] 

50 [coordinates_np_.append([coord]) for coord in coordinates_np] 

51 msh = mesh.create_mesh(MPI.COMM_WORLD, cells_np, coordinates_np_, domain) 

52 return msh