Skip to content

Commit

Permalink
Fix loading data to fp32 (PaddlePaddle#154)
Browse files Browse the repository at this point in the history
* remove print

* add padding in create input from loss

* fix nproc typo

* fix loading fp32
  • Loading branch information
xingfeng01 authored Aug 2, 2022
1 parent 020064e commit fc236ee
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
paddle.seed(1)
np.random.seed(1)

paddle.enable_static()

# time array
time_tmp = np.linspace(0, 50, 50, endpoint=True).astype(int)
time_array = np.random.choice(time_tmp, 11)
Expand All @@ -32,19 +34,20 @@
# loading data from files
dr = loading_cfd_data.DataLoader(path='./datasets/')
# interior data
i_t, i_x, i_y = dr.loading_train_inside_domain_data(time_array, flatten=True)
i_t, i_x, i_y = dr.loading_train_inside_domain_data(
time_array, flatten=True, dtype='float32')
# boundary inlet and circle
b_inlet_u, b_inlet_v, b_inlet_t, b_inlet_x, b_inlet_y = dr.loading_boundary_data(
time_array, flatten=True)
time_array, flatten=True, dtype='float32')
# boundary outlet
b_outlet_p, b_outlet_t, b_outlet_x, b_outlet_y = dr.loading_outlet_data(
time_array, flatten=True)
time_array, flatten=True, dtype='float32')
# initial data
init_p, init_u, init_v, init_t, init_x, init_y = dr.loading_initial_data(
[1], flatten=True)
[1], flatten=True, dtype='float32')
# supervised data
sup_p, sup_u, sup_v, sup_t, sup_x, sup_y = dr.loading_supervised_data(
time_array, flatten=True)
time_array, flatten=True, dtype='float32')

inputeq = np.stack((i_t, i_x, i_y), axis=1)
inputbc1 = np.stack((b_inlet_t, b_inlet_x, b_inlet_y), axis=1)
Expand Down
66 changes: 45 additions & 21 deletions examples/cylinder/2d_unsteady_continuous/loading_cfd_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ def replicate_time_list(self, time_list, domain_shape, spatial_data):

return replicated_t, spatial_data

def loading_train_inside_domain_data(self, time_list, flatten=False):
def loading_train_inside_domain_data(self,
time_list,
flatten=False,
dtype='float32'):
# load train_domain points
# domain_train.csv, title is p,U:0,U:1,U:2,Points:0,Points:1,Points:2
filename = 'domain_train.csv'
Expand All @@ -110,13 +113,16 @@ def loading_train_inside_domain_data(self, time_list, flatten=False):
x = domain_data[:, 4].reshape((-1, 1))
y = domain_data[:, 5].reshape((-1, 1))
t, xy = self.replicate_time_list(time_list, x.shape[0], [x, y])
t = t.astype(dtype)
xy[0] = xy[0].astype(dtype)
xy[1] = xy[1].astype(dtype)
print("residual data shape:", t.shape[0])
if flatten == True:
return t.flatten(), xy[0].flatten(), xy[1].flatten()
else:
return t, xy[0], xy[1]

def loading_outlet_data(self, time_list, flatten=False):
def loading_outlet_data(self, time_list, flatten=False, dtype='float32'):
filename = 'domain_outlet.csv'
path = self.path

Expand All @@ -133,6 +139,11 @@ def loading_outlet_data(self, time_list, flatten=False):
t, pxy = self.replicate_time_list(time_list, outlet_data.shape[0],
[p, x, y])

pxy[0] = pxy[0].astype(dtype)
pxy[1] = pxy[1].astype(dtype)
pxy[2] = pxy[2].astype(dtype)
t = t.astype(dtype)

if flatten == True:
return pxy[0].flatten(), t.flatten(), pxy[1].flatten(), pxy[
2].flatten()
Expand Down Expand Up @@ -160,17 +171,27 @@ def loading_side_data(self, time_list, path):
# u, v, x, y
return self.loading_data(time_list, path, filename)

def loading_boundary_data(self, time_list, num_random=None, flatten=False):
def loading_boundary_data(self,
time_list,
num_random=None,
flatten=False,
dtype='float32'):
inlet_bc = self.loading_inlet_data(time_list, self.path)
# side_bc = self.loading_side_data(time_list, self.path)
cylinder_bc = self.loading_cylinder_data(time_list, self.path)

