|
| 1 | +from __future__ import print_function, absolute_import, division #makes KratosMultiphysics backward compatible with python 2.6 and 2.7 |
| 2 | + |
| 3 | +# Time monitoring |
| 4 | +import time as timer |
| 5 | +print(timer.ctime()) |
| 6 | +initial_time = timer.perf_counter() |
| 7 | + |
| 8 | +## Necessary modules ----------------------------------------------------------------------------------------- |
| 9 | + |
| 10 | +# Import system python |
| 11 | +import os |
| 12 | + |
| 13 | +# Including kratos path |
| 14 | +import KratosMultiphysics |
| 15 | +# Including Applications path |
| 16 | +import KratosMultiphysics.ExternalSolversApplication as KratosSolvers |
| 17 | +import KratosMultiphysics.TrilinosApplication as TrilinosApplication |
| 18 | +import KratosMultiphysics.ConvectionDiffusionApplication as KratosConvDiff |
| 19 | +import KratosMultiphysics.SolidMechanicsApplication as KratosSolid |
| 20 | +import KratosMultiphysics.PoromechanicsApplication as KratosPoro |
| 21 | +import KratosMultiphysics.StructuralMechanicsApplication as KratosStructural |
| 22 | +import KratosMultiphysics.DamApplication as KratosDam |
| 23 | + |
| 24 | +# Parsing the parameters |
| 25 | +parameter_file = open("ProjectParameters.json",'r') |
| 26 | +ProjectParameters = KratosMultiphysics.Parameters( parameter_file.read()) |
| 27 | + |
| 28 | +# Parallel Configuration |
| 29 | +parallel_type = ProjectParameters["problem_data"]["parallel_type"].GetString() |
| 30 | +parallel=KratosMultiphysics.OpenMPUtils() |
| 31 | +parallel.SetNumThreads(ProjectParameters["problem_data"]["number_of_threads"].GetInt()) |
| 32 | +if parallel_type == "MPI": |
| 33 | + import KratosMultiphysics.mpi as KratosMPI |
| 34 | + print("MPI parallel configuration. OMP_NUM_THREADS =",parallel.GetNumThreads()) |
| 35 | +else: |
| 36 | + print("OpenMP parallel configuration. OMP_NUM_THREADS =",parallel.GetNumThreads()) |
| 37 | + |
| 38 | + |
| 39 | +## Defining variables ---------------------------------------------------------------------------------------- |
| 40 | +problem_path = os.getcwd() |
| 41 | +problem_name = ProjectParameters["problem_data"]["problem_name"].GetString() |
| 42 | +domain_size = ProjectParameters["problem_data"]["domain_size"].GetInt() |
| 43 | +echo_level = ProjectParameters["solver_settings"]["echo_level"].GetInt() |
| 44 | +buffer_size = ProjectParameters["solver_settings"]["buffer_size"].GetInt() |
| 45 | + |
| 46 | +## Model part ------------------------------------------------------------------------------------------------ |
| 47 | + |
| 48 | +# Defining the model part |
| 49 | + |
| 50 | +main_model_part = KratosMultiphysics.ModelPart(ProjectParameters["problem_data"]["model_part_name"].GetString()) |
| 51 | + |
| 52 | +# Construct the solver (main setting methods are located in the solver_module) |
| 53 | +solver_module = __import__(ProjectParameters["solver_settings"]["solver_type"].GetString()) |
| 54 | +solver = solver_module.CreateSolver(main_model_part, ProjectParameters["solver_settings"]) |
| 55 | + |
| 56 | +# Add variables |
| 57 | +solver.AddVariables() |
| 58 | + |
| 59 | +# Read model_part (note: the buffer_size is set here) |
| 60 | +solver.ImportModelPart() |
| 61 | + |
| 62 | +# Add degrees of freedom |
| 63 | +solver.AddDofs() |
| 64 | + |
| 65 | +# Creation of Kratos model |
| 66 | +DamModel = KratosMultiphysics.Model() |
| 67 | +DamModel.AddModelPart(main_model_part) |
| 68 | + |
| 69 | +# Build sub_model_parts or submeshes (rearrange parts for the application of custom processes) |
| 70 | +## Get the list of the submodel part in the object Model |
| 71 | +for i in range(ProjectParameters["solver_settings"]["processes_sub_model_part_list"].size()): |
| 72 | + part_name = ProjectParameters["solver_settings"]["processes_sub_model_part_list"][i].GetString() |
| 73 | + DamModel.AddModelPart(main_model_part.GetSubModelPart(part_name)) |
| 74 | + |
| 75 | +# Print control |
| 76 | +if(echo_level > 1): |
| 77 | + print(main_model_part) |
| 78 | + for properties in main_model_part.Properties: |
| 79 | + print(properties) |
| 80 | + |
| 81 | +## Initialize ------------------------------------------------------------------------------------------------ |
| 82 | + |
| 83 | +# Construct the processes to be applied |
| 84 | +import process_factory |
| 85 | +list_of_processes = process_factory.KratosProcessFactory(DamModel).ConstructListOfProcesses( ProjectParameters["constraints_process_list"] ) |
| 86 | +list_of_processes += process_factory.KratosProcessFactory(DamModel).ConstructListOfProcesses( ProjectParameters["loads_process_list"] ) |
| 87 | + |
| 88 | +# Print list of constructed processes |
| 89 | +if(echo_level>1): |
| 90 | + for process in list_of_processes: |
| 91 | + print(process) |
| 92 | + |
| 93 | +# Initialize processes |
| 94 | +for process in list_of_processes: |
| 95 | + process.ExecuteInitialize() |
| 96 | + |
| 97 | +# Initialize GiD I/O |
| 98 | +computing_model_part = solver.GetComputingModelPart() |
| 99 | +output_settings = ProjectParameters["output_configuration"] |
| 100 | +if parallel_type == "OpenMP": |
| 101 | + import poromechanics_cleaning_utility |
| 102 | + poromechanics_cleaning_utility.CleanPreviousFiles(problem_path) # Clean previous post files |
| 103 | + from gid_output_process import GiDOutputProcess |
| 104 | + gid_output = GiDOutputProcess(computing_model_part, |
| 105 | + problem_name, |
| 106 | + output_settings) |
| 107 | +else: |
| 108 | + from gid_output_process_mpi import GiDOutputProcessMPI |
| 109 | + gid_output = GiDOutputProcessMPI(computing_model_part, |
| 110 | + problem_name, |
| 111 | + output_settings) |
| 112 | +gid_output.ExecuteInitialize() |
| 113 | + |
| 114 | +#Initialize the solver |
| 115 | +solver.Initialize() |
| 116 | + |
| 117 | +# ExecuteBeforeSolutionLoop |
| 118 | +for process in list_of_processes: |
| 119 | + process.ExecuteBeforeSolutionLoop() |
| 120 | + |
| 121 | +## Set results when they are written in a single file |
| 122 | +gid_output.ExecuteBeforeSolutionLoop() |
| 123 | + |
| 124 | +## It is just need one step --------------------------------------------------------------------------------------------- |
| 125 | + |
| 126 | +# Update imposed conditions |
| 127 | +for process in list_of_processes: |
| 128 | + process.ExecuteInitializeSolutionStep() |
| 129 | + |
| 130 | +gid_output.ExecuteInitializeSolutionStep() |
| 131 | + |
| 132 | +# Solve step |
| 133 | +solver.Solve() |
| 134 | + |
| 135 | +gid_output.ExecuteFinalizeSolutionStep() |
| 136 | + |
| 137 | +for process in list_of_processes: |
| 138 | + process.ExecuteFinalizeSolutionStep() |
| 139 | + |
| 140 | +for process in list_of_processes: |
| 141 | + process.ExecuteBeforeOutputStep() |
| 142 | + |
| 143 | +for process in list_of_processes: |
| 144 | + process.ExecuteAfterOutputStep() |
| 145 | + |
| 146 | +## Finalize -------------------------------------------------------------------------------------------------- |
| 147 | + |
| 148 | +# Finalizing output files |
| 149 | +eigen_values = [ev for ev in main_model_part.ProcessInfo[KratosStructural.EIGENVALUE_VECTOR]] |
| 150 | +print ("The Eigenvalues are:") |
| 151 | +print (eigen_values) |
| 152 | +eigen_utility = KratosStructural.EigenvectorToSolutionStepVariableTransferUtility() |
| 153 | +for step in range(len(eigen_values)): |
| 154 | + main_model_part.ProcessInfo[KratosMultiphysics.TIME] = float(step+1) |
| 155 | + eigen_utility.Transfer(main_model_part,step,0) |
| 156 | + gid_output.PrintOutput() |
| 157 | + |
| 158 | +gid_output.ExecuteFinalize() |
| 159 | + |
| 160 | +for process in list_of_processes: |
| 161 | + process.ExecuteFinalize() |
| 162 | + |
| 163 | +# Writing an output file |
| 164 | +output_name = 'Eigenvalues.txt' |
| 165 | +with open(output_name, 'w') as output: |
| 166 | + output.write ("The Eigenvalues are:") |
| 167 | + output.write (str(eigen_values)) |
| 168 | + |
| 169 | +# Time control |
| 170 | +print("Analysis Completed. Elapsed Time = %.3f" % (timer.perf_counter() - initial_time)," seconds.") |
| 171 | +print(timer.ctime()) |
0 commit comments