|
| 1 | +import KratosMultiphysics as KM |
| 2 | +import KratosMultiphysics.IgaApplication |
| 3 | +from KratosMultiphysics.CoSimulationApplication.co_simulation_analysis import CoSimulationAnalysis |
| 4 | + |
| 5 | +# External imports |
| 6 | +import importlib |
| 7 | +import matplotlib.pyplot as plt |
| 8 | +import numpy as np |
| 9 | +from scipy.interpolate import griddata |
| 10 | + |
| 11 | +import sys |
| 12 | +import time |
| 13 | + |
| 14 | +class CustomCoSimulationAnalysis(CoSimulationAnalysis): |
| 15 | + def __init__(self, parameters): |
| 16 | + """Call the base class constructor.""" |
| 17 | + super().__init__(parameters) |
| 18 | + self.time_history = [] |
| 19 | + self.disp_x_history_A = [] |
| 20 | + self.disp_x_history_B = [] |
| 21 | + |
| 22 | + def OutputSolutionStep(self): |
| 23 | + super().OutputSolutionStep() |
| 24 | + |
| 25 | + solver = self._GetSolver() |
| 26 | + |
| 27 | + if hasattr(solver, "model") and hasattr(solver.model, "solver_wrappers"): |
| 28 | + structure_solver = solver.model.solver_wrappers.get("structure") |
| 29 | + if structure_solver and hasattr(structure_solver, "model"): |
| 30 | + sub_model = structure_solver.model |
| 31 | + model_part_name = "IgaModelPart" |
| 32 | + |
| 33 | + if hasattr(sub_model, "HasModelPart") and sub_model.HasModelPart(model_part_name): |
| 34 | + model_part = sub_model.GetModelPart(model_part_name) |
| 35 | + |
| 36 | + wet_interface_sub_model_part = model_part.GetSubModelPart("Load_4") |
| 37 | + current_time = wet_interface_sub_model_part.ProcessInfo[KratosMultiphysics.TIME] |
| 38 | + |
| 39 | + # Only do this the first time |
| 40 | + if not hasattr(self, "tracking_conditions_initialized"): |
| 41 | + self.tracking_conditions_initialized = True |
| 42 | + |
| 43 | + # Choose target locations |
| 44 | + self.point_A = (0.50025, 0.25) # Replace with your target coordinates |
| 45 | + self.point_B = (0.50, 0.125) |
| 46 | + |
| 47 | + def get_closest_condition(target_point): |
| 48 | + min_dist = float("inf") |
| 49 | + closest_cond = None |
| 50 | + for cond in wet_interface_sub_model_part.Conditions: |
| 51 | + center = cond.GetGeometry().Center() |
| 52 | + dist = (center.X - target_point[0])**2 + (center.Y - target_point[1])**2 |
| 53 | + if dist < min_dist: |
| 54 | + min_dist = dist |
| 55 | + closest_cond = cond |
| 56 | + return closest_cond |
| 57 | + |
| 58 | + self.condition_A = get_closest_condition(self.point_A) |
| 59 | + self.condition_B = get_closest_condition(self.point_B) |
| 60 | + |
| 61 | + # --- Evaluate displacement at A --- |
| 62 | + condition = self.condition_A |
| 63 | + geom = condition.GetGeometry() |
| 64 | + N = geom.ShapeFunctionsValues() |
| 65 | + |
| 66 | + solution_A = 0.0 |
| 67 | + for i, node in enumerate(condition.GetNodes()): |
| 68 | + solution_A += node.GetSolutionStepValue(KratosMultiphysics.DISPLACEMENT_X) * N[0, i] |
| 69 | + |
| 70 | + self.time_history.append(current_time) |
| 71 | + self.disp_x_history_A.append(solution_A) |
| 72 | + |
| 73 | + # --- Evaluate displacement at B --- |
| 74 | + condition = self.condition_B |
| 75 | + geom = condition.GetGeometry() |
| 76 | + N = geom.ShapeFunctionsValues() |
| 77 | + |
| 78 | + solution_B = 0.0 |
| 79 | + for i, node in enumerate(condition.GetNodes()): |
| 80 | + solution_B += node.GetSolutionStepValue(KratosMultiphysics.DISPLACEMENT_X) * N[0, i] |
| 81 | + |
| 82 | + self.disp_x_history_B.append(solution_B) |
| 83 | + |
| 84 | + def Finalize(self): |
| 85 | + super().Finalize() |
| 86 | + |
| 87 | + import matplotlib.pyplot as plt |
| 88 | + |
| 89 | + # Enable LaTeX-style rendering |
| 90 | + plt.rcParams["text.usetex"] = True |
| 91 | + plt.rcParams["font.family"] = "serif" |
| 92 | + |
| 93 | + # Plot |
| 94 | + plt.figure() |
| 95 | + plt.plot(self.time_history, self.disp_x_history_A, label=r"$u_x$ at Point A") |
| 96 | + plt.plot(self.time_history, self.disp_x_history_B, label=r"$u_x$ at Point B") |
| 97 | + |
| 98 | + # Labels with LaTeX formatting |
| 99 | + plt.xlabel(r"\textbf{Time} [s]") |
| 100 | + plt.ylabel(r"\textbf{Displacement} $u_x$ [m]") |
| 101 | + plt.title(r"\textbf{Displacement vs Time (Nearest Neighbor Mapper)}", fontsize=14) |
| 102 | + |
| 103 | + # Grid and legend |
| 104 | + plt.grid(True) |
| 105 | + plt.legend(loc="best") |
| 106 | + |
| 107 | + # Save and show |
| 108 | + plt.savefig("displacement_vs_time.pdf", dpi=300, bbox_inches="tight") |
| 109 | + plt.show() |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + |
| 113 | + with open("ProjectParametersCoSim.json", 'r') as parameter_file: |
| 114 | + parameters = KratosMultiphysics.Parameters(parameter_file.read()) |
| 115 | + |
| 116 | + simulation = CustomCoSimulationAnalysis(parameters) |
| 117 | + simulation.Run() |
0 commit comments