|
| 1 | + |
| 2 | +import numpy as np |
| 3 | +from mpi4py import MPI |
| 4 | +import pygeosx |
| 5 | +from pygeosx_tools import wrapper |
| 6 | +from geosx_xml_tools.main import preprocess_parallel |
| 7 | + |
| 8 | + |
| 9 | +# PYGEOSX_STRESS_FN |
| 10 | +def stress_fn(x): |
| 11 | + """ |
| 12 | + Function to set stress values |
| 13 | +
|
| 14 | + Args: |
| 15 | + x (np.ndarray) the element centers |
| 16 | +
|
| 17 | + Returns: |
| 18 | + np.ndarray: stress values |
| 19 | + """ |
| 20 | + R = x[:, 0]**2 + x[:, 1]**2 + x[:, 2]**2 |
| 21 | + return np.sin(2.0 * np.pi * R / np.amax(R)) |
| 22 | +# PYGEOSX_STRESS_FN_END |
| 23 | + |
| 24 | + |
| 25 | +def run_problem(): |
| 26 | + """ |
| 27 | + Run the GEOSX problem |
| 28 | + """ |
| 29 | + # PYGEOSX_INITIALIZATION |
| 30 | + # Get the MPI rank |
| 31 | + comm = MPI.COMM_WORLD |
| 32 | + rank = comm.Get_rank() |
| 33 | + |
| 34 | + # Initialize the code and set initial conditions |
| 35 | + problem = pygeosx.initialize(rank, args) |
| 36 | + pygeosx.apply_initial_conditions() |
| 37 | + |
| 38 | + # Rather than specifying the wrapper paths explicitly, |
| 39 | + # search for them using a set of filters |
| 40 | + location_key = wrapper.get_matching_wrapper_path(problem, ['Level0', 'Region2', 'elementCenter']) |
| 41 | + stress_key = wrapper.get_matching_wrapper_path(problem, ['Level0', 'Region2', 'shale', 'stress']) |
| 42 | + ghost_key = wrapper.get_matching_wrapper_path(problem, ['Level0', 'Region2', 'cb1', 'ghostRank']) |
| 43 | + # PYGEOSX_INITIALIZATION_END |
| 44 | + |
| 45 | + # PYGEOSX_STRESS_IC |
| 46 | + # Print initial stress |
| 47 | + wrapper.print_global_value_range(problem, stress_key, 'stress') |
| 48 | + |
| 49 | + # Zero out stress |
| 50 | + wrapper.set_wrapper_to_value(problem, stress_key, 0.0) |
| 51 | + wrapper.print_global_value_range(problem, stress_key, 'stress') |
| 52 | + |
| 53 | + # Set stress via a function |
| 54 | + wrapper.set_wrapper_with_function(problem, stress_key, location_key, stress_fn, target_index=0) |
| 55 | + wrapper.set_wrapper_with_function(problem, stress_key, location_key, stress_fn, target_index=1) |
| 56 | + wrapper.set_wrapper_with_function(problem, stress_key, location_key, stress_fn, target_index=2) |
| 57 | + wrapper.print_global_value_range(problem, stress_key, 'stress') |
| 58 | + # PYGEOSX_STRESS_IC_END |
| 59 | + |
| 60 | + # PYGEOSX_MAIN_LOOP |
| 61 | + # Run the code |
| 62 | + while pygeosx.run() != pygeosx.COMPLETED: |
| 63 | + wrapper.print_global_value_range(problem, stress_key, 'stress') |
| 64 | + |
| 65 | + # Gather/allgather tests |
| 66 | + tmp = wrapper.gather_wrapper(problem, stress_key) |
| 67 | + print(wrapper.rank, 'gather', np.shape(tmp), flush=True) |
| 68 | + |
| 69 | + tmp = wrapper.allgather_wrapper(problem, stress_key) |
| 70 | + print(wrapper.rank, 'allgather', np.shape(tmp), flush=True) |
| 71 | + |
| 72 | + tmp = wrapper.allgather_wrapper(problem, stress_key, ghost_key=ghost_key) |
| 73 | + print(wrapper.rank, 'allgather_ghost_filtered', np.shape(tmp), flush=True) |
| 74 | + # PYGEOSX_MAIN_LOOP_END |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == '__main__': |
| 78 | + run_problem() |
| 79 | + |
| 80 | + |
0 commit comments