Skip to content

Commit 3449719

Browse files
committed
* Add support of pow and divide operation.
* Allow register the value of a scalar field after the field is defined. * Update the docstring and documentations.
1 parent aa35414 commit 3449719

39 files changed

+5214
-1075
lines changed

.github/workflows/build_docs.yml

+35-35
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
on:
2-
push:
3-
branches:
4-
- main
5-
permissions:
6-
contents: write
7-
jobs:
8-
deploy:
9-
runs-on: ubuntu-latest
10-
steps:
11-
- uses: actions/checkout@v4
12-
- name: Configure Git Credentials
13-
run: |
14-
git config user.name github-actions[bot]
15-
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
16-
- uses: actions/setup-python@v5
17-
with:
18-
python-version: 3.x
19-
- run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV
20-
21-
- name: Install dependencies
22-
run: |
23-
python -m pip install --upgrade pip
24-
pip install setuptools
25-
bash install.sh
26-
python -m pip install -r docs/requirements.txt
27-
28-
- uses: actions/cache@v4
29-
with:
30-
key: mkdocs-material-${{ env.cache_id }}
31-
path: .cache
32-
restore-keys: |
33-
mkdocs-material-
34-
- run: pip install mkdocs-material
35-
- run: mkdocs gh-deploy --force
1+
on:
2+
push:
3+
branches:
4+
- main
5+
permissions:
6+
contents: write
7+
jobs:
8+
deploy:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
- name: Configure Git Credentials
13+
run: |
14+
git config user.name github-actions[bot]
15+
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
16+
- uses: actions/setup-python@v5
17+
with:
18+
python-version: 3.x
19+
- run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV
20+
21+
- name: Install dependencies
22+
run: |
23+
python -m pip install --upgrade pip
24+
pip install setuptools
25+
bash install.sh
26+
python -m pip install -r docs/requirements.txt
27+
28+
- uses: actions/cache@v4
29+
with:
30+
key: mkdocs-material-${{ env.cache_id }}
31+
path: .cache
32+
restore-keys: |
33+
mkdocs-material-
34+
- run: pip install mkdocs-material
35+
- run: mkdocs gh-deploy --force

ConvDO/operator.py renamed to ConvDO/_operator_deprecated.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
#usr/bin/python3
22
# -*- coding: UTF-8 -*-
3-
import os,torch
3+
import torch
44
from . import *
55
from .domain import *
66
from .obstacles import *
77
from .boundaries import *
8+
from typing import Optional
89

910
print("Warning: This is an old version of operator which will be deprecated soon. Please use 'operator_HO' instead.")
1011

1112
class ScalarField(CommutativeValue):
1213

13-
def __init__(self,value,domain=UnconstrainedDomain()) -> None:
14+
def __init__(self,value:torch.Tensor,
15+
domain:Optional[Domain]=UnconstrainedDomain()) -> None:
1416
self.value=value
1517
self.domain=domain
1618

@@ -55,7 +57,7 @@ def __mul__(self,other):
5557
else:
5658
try:
5759
return self*other
58-
except TypeError:
60+
except Exception:
5961
return NotImplemented
6062

6163
class GradY():
@@ -87,7 +89,7 @@ def __mul__(self,other):
8789
else:
8890
try:
8991
return self*other
90-
except TypeError:
92+
except Exception:
9193
return NotImplemented
9294

9395
class LaplacianX():
@@ -119,7 +121,7 @@ def __mul__(self,other):
119121
else:
120122
try:
121123
return self*other
122-
except TypeError:
124+
except Exception:
123125
return NotImplemented
124126

125127
class LaplacianY():
@@ -151,7 +153,7 @@ def __mul__(self,other):
151153
else:
152154
try:
153155
return self*other
154-
except TypeError:
156+
except Exception:
155157
return NotImplemented
156158

157159
class Laplacian():

ConvDO/boundaries.py

+114-33
Original file line numberDiff line numberDiff line change
@@ -22,94 +22,123 @@ def correct_left(self,padded_face,ori_field,delta):
2222
pass
2323

2424
class DirichletBoundary(Boundary):
25+
'''
26+
DirichletBoundary is a boundary condition that the value of the field is fixed at the boundary.
27+
'''
2528

26-
def __init__(self,boundary_value) -> None:
29+
def __init__(self,boundary_value: float) -> None:
2730
super().__init__()
28-
self.boundary_value=boundary_value
29-
self.face_calculator=DirichletFace(boundary_value)
31+
self.boundary_face=DirichletFace(boundary_value)
3032

3133
def correct_top(self,padded_face,ori_field,delta):
32-
padded_face[...,0,:]=self.face_calculator.correct_outward_padding(ori_field[...,0,:])
34+
padded_face[...,0,:]=self.boundary_face.correct_outward_padding(ori_field[...,0,:])
3335
return padded_face
3436

3537
def correct_right(self,padded_face,ori_field,delta):
36-
padded_face[...,:,-1]=self.face_calculator.correct_outward_padding(ori_field[...,:,-1])
38+
padded_face[...,:,-1]=self.boundary_face.correct_outward_padding(ori_field[...,:,-1])
3739
return padded_face
3840

3941
def correct_bottom(self,padded_face,ori_field,delta):
40-
padded_face[...,-1,:]=self.face_calculator.correct_inward_padding(ori_field[...,-1,:])
42+
padded_face[...,-1,:]=self.boundary_face.correct_inward_padding(ori_field[...,-1,:])
4143
return padded_face
4244

4345
def correct_left(self,padded_face,ori_field,delta):
44-
padded_face[...,:,0]=self.face_calculator.correct_inward_padding(ori_field[...,:,0])
46+
padded_face[...,:,0]=self.boundary_face.correct_inward_padding(ori_field[...,:,0])
4547
return padded_face
4648

4749
# + :
4850
def __add__(self, other):
4951
if isinstance(other,DirichletBoundary):
5052
# Dirichlet+Dirichlet=Dirichlet
51-
return DirichletBoundary(self.boundary_value+other.boundary_value)
53+
return DirichletBoundary(self.boundary_face.face_value+other.boundary_face.face_value)
5254
elif isinstance(other,Boundary):
5355
# Dirichlet+otherboundary=uncontrainedBoundary
5456
return UnConstrainedBoundary()
5557
else:
5658
try:
5759
# Dirichlet+number=Dirichlet
58-
return DirichletBoundary(self.boundary_value+other)
59-
except TypeError:
60+
return DirichletBoundary(self.boundary_face.face_value+other)
61+
except Exception:
6062
return NotImplemented
6163

6264
# *
6365
def __mul__(self,other):
6466
if isinstance(other,DirichletBoundary):
6567
# Dirichlet*Dirichlet=Dirichlet
66-
return DirichletBoundary(self.boundary_value*other.boundary_value)
68+
return DirichletBoundary(self.boundary_face.face_value*other.boundary_face.face_value)
6769
elif isinstance(other,Boundary):
6870
# Dirichlet*otherboundary=uncontrainedBoundary
6971
return UnConstrainedBoundary()
7072
else:
7173
try:
7274
# Dirichlet*number=Dirichlet
73-
return DirichletBoundary(self.boundary_value*other)
74-
except TypeError:
75+
return DirichletBoundary(self.boundary_face.face_value*other)
76+
except Exception:
7577
return NotImplemented
78+
79+
def __truediv__(self, other):
80+
if isinstance(other,DirichletBoundary):
81+
return DirichletBoundary(self.boundary_face.face_value/other.boundary_face.face_value)
82+
elif isinstance(other,Boundary):
83+
return UnConstrainedBoundary()
84+
else:
85+
try:
86+
return DirichletBoundary(self.boundary_face.face_value/other)
87+
except Exception:
88+
return NotImplemented
89+
90+
def __rtruediv__(self, other):
91+
if isinstance(other,DirichletBoundary):
92+
return DirichletBoundary(other.boundary_face.face_value/self.boundary_face.face_value)
93+
elif isinstance(other,Boundary):
94+
return UnConstrainedBoundary()
95+
else:
96+
try:
97+
return DirichletBoundary(other/self.boundary_face.face_value)
98+
except Exception:
99+
return NotImplemented
100+
101+
def __pow__(self, other):
102+
return DirichletBoundary(self.boundary_face.face_value**other)
76103

77104
class NeumannBoundary(Boundary):
105+
'''
106+
NeumannBoundary is a boundary condition that the gradient of the field is fixed at the boundary.
107+
'''
78108

79-
def __init__(self,face_gradient) -> None:
109+
def __init__(self,face_gradient: float) -> None:
80110
super().__init__()
81-
self.face_gradient=face_gradient
82-
self.face_calculator=NeumannFace(face_gradient)
111+
self.boundary_face=NeumannFace(face_gradient)
83112

84113
def correct_top(self,padded_face,ori_field,delta):
85-
padded_face[...,0,:]=self.face_calculator.correct_outward_padding(ori_field[...,0,:],delta)
114+
padded_face[...,0,:]=self.boundary_face.correct_outward_padding(ori_field[...,0,:],delta)
86115
return padded_face
87116

88117
def correct_right(self,padded_face,ori_field,delta):
89-
padded_face[...,:,-1]=self.face_calculator.correct_outward_padding(ori_field[...,:,-1],delta)
118+
padded_face[...,:,-1]=self.boundary_face.correct_outward_padding(ori_field[...,:,-1],delta)
90119
return padded_face
91120

92121
def correct_bottom(self,padded_face,ori_field,delta):
93-
padded_face[...,-1,:]=self.face_calculator.correct_inward_padding(ori_field[...,-1,:],delta)
122+
padded_face[...,-1,:]=self.boundary_face.correct_inward_padding(ori_field[...,-1,:],delta)
94123
return padded_face
95124

96125
def correct_left(self,padded_face,ori_field,delta):
97-
padded_face[...,:,0]=self.face_calculator.correct_inward_padding(ori_field[...,:,0],delta)
126+
padded_face[...,:,0]=self.boundary_face.correct_inward_padding(ori_field[...,:,0],delta)
98127
return padded_face
99128

100129
# + :
101130
def __add__(self, other):
102131
if isinstance(other,NeumannBoundary):
103132
# Neumann+Neumann=Dirichlet
104-
return NeumannBoundary(self.face_gradient+other.face_gradient)
133+
return NeumannBoundary(self.boundary_face.face_gradient+other.boundary_face.face_gradient)
105134
elif isinstance(other,Boundary):
106-
# Dirichlet+otherboundary=uncontrainedBoundary
135+
# Neumann+otherboundary=uncontrainedBoundary
107136
return UnConstrainedBoundary()
108137
else:
109138
try:
110-
# Neumann+number=NeumannBoundary
111-
return NeumannBoundary(self.face_gradient)
112-
except TypeError:
139+
# Neumann+number=Neumann
140+
return NeumannBoundary(self.boundary_face.face_gradient)
141+
except Exception:
113142
return NotImplemented
114143

115144
# *
@@ -119,32 +148,58 @@ def __mul__(self,other):
119148
return UnConstrainedBoundary()
120149
else:
121150
try:
122-
# Neumann*number=Neumann
123-
return DirichletBoundary(self.boundary_value*other)
124-
except TypeError:
151+
# Neumann*number=Neumann*number
152+
return DirichletBoundary(self.boundary_face.face_value*other)
153+
except Exception:
125154
return NotImplemented
126155

156+
def __truediv__(self, other):
157+
if isinstance(other,Boundary):
158+
return UnConstrainedBoundary()
159+
else:
160+
try:
161+
return DirichletBoundary(self.boundary_face.face_value/other)
162+
except Exception:
163+
return NotImplemented
164+
165+
def __rtruediv__(self, other):
166+
if isinstance(other,Boundary):
167+
return UnConstrainedBoundary()
168+
else:
169+
try:
170+
return DirichletBoundary(other/self.boundary_face.face_value)
171+
except Exception:
172+
return NotImplemented
173+
174+
def __pow__(self, other):
175+
return UnConstrainedBoundary()
176+
127177

128178
class UnConstrainedBoundary(Boundary):
179+
'''
180+
UnConstrainedBoundary is a boundary condition that the value at the boundary is calculated by the value of the neighbour cells.
181+
If you are not sure about the boundary condition, you can use UnConstrainedBoundary.
182+
The
183+
'''
129184

130185
def __init__(self) -> None:
131186
super().__init__()
132-
self.face_calculator=UnConstrainedFace()
187+
self.boundary_face=UnConstrainedFace()
133188

134189
def correct_top(self,padded_face,ori_field,delta):
135-
padded_face[...,0,:]=self.face_calculator.correct_outward_padding(ori_field[...,0,:],ori_field[...,1,:],ori_field[...,2,:])
190+
padded_face[...,0,:]=self.boundary_face.correct_outward_padding(ori_field[...,0,:],ori_field[...,1,:],ori_field[...,2,:])
136191
return padded_face
137192

138193
def correct_right(self,padded_face,ori_field,delta):
139-
padded_face[...,:,-1]=self.face_calculator.correct_outward_padding(ori_field[...,:,-1],ori_field[...,:,-2],ori_field[...,:,-3])
194+
padded_face[...,:,-1]=self.boundary_face.correct_outward_padding(ori_field[...,:,-1],ori_field[...,:,-2],ori_field[...,:,-3])
140195
return padded_face
141196

142197
def correct_bottom(self,padded_face,ori_field,delta):
143-
padded_face[...,-1,:]=self.face_calculator.correct_outward_padding(ori_field[...,-1,:],ori_field[...,-2,:],ori_field[...,-3,:])
198+
padded_face[...,-1,:]=self.boundary_face.correct_outward_padding(ori_field[...,-1,:],ori_field[...,-2,:],ori_field[...,-3,:])
144199
return padded_face
145200

146201
def correct_left(self,padded_face,ori_field,delta):
147-
padded_face[...,:,0]=self.face_calculator.correct_outward_padding(ori_field[...,:,0],ori_field[...,:,1],ori_field[...,:,2])
202+
padded_face[...,:,0]=self.boundary_face.correct_outward_padding(ori_field[...,:,0],ori_field[...,:,1],ori_field[...,:,2])
148203
return padded_face
149204

150205
# + :
@@ -153,9 +208,20 @@ def __add__(self, other):
153208
# *
154209
def __mul__(self,other):
155210
return UnConstrainedBoundary()
211+
212+
def __pow__(self, other):
213+
return UnConstrainedBoundary()
214+
215+
def __truediv__(self, other):
216+
return UnConstrainedBoundary()
156217

218+
def __rtruediv__(self, other):
219+
return UnConstrainedBoundary()
157220

158221
class PeriodicBoundary(Boundary):
222+
'''
223+
Periodic boundary conditions.
224+
'''
159225

160226
def __init__(self) -> None:
161227
super().__init__()
@@ -197,3 +263,18 @@ def __mul__(self,other):
197263
return UnConstrainedBoundary()
198264
else:
199265
return PeriodicBoundary()
266+
267+
def __pow__(self, other):
268+
return PeriodicBoundary()
269+
270+
def __truediv__(self, other):
271+
if isinstance(other,PeriodicBoundary):
272+
return PeriodicBoundary()
273+
else:
274+
return UnConstrainedBoundary()
275+
276+
def __rtruediv__(self, other):
277+
if isinstance(other,PeriodicBoundary):
278+
return PeriodicBoundary()
279+
else:
280+
return UnConstrainedBoundary()

0 commit comments

Comments
 (0)