u = np.concatenate((inlet_bc[0], cylinder_bc[0]))
v = np.concatenate((inlet_bc[1], cylinder_bc[1]))
x = np.concatenate((inlet_bc[2], cylinder_bc[2]))
y = np.concatenate((inlet_bc[3], cylinder_bc[3]))
u = np.concatenate((inlet_bc[0], cylinder_bc[0])).astype(dtype)
v = np.concatenate((inlet_bc[1], cylinder_bc[1])).astype(dtype)
x = np.concatenate((inlet_bc[2], cylinder_bc[2])).astype(dtype)
y = np.concatenate((inlet_bc[3], cylinder_bc[3])).astype(dtype)
t, uvxy = self.replicate_time_list(time_list, u.shape[0], [u, v, x, y])

t = t.astype(dtype)
uvxy[0] = uvxy[0].astype(dtype)
uvxy[1] = uvxy[1].astype(dtype)
uvxy[2] = uvxy[2].astype(dtype)
uvxy[3] = uvxy[3].astype(dtype)

if flatten == True:
return uvxy[0].flatten(), uvxy[1].flatten(), t.flatten(), uvxy[
2].flatten(), uvxy[3].flatten()
Expand Down Expand Up @@ -200,7 +221,10 @@ def loading_data(self, time_list, path, filename, num_random=None):
print("boundary data shape:", boundary_data.shape[0])
return u, v, x, y

def loading_supervised_data(self, time_list, flatten=False):
def loading_supervised_data(self,
time_list,
flatten=False,
dtype='float32'):
path = self.path

supervised_data = None
Expand All @@ -226,20 +250,20 @@ def loading_supervised_data(self, time_list, flatten=False):

print("supervised data shape:", full_supervised_data.shape[0])
# p, u, v, t, x, y
p = full_supervised_data[:, 1].reshape((-1, 1))
u = full_supervised_data[:, 2].reshape((-1, 1))
v = full_supervised_data[:, 3].reshape((-1, 1))
t = full_supervised_data[:, 0].reshape((-1, 1))
x = full_supervised_data[:, 6].reshape((-1, 1))
y = full_supervised_data[:, 7].reshape((-1, 1))
p = full_supervised_data[:, 1].reshape((-1, 1)).astype(dtype)
u = full_supervised_data[:, 2].reshape((-1, 1)).astype(dtype)
v = full_supervised_data[:, 3].reshape((-1, 1)).astype(dtype)
t = full_supervised_data[:, 0].reshape((-1, 1)).astype(dtype)
x = full_supervised_data[:, 6].reshape((-1, 1)).astype(dtype)
y = full_supervised_data[:, 7].reshape((-1, 1)).astype(dtype)

if flatten == True:
return p.flatten(), u.flatten(), v.flatten(), t.flatten(
), x.flatten(), y.flatten()
else:
return p, u, v, t, x, y

def loading_initial_data(self, time_list, flatten=False):
def loading_initial_data(self, time_list, flatten=False, dtype='float32'):
# "p","U:0","U:1","U:2","vtkOriginalPointIds","Points:0","Points:1","Points:2"
path = self.path

Expand All @@ -254,12 +278,12 @@ def loading_initial_data(self, time_list, flatten=False):

print("initial data shape:", initial_data.shape[0])
# p, u, v, t, x, y
p = initial_t_data[:, 1].reshape((-1, 1))
u = initial_t_data[:, 2].reshape((-1, 1))
v = initial_t_data[:, 3].reshape((-1, 1))
t = initial_t_data[:, 0].reshape((-1, 1))
x = initial_t_data[:, 5].reshape((-1, 1))
y = initial_t_data[:, 6].reshape((-1, 1))
p = initial_t_data[:, 1].reshape((-1, 1)).astype(dtype)
u = initial_t_data[:, 2].reshape((-1, 1)).astype(dtype)
v = initial_t_data[:, 3].reshape((-1, 1)).astype(dtype)
t = initial_t_data[:, 0].reshape((-1, 1)).astype(dtype)
x = initial_t_data[:, 5].reshape((-1, 1)).astype(dtype)
y = initial_t_data[:, 6].reshape((-1, 1)).astype(dtype)
if flatten == True:
return p.flatten(), u.flatten(), v.flatten(), t.flatten(
), x.flatten(), y.flatten()
Expand Down

0 comments on commit fc236ee

Please sign in to comment.