Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
SWIFTsim
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SWIFT
SWIFTsim
Commits
226ff24a
Commit
226ff24a
authored
8 years ago
by
Matthieu Schaller
Browse files
Options
Downloads
Patches
Plain Diff
Also updated the Minimal hydro scheme to the new cooling way
parent
ef8184b8
No related branches found
No related tags found
1 merge request
!301
New time line
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
examples/CoolingBox/test_energy_conservation.py
+0
-116
0 additions, 116 deletions
examples/CoolingBox/test_energy_conservation.py
src/hydro/Minimal/hydro.h
+21
-69
21 additions, 69 deletions
src/hydro/Minimal/hydro.h
src/hydro/Minimal/hydro_io.h
+2
-2
2 additions, 2 deletions
src/hydro/Minimal/hydro_io.h
with
23 additions
and
187 deletions
examples/CoolingBox/test_energy_conservation.py
deleted
100644 → 0
+
0
−
116
View file @
ef8184b8
import
numpy
as
np
import
matplotlib.pyplot
as
plt
import
h5py
as
h5
import
sys
stats_filename
=
"
./energy.txt
"
snap_filename
=
"
coolingBox_000.hdf5
"
#plot_dir = "./"
n_snaps
=
41
time_end
=
4.0
dt_snap
=
0.1
#some constants in cgs units
k_b
=
1.38E-16
#boltzmann
m_p
=
1.67e-24
#proton mass
#initial conditions set in makeIC.py
rho
=
4.8e3
P
=
4.5e6
#n_H_cgs = 0.0001
gamma
=
5.
/
3.
T_init
=
1.0e5
#find the sound speed
#Read the units parameters from the snapshot
f
=
h5
.
File
(
snap_filename
,
'
r
'
)
units
=
f
[
"
InternalCodeUnits
"
]
unit_mass
=
units
.
attrs
[
"
Unit mass in cgs (U_M)
"
]
unit_length
=
units
.
attrs
[
"
Unit length in cgs (U_L)
"
]
unit_time
=
units
.
attrs
[
"
Unit time in cgs (U_t)
"
]
parameters
=
f
[
"
Parameters
"
]
cooling_lambda
=
float
(
parameters
.
attrs
[
"
LambdaCooling:lambda_cgs
"
])
min_T
=
float
(
parameters
.
attrs
[
"
LambdaCooling:minimum_temperature
"
])
mu
=
float
(
parameters
.
attrs
[
"
LambdaCooling:mean_molecular_weight
"
])
X_H
=
float
(
parameters
.
attrs
[
"
LambdaCooling:hydrogen_mass_abundance
"
])
#get number of particles
header
=
f
[
"
Header
"
]
n_particles
=
header
.
attrs
[
"
NumPart_ThisFile
"
][
0
]
#read energy and time arrays
array
=
np
.
genfromtxt
(
stats_filename
,
skip_header
=
1
)
time
=
array
[:,
0
]
total_energy
=
array
[:,
2
]
total_mass
=
array
[:,
1
]
time
=
time
[
1
:]
total_energy
=
total_energy
[
1
:]
total_mass
=
total_mass
[
1
:]
#conversions to cgs
rho_cgs
=
rho
*
unit_mass
/
(
unit_length
)
**
3
time_cgs
=
time
*
unit_time
u_init_cgs
=
total_energy
[
0
]
/
(
total_mass
[
0
])
*
unit_length
**
2
/
(
unit_time
)
**
2
n_H_cgs
=
X_H
*
rho_cgs
/
m_p
#find the sound speed in cgs
c_s
=
np
.
sqrt
((
gamma
-
1.
)
*
k_b
*
T_init
/
(
mu
*
m_p
))
#assume box size is unit length
sound_crossing_time
=
unit_length
/
c_s
print
"
Sound speed = %g cm/s
"
%
c_s
print
"
Sound crossing time = %g s
"
%
sound_crossing_time
#find the energy floor
u_floor_cgs
=
k_b
*
min_T
/
(
mu
*
m_p
*
(
gamma
-
1.
))
#find analytic solution
analytic_time_cgs
=
np
.
linspace
(
time_cgs
[
0
],
time_cgs
[
-
1
],
1000
)
du_dt_cgs
=
-
cooling_lambda
*
n_H_cgs
**
2
/
rho_cgs
u_analytic
=
du_dt_cgs
*
(
analytic_time_cgs
-
analytic_time_cgs
[
0
])
+
u_init_cgs
cooling_time
=
u_init_cgs
/
(
-
du_dt_cgs
)
#put time in units of sound crossing time
time
=
time_cgs
/
sound_crossing_time
analytic_time
=
analytic_time_cgs
/
sound_crossing_time
#rescale energy to initial energy
total_energy
/=
total_energy
[
0
]
u_analytic
/=
u_init_cgs
u_floor_cgs
/=
u_init_cgs
# plot_title = r"$\Lambda \, = \, %1.1g \mathrm{erg}\mathrm{cm^3}\mathrm{s^{-1}} \, \, T_{init} = %1.1g\mathrm{K} \, \, T_{floor} = %1.1g\mathrm{K} \, \, n_H = %1.1g\mathrm{cm^{-3}}$" %(cooling_lambda,T_init,T_floor,n_H)
# plot_filename = "energy_plot_creasey_no_cooling_T_init_1p0e5_n_H_0p1.png"
#analytic_solution = np.zeros(n_snaps-1)
for
i
in
range
(
u_analytic
.
size
):
if
u_analytic
[
i
]
<
u_floor_cgs
:
u_analytic
[
i
]
=
u_floor_cgs
plt
.
plot
(
time
-
time
[
0
],
total_energy
,
'
k
'
,
label
=
"
Numerical solution from energy.txt
"
)
plt
.
plot
(
analytic_time
-
analytic_time
[
0
],
u_analytic
,
'
r
'
,
lw
=
2.0
,
label
=
"
Analytic Solution
"
)
#now get energies from the snapshots
snapshot_time
=
np
.
linspace
(
0
,
time_end
,
num
=
n_snaps
)
snapshot_time
=
snapshot_time
[
1
:]
snapshot_time_cgs
=
snapshot_time
*
unit_time
snapshot_time
=
snapshot_time_cgs
/
sound_crossing_time
snapshot_time
-=
snapshot_time
[
0
]
snapshot_energy
=
np
.
zeros
(
n_snaps
)
for
i
in
range
(
0
,
n_snaps
):
snap_filename
=
"
coolingBox_%03d.hdf5
"
%
i
f
=
h5
.
File
(
snap_filename
,
'
r
'
)
snapshot_internal_energy_array
=
np
.
array
(
f
[
"
PartType0/InternalEnergy
"
])
total_internal_energy
=
np
.
sum
(
snapshot_internal_energy_array
)
velocity_array
=
np
.
array
(
f
[
"
PartType0/Velocities
"
])
total_kinetic_energy
=
0.5
*
np
.
sum
(
velocity_array
**
2
)
snapshot_energy
[
i
]
=
total_internal_energy
+
total_kinetic_energy
snapshot_energy
/=
snapshot_energy
[
0
]
snapshot_energy
=
snapshot_energy
[
1
:]
plt
.
plot
(
snapshot_time
,
snapshot_energy
,
'
bd
'
,
label
=
"
Numerical solution from snapshots
"
)
#plt.title(r"$n_H = %1.1e \, \mathrm{cm}^{-3}$" %n_H_cgs)
plt
.
xlabel
(
"
Time (sound crossing time)
"
)
plt
.
ylabel
(
"
Energy/Initial energy
"
)
plt
.
ylim
(
0.99
,
1.01
)
#plt.xlim(0,min(10,time[-1]))
plt
.
legend
(
loc
=
"
upper right
"
)
if
(
int
(
sys
.
argv
[
1
])
==
0
):
plt
.
show
()
else
:
plt
.
savefig
(
full_plot_filename
,
format
=
"
png
"
)
plt
.
close
()
This diff is collapsed.
Click to expand it.
src/hydro/Minimal/hydro.h
+
21
−
69
View file @
226ff24a
...
@@ -49,26 +49,22 @@
...
@@ -49,26 +49,22 @@
* energy from the thermodynamic variable.
* energy from the thermodynamic variable.
*
*
* @param p The particle of interest
* @param p The particle of interest
* @param dt Time since the last kick
*/
*/
__attribute__
((
always_inline
))
INLINE
static
float
hydro_get_internal_energy
(
__attribute__
((
always_inline
))
INLINE
static
float
hydro_get_internal_energy
(
const
struct
part
*
restrict
p
,
float
dt
)
{
const
struct
part
*
restrict
p
)
{
return
p
->
u
+
p
->
u_dt
*
dt
;
return
p
->
u
;
}
}
/**
/**
* @brief Returns the pressure of a particle
* @brief Returns the pressure of a particle
*
*
* @param p The particle of interest
* @param p The particle of interest
* @param dt Time since the last kick
*/
*/
__attribute__
((
always_inline
))
INLINE
static
float
hydro_get_pressure
(
__attribute__
((
always_inline
))
INLINE
static
float
hydro_get_pressure
(
const
struct
part
*
restrict
p
,
float
dt
)
{
const
struct
part
*
restrict
p
)
{
const
float
u
=
p
->
u
+
p
->
u_dt
*
dt
;
return
gas_pressure_from_internal_energy
(
p
->
rho
,
u
);
return
gas_pressure_from_internal_energy
(
p
->
rho
,
p
->
u
);
}
}
/**
/**
...
@@ -79,24 +75,20 @@ __attribute__((always_inline)) INLINE static float hydro_get_pressure(
...
@@ -79,24 +75,20 @@ __attribute__((always_inline)) INLINE static float hydro_get_pressure(
* the thermodynamic variable.
* the thermodynamic variable.
*
*
* @param p The particle of interest
* @param p The particle of interest
* @param dt Time since the last kick
*/
*/
__attribute__
((
always_inline
))
INLINE
static
float
hydro_get_entropy
(
__attribute__
((
always_inline
))
INLINE
static
float
hydro_get_entropy
(
const
struct
part
*
restrict
p
,
float
dt
)
{
const
struct
part
*
restrict
p
)
{
const
float
u
=
p
->
u
+
p
->
u_dt
*
dt
;
return
gas_entropy_from_internal_energy
(
p
->
rho
,
u
);
return
gas_entropy_from_internal_energy
(
p
->
rho
,
p
->
u
);
}
}
/**
/**
* @brief Returns the sound speed of a particle
* @brief Returns the sound speed of a particle
*
*
* @param p The particle of interest
* @param p The particle of interest
* @param dt Time since the last kick
*/
*/
__attribute__
((
always_inline
))
INLINE
static
float
hydro_get_soundspeed
(
__attribute__
((
always_inline
))
INLINE
static
float
hydro_get_soundspeed
(
const
struct
part
*
restrict
p
,
float
dt
)
{
const
struct
part
*
restrict
p
)
{
return
p
->
force
.
soundspeed
;
return
p
->
force
.
soundspeed
;
}
}
...
@@ -124,68 +116,31 @@ __attribute__((always_inline)) INLINE static float hydro_get_mass(
...
@@ -124,68 +116,31 @@ __attribute__((always_inline)) INLINE static float hydro_get_mass(
}
}
/**
/**
* @brief Modifies the thermal state of a particle to the imposed internal
* @brief Returns the time derivative of internal energy of a particle
* energy
*
*
* This overwrites the current state of the particle but does *not* change its
* We assume a constant density.
* time-derivatives. Internal energy, pressure, sound-speed and signal velocity
* will be updated.
*
*
* @param p The particle
* @param p The particle of interest
* @param u The new internal energy
*/
*/
__attribute__
((
always_inline
))
INLINE
static
void
hydro_set_internal_energy
(
__attribute__
((
always_inline
))
INLINE
static
float
hydro_get_internal_energy_dt
(
struct
part
*
restrict
p
,
float
u
)
{
const
struct
part
*
restrict
p
)
{
p
->
u
=
u
;
/* Compute the new pressure */
const
float
pressure
=
gas_pressure_from_internal_energy
(
p
->
rho
,
p
->
u
);
/* Compute the new sound speed */
const
float
soundspeed
=
gas_soundspeed_from_internal_energy
(
p
->
rho
,
p
->
u
);
/* Update the signal velocity */
const
float
v_sig_old
=
p
->
force
.
v_sig
;
const
float
v_sig_new
=
p
->
force
.
v_sig
-
p
->
force
.
soundspeed
+
soundspeed
;
const
float
v_sig
=
max
(
v_sig_old
,
v_sig_new
);
p
->
force
.
soundspeed
=
soundspeed
;
return
p
->
u_dt
;
p
->
force
.
pressure
=
pressure
;
p
->
force
.
v_sig
=
v_sig
;
}
}
/**
/**
* @brief
Modifies the thermal state of a particle to the imposed entropy
* @brief
Returns the time derivative of internal energy of a particle
*
*
* This overwrites the current state of the particle but does *not* change its
* We assume a constant density.
* time-derivatives. Internal energy, pressure, sound-speed and signal velocity
* will be updated.
*
*
* @param p The particle
* @param p The particle
of interest.
* @param
S
The new
entropy
* @param
du_dt
The new
time derivative of the internal energy.
*/
*/
__attribute__
((
always_inline
))
INLINE
static
void
hydro_set_entropy
(
__attribute__
((
always_inline
))
INLINE
static
void
hydro_set_internal_energy_dt
(
struct
part
*
restrict
p
,
float
S
)
{
struct
part
*
restrict
p
,
float
du_dt
)
{
p
->
u
=
gas_internal_energy_from_entropy
(
p
->
rho
,
S
);
/* Compute the pressure */
p
->
u_dt
=
du_dt
;
const
float
pressure
=
gas_pressure_from_internal_energy
(
p
->
rho
,
p
->
u
);
/* Compute the new sound speed */
const
float
soundspeed
=
gas_soundspeed_from_internal_energy
(
p
->
rho
,
p
->
u
);
/* Update the signal velocity */
const
float
v_sig_old
=
p
->
force
.
v_sig
;
const
float
v_sig_new
=
p
->
force
.
v_sig
-
p
->
force
.
soundspeed
+
soundspeed
;
const
float
v_sig
=
max
(
v_sig_old
,
v_sig_new
);
p
->
force
.
soundspeed
=
soundspeed
;
p
->
force
.
pressure
=
pressure
;
p
->
force
.
v_sig
=
v_sig
;
}
}
/**
/**
* @brief Computes the hydro time-step of a given particle
* @brief Computes the hydro time-step of a given particle
*
*
...
@@ -406,10 +361,7 @@ __attribute__((always_inline)) INLINE static void hydro_kick_extra(
...
@@ -406,10 +361,7 @@ __attribute__((always_inline)) INLINE static void hydro_kick_extra(
/* Do not decrease the energy by more than a factor of 2*/
/* Do not decrease the energy by more than a factor of 2*/
const
float
u_change
=
p
->
u_dt
*
dt
;
const
float
u_change
=
p
->
u_dt
*
dt
;
if
(
u_change
>
-
0
.
5
f
*
xp
->
u_full
)
xp
->
u_full
=
max
(
xp
->
u_full
+
u_change
,
0
.
5
f
*
xp
->
u_full
);
xp
->
u_full
+=
u_change
;
else
xp
->
u_full
*=
0
.
5
f
;
/* Compute the pressure */
/* Compute the pressure */
const
float
pressure
=
gas_pressure_from_internal_energy
(
p
->
rho
,
xp
->
u_full
);
const
float
pressure
=
gas_pressure_from_internal_energy
(
p
->
rho
,
xp
->
u_full
);
...
...
This diff is collapsed.
Click to expand it.
src/hydro/Minimal/hydro_io.h
+
2
−
2
View file @
226ff24a
...
@@ -71,12 +71,12 @@ void hydro_read_particles(struct part* parts, struct io_props* list,
...
@@ -71,12 +71,12 @@ void hydro_read_particles(struct part* parts, struct io_props* list,
float
convert_S
(
struct
engine
*
e
,
struct
part
*
p
)
{
float
convert_S
(
struct
engine
*
e
,
struct
part
*
p
)
{
return
hydro_get_entropy
(
p
,
0
);
return
hydro_get_entropy
(
p
);
}
}
float
convert_P
(
struct
engine
*
e
,
struct
part
*
p
)
{
float
convert_P
(
struct
engine
*
e
,
struct
part
*
p
)
{
return
hydro_get_pressure
(
p
,
0
);
return
hydro_get_pressure
(
p
);
}
}
/**
/**
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment