pydfnWorks
python wrapper for dfnWorks
add_attribute_to_mesh.py
Go to the documentation of this file.
1 import subprocess
2 from numpy import genfromtxt, zeros, savetxt
3 import sys
4 import os
5 
6 from pydfnworks.dfnGen.meshing.mesh_dfn_helper import run_lagrit_script
7 
8 
9 def create_variable_file(variable, variable_file, matid_file="materialid.dat"):
10  """
11  Creates a node based file for variables
12 
13  Parameters
14  -----------
15  variable : string
16  name of variable
17  variable_file : string
18  name of file containing variable files. Must be a single column where each line corresponds to that fracture number.
19  matid_file : string
20  name of materialid file produced by large. Normally produced by run_meshing. Can
21  Returns
22  ----------
23  variable_file_by_node : string
24  name of file containing node based values of the variable
25 
26  """
27 
28  print(f"--> Making {variable} by node file")
29  values = genfromtxt(variable_file, skip_header=1)[:, -1]
30  if not os.path.isfile(matid_file):
31  error = f"ERROR!!! Cannot locate the file '{matid_file}'\nExiting\n"
32  sys.stderr.write(error)
33  sys.exit(1)
34  nodes = genfromtxt(matid_file, skip_header=3).astype(int)
35  value_by_node = zeros(len(nodes))
36  for i, n in enumerate(nodes):
37  value_by_node[i] = values[n - 1]
38  variable_file_by_node = f"{variable}_by_node.dat"
39  savetxt(variable_file_by_node, value_by_node)
40  print("--> Complete")
41  return variable_file_by_node
42 
43 
44 def create_lagrit_append_script(variable, variable_file, mesh_file_in,
45  mesh_file_out):
46  """
47  Creates a LaGriT script to append the attribute to the mesh
48 
49  Parameters
50  -----------
51  variable : string
52  name of variable
53  variable_file : string
54  name of file containing variable files. Must be a single column where each line corresponds to that node number in the mesh
55  mesh_file_in : string
56  Name of source mesh file
57  mesh_file_out : string
58  Name of Target mesh file
59  Returns
60  ----------
61  lagrit_file : string
62  Name of LaGriT output file
63  """
64  print("Making LaGriT script")
65  lagrit_script = f'''
66 read / {mesh_file_in} / mo1
67 cmo / addatt / mo1 / {variable} / vdouble / scalar / nnodes
68 cmo / setatt / mo1 / {variable} / 1 0 0 / 1
69 cmo / readatt / mo1 / {variable} / 1, 0, 0 / {variable_file}
70 dump / {mesh_file_out} / mo1
71 finish
72 '''
73 
74  lagrit_file = f"add_{variable}_to_mesh.lgi"
75  fp = open(lagrit_file, "w")
76  fp.write(lagrit_script)
77  fp.flush()
78  fp.close()
79  print("Complete")
80  return lagrit_file
81 
82 
84  variable,
85  variable_file,
86  mesh_file_in,
87  mesh_file_out=None,
88  cell_based=None):
89  """
90  Adds a variable to the nodes of a mesh. Can be either fracture (material) based
91  or node based.
92 
93  Parameters
94  -----------
95  self : object
96  DFN Class
97  variable : string
98  name of variable
99  variable_file : string
100  name of file containing variable files. Must be a single column where each line corresponds to that node number in the mesh
101  mesh_file_in : string
102  Name of source mesh file
103  mesh_file_out : string
104  Name of Target mesh file. If no name if provide, mesh_file_in will be used
105  cell_based : bool
106  Set to True if variable_file contains cell-based values, Set to False
107  if variable_file provide fracture based values
108 
109  Returns
110  ----------
111  lagrit_file : string
112  Name of LaGriT output file
113  """
114 
115  if mesh_file_out is None:
116  mesh_file_out = mesh_file_in
117 
118  if cell_based:
119  lagrit_file = create_lagrit_append_script(variable, variable_file,
120  mesh_file_in, mesh_file_out)
121  else:
122  variable_file_by_node = create_variable_file(variable, variable_file)
123  lagrit_file = create_lagrit_append_script(variable,
124  variable_file_by_node,
125  mesh_file_in, mesh_file_out)
126  run_lagrit_script(lagrit_file)
def add_variable_to_mesh(self, variable, variable_file, mesh_file_in, mesh_file_out=None, cell_based=None)
def create_variable_file(variable, variable_file, matid_file="materialid.dat")
def create_lagrit_append_script(variable, variable_file, mesh_file_in, mesh_file_out)