-
Notifications
You must be signed in to change notification settings - Fork 913
Python source term for heat solver #2626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bigfooted
wants to merge
10
commits into
develop
Choose a base branch
from
feature_heat_custom_source
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+201
−0
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0d09263
python source term for heat solver
bigfooted 4a036f6
remove unused variable
bigfooted 899412f
Merge branch 'develop' into feature_heat_custom_source
bigfooted f4cb7e7
Merge branch 'develop' into feature_heat_custom_source
bigfooted 8d975a5
Merge branch 'develop' into feature_heat_custom_source
bigfooted f1bc726
Merge branch 'develop' into feature_heat_custom_source
bigfooted e103028
Merge branch 'develop' into feature_heat_custom_source
bigfooted 22d6253
added regression
bigfooted 0a4c24c
Merge branch 'feature_heat_custom_source' of https://github.com/su2co…
bigfooted 41c2fad
cleanup
bigfooted File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| ## \file run.py | ||
| # \brief Unsteady adjoint heat transfer case with custom heat flux. | ||
| # \version 8.3.0 "Harrier" | ||
| # | ||
| # SU2 Project Website: https://su2code.github.io | ||
| # | ||
| # The SU2 Project is maintained by the SU2 Foundation | ||
| # (http://su2foundation.org) | ||
| # | ||
| # Copyright 2012-2025, SU2 Contributors (cf. AUTHORS.md) | ||
| # | ||
| # SU2 is free software; you can redistribute it and/or | ||
| # modify it under the terms of the GNU Lesser General Public | ||
| # License as published by the Free Software Foundation; either | ||
| # version 2.1 of the License, or (at your option) any later version. | ||
| # | ||
| # SU2 is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| # Lesser General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU Lesser General Public | ||
| # License along with SU2. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| import pysu2ad | ||
| from mpi4py import MPI | ||
|
|
||
| common_settings = """ | ||
| SOLVER= HEAT_EQUATION | ||
| INC_NONDIM= DIMENSIONAL | ||
| TIME_DOMAIN= YES | ||
| TIME_STEP= 0.002 | ||
| TIME_MARCHING= DUAL_TIME_STEPPING-2ND_ORDER | ||
| FREESTREAM_TEMPERATURE= 300 | ||
| MATERIAL_DENSITY= 1000 | ||
| SPECIFIC_HEAT_CP = 500 | ||
| THERMAL_CONDUCTIVITY_CONSTANT= 10.0 | ||
| MARKER_HEATFLUX= ( x_minus, 0, x_plus, 0, y_minus, 0, y_plus, 0 ) | ||
| MARKER_PYTHON_CUSTOM= ( x_minus, x_plus, y_minus, y_plus ) | ||
| MARKER_MONITORING= ( x_minus, x_plus, y_minus, y_plus ) | ||
| PYTHON_CUSTOM_SOURCE= YES | ||
| NUM_METHOD_GRAD= GREEN_GAUSS | ||
| CFL_NUMBER= 100 | ||
| LINEAR_SOLVER= CONJUGATE_GRADIENT | ||
| DISCADJ_LIN_SOLVER= CONJUGATE_GRADIENT | ||
| LINEAR_SOLVER_PREC= ILU | ||
| DISCADJ_LIN_PREC= ILU | ||
| LINEAR_SOLVER_ERROR= 1E-5 | ||
| LINEAR_SOLVER_ITER= 100 | ||
| MESH_FORMAT= RECTANGLE | ||
| MESH_BOX_SIZE= ( 33, 33, 0 ) | ||
| MESH_BOX_LENGTH= ( __SIZE__, __SIZE__, 0 ) | ||
| OUTPUT_FILES= RESTART, PARAVIEW | ||
| OUTPUT_WRT_FREQ= 100,100 | ||
| OBJECTIVE_FUNCTION= AVG_TEMPERATURE | ||
| INNER_ITER= 20 | ||
| CONV_RESIDUAL_MINVAL= -4 | ||
| CONV_STARTITER= 2 | ||
| MAX_TIME= 10.0 | ||
| TIME_ITER= 101 | ||
| """ | ||
|
|
||
| primal_settings = """ | ||
| MATH_PROBLEM= DIRECT | ||
| SCREEN_OUTPUT= TIME_ITER, CUR_TIME, INNER_ITER, RMS_RES, LINSOL, AVG_TEMPERATURE, TAVG_AVG_TEMPERATURE | ||
| HISTORY_OUTPUT= ITER, RMS_RES, HEAT, TAVG_HEAT | ||
| """ | ||
|
|
||
| def HeatSource(time, driver, iPoint): | ||
| """Applies a source in the lower left quadrant for a period of time.""" | ||
| allCoords = driver.Coordinates() | ||
| coord = allCoords.Get(iPoint) | ||
| x = coord[0] | ||
| y = coord[1] | ||
| xp = 0.0125 | ||
| yp = 0.0125 | ||
| R = 0.0125 | ||
| Source = 0.0 | ||
| if (time > 0.0): | ||
| if ((x-xp)*(x-xp) + (y-yp)*(y-yp) < R*R): | ||
| Source = 100.0 | ||
|
|
||
| return Source | ||
|
|
||
| def RunPrimal(size): | ||
| """ | ||
| Run the heat solver with a custom heat (function of time) flux on all boundaries. | ||
| Returns the final average boundary temperature. | ||
| """ | ||
| comm = MPI.COMM_WORLD | ||
| rank = comm.Get_rank() | ||
|
|
||
| if rank == 0: | ||
| with open('config_unsteady.cfg', 'w') as f: | ||
| f.write(common_settings.replace('__SIZE__', str(size)) + primal_settings) | ||
| comm.Barrier() | ||
|
|
||
| # Initialize the primal driver of SU2, this includes solver preprocessing. | ||
| try: | ||
| driver = pysu2ad.CSinglezoneDriver('config_unsteady.cfg', 1, comm) | ||
| except TypeError as exception: | ||
| print('A TypeError occured in pysu2ad.CSinglezoneDriver : ', exception) | ||
| raise | ||
|
|
||
| # Get the ID of the markers where the heat flux is applied. | ||
| all_marker_ids = driver.GetMarkerIndices() | ||
| marker_names = ['x_minus', 'x_plus', 'y_minus', 'y_plus'] | ||
| marker_ids = [] | ||
| for name in marker_names: | ||
| marker_ids.append(all_marker_ids[name] if name in all_marker_ids else -1) | ||
|
|
||
| # Run the time loop in python to vary the heat flux. | ||
| dt = driver.GetUnsteadyTimeStep() | ||
|
|
||
| iHEATSOLVER = driver.GetSolverIndices()['HEAT'] | ||
| Source = driver.UserDefinedSource(iHEATSOLVER) | ||
|
|
||
|
|
||
| for time_iter in range(driver.GetNumberTimeIter()): | ||
| # Custom heat flux. | ||
| #ApplyHeatFlux(time_iter * dt, driver, marker_ids) | ||
|
|
||
| # set the source term, per point | ||
| for i_node in range(driver.GetNumberNodes() - driver.GetNumberHaloNodes()): | ||
| # add source term: | ||
| S = HeatSource(time_iter * dt, driver, i_node) | ||
| Source.Set(i_node, 0, S) | ||
|
|
||
|
|
||
| driver.Preprocess(time_iter) | ||
|
|
||
| # Run one time iteration. | ||
| driver.Run() | ||
| driver.Postprocess() | ||
| driver.Update() | ||
|
|
||
| # Monitor the solver and output solution to file if required. | ||
| driver.Monitor(time_iter) | ||
| driver.Output(time_iter) | ||
|
|
||
| # Get the final average temperature. | ||
| avg_temperature = driver.GetOutputValue('AVG_TEMPERATURE') | ||
|
|
||
| # Finalize the solver and exit cleanly. | ||
| driver.Finalize() | ||
|
|
||
| return avg_temperature | ||
|
|
||
|
|
||
| def main(): | ||
| comm = MPI.COMM_WORLD | ||
| #rank = comm.Get_rank() | ||
|
|
||
| RunPrimal(0.1) | ||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Unused local variable Note
Copilot Autofix
AI about 10 hours ago
To fix an unused local variable, either remove the assignment if it has no side effects, or, if it is intentionally kept for clarity, rename it using a convention that indicates it is unused (for example,
_or a name containingunused). Here the right-hand sideMPI.COMM_WORLDhas no side effects; it just returns a communicator object. The best minimal fix that preserves structure and makes intent clear is to renamecommto_commso that tools recognize it as intentionally unused, and optionally keep the commentedrankline as-is for future reference. This change is localized tomain()inTestCases/py_wrapper/custom_heat_source/run.pyaround line 162, and does not require any new imports or other code changes.