
    Zh                         d dl mZ d dl mZ d dl mZ d dl mZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d d	lZ G d de      Zy	)    )Frame)Instruction)
Integrator)schemes)Field)Group)IntVar)colorizeN)std)
hdf5writer)Boundary)SimpleNamespacec                   ~     e Zd ZdZdZ  fdZ fdZed        Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd ZddZ xZS )
Simulationa  The main simulation class for running dust coagulation simulations.

    `dustpy.Simulation`` is a child of ``simframe.Frame``.

    For setting simple initial conditions use ``Simulation.ini``,
    For making the simulation grids use ``Simulation.makegrids()``,
    For initialization use ``Simulation.initialize()``,
    For running simulations use ``Simulation.run()``.

    Please have a look at the documentation of ``simframe`` for further details.DustPyc                 *   t        |   d?i | t        d?i t        d?i ddddddddd	d
	t        d?i ddt        j                  z  dt        j
                  z  ddt        j                  z  dt        d?i dddddt        j                  z  dt        j                  z  dt        d?i dt        j                  z  dt        j                  z  ddd| _        t        | d      | _
        d| j                  _        t        | d      | j                  _        d| j                  j                  _        d| j                  j                  _        dd g| j                  j                  _        t        | d!      | j                  _        d| j                  j                   _        d| j                  j                   _        t        | d"      | j                  _        d| j                  j&                  _        d| j                  j&                  _        d| j                  j&                  _        d| j                  j&                  _        d| j                  j&                  _        d| j                  j&                  _        d| j                  j&                  _        d| j                  _        t        | d#      | j                  _        d| j                  j6                  _        d| j                  j6                  _        d| j                  j6                  _        g d$| j                  j6                  _        d| j                  _        t        | d%      | j                  _        d| j                  j>                  _         d| j                  j>                  _!        d| j                  j>                  _"        g d&| j                  j>                  _        d| j                  _#        d| j                  _$        d| j                  _%        d| j                  _&        d| j                  _'        t        | d'      | j                  _(        d| j                  jP                  _)        d| j                  jP                  _        d(d)g| j                  jP                  _        t        | d*      | j                  _*        d| j                  jT                  _+        d| j                  jT                  _,        d| j                  jT                  _-        d| j                  jT                  _"        d+d,g| j                  jT                  _        d| j                  _.        d| j                  _/        d| j                  _0        t        | d-      | j                  _1        d| j                  jb                  _)        t        | d.      | j                  jb                  _2        d| j                  jb                  jd                  _3        d| j                  jb                  jd                  _4        d| j                  jb                  jd                  _        d| j                  jb                  jd                  _"        d| j                  jb                  jd                  _        d| j                  jb                  jd                  _        g d/| j                  jb                  jd                  _        d| j                  jb                  _5        d| j                  jb                  _        g d0| j                  jb                  _        g d1| j                  _        t        | d2      | _6        d| jl                  _7        t        | d!      | jl                  _        d| jl                  j                   _        d| jl                  j                   _        d| jl                  _8        d| jl                  _9        d| jl                  _        d| jl                  _:        d| jl                  _;        d| jl                  _<        d| jl                  _=        d| jl                  _>        d| jl                  _?        d| jl                  _&        t        | d3      | jl                  _*        d| jl                  jT                  _,        d| jl                  jT                  _-        d| jl                  jT                  _"        d+d,g| jl                  jT                  _        d| jl                  _.        d| jl                  _/        d| jl                  _@        t        | d4      | jl                  _A        d| jl                  j                  _B        d| jl                  j                  _1        d5d6g| jl                  j                  _        t        | d-      | jl                  _1        d| jl                  jb                  _        d| jl                  jb                  _C        d7d8g| jl                  jb                  _        g d9| jl                  _        t        | d:      | _D        d| j                  _E        d| j                  _F        d| j                  _G        d| j                  _        d| j                  _H        d| j                  _I        d| j                  _J        d;g| j                  _        t        | d<      | _K        d| j                  _L        d| j                  _M        d| j                  _N        d| j                  _@        g d=| j                  _        g d>| _        d| _O        y)@zMain simulation class.g-C6?F      $@g{Gz?g            ?gUUUUUUgQ?g      Y@)	aIniMaxallowDriftingParticleserosionMassRatiod2gRatiodistExpexcavatedMassfragmentDistribution
rhoMonomervFraggMbP?g?gffffff@g      g      N@)alphaMdiskmuSigmaExpSigmaRc   g-q=     j@d        @@)NmbpdmminmmaxNrrminrmaxg       @g     @)MRT)dustgasgridstarzDust quantitiesdescriptionNzBackreaction coefficientsABzBoundary conditionszCoagulation quantitieszMixing parameters)radturbvertFluxes)advdifftotProbabilitiesfragstickSourcesextr>   
VelocitieszRelative velocities)azibrownr8   r9   r:   r>   r@   driftmaxreldeltarhosfillaStHrhobackreactionvDepskernelpSzGas quantitieszSource termszTorque parametersLambdarS   viscr8   )r    r/   r   csHpnurQ   nmfpPetatorquerX   zGrid quantitiesOmegaKzStellar quantities)r-   r.   r/   L)r3   r2   r1   r0    )Psuper__init__r   cM_sunm_pauR_sun_inir   r0   rN   rR   r6   r7   updaterboundaryinneroutercoagulationrA   	stick_indrU   lf_indrm_indphirT   rK   r8   r9   r:   Fir<   r=   r>   rM   rP   rV   rQ   rL   rW   r@   rX   coagrC   hydSigma
SigmaFloorrO   rS   rI   rE   rF   rH   r1   r   r[   ra   r\   r_   r    r^   r]   r`   r/   rb   rY   rZ   r2   r*   rriNmmrc   r3   rd   r-   r.   t)selfkwargs	__class__s     `/mounts/lovelace/software/anaconda3/envs/py312/lib/python3.12/site-packages/dustpy/simulation.pyrg   zSimulation.__init__#   s    	"6" $  & 
0AV\aVYNSMQSUZ_PTKO	BD 
0A /> /@%JNqww,GJ155yMPLOPQPTPTH	AC /@ 0? 0A1JPJNHKJLQTT'JOPQPTPT*BD 0A 0? 0Ar!''zGI!''zGLBD 0A5')  &	J $,=>			!&9";		#'		 #'		 *-s		&"45JK		#'		 #'		  %6!8		&*		#*.		'"&		$(		!'+		$'+		$$(		!		2EF		"		#		#		"9				Tx8				 				5												Do>				 		%w/		Di8										$en				#				Dl;				2GH		"		 $		"		"		#		#		#:		#				9		Y		 +;<!$4IJ"&"&

4^<






#U^

"
2EF!% #+S/4\:




$e_

J $,=>															%J		 $,@A									0		 7    c                 `   | j                   dkD  rd}|dj                  | j                        z  }|dz  }|dj                  d      z  }|dj                  d      z  }|d	j                  d
      z  }|dz  }|t        dd      z  }t	        |       | j                          t        |           y)z#This functions runs the simulation.r    z
DustPy v{}
z
Documentation: {}z"https://stammler.github.io/dustpy/z
PyPI:          {}z https://pypi.org/project/dustpy/z
GitHub:        {}z#https://github.com/stammler/dustpy/z)
Please cite Stammler & Birnstiel (2022).blueN)	verbosityformat__version__r
   printcheckmassconservationrf   run)r   msgr   s     r   r   zSimulation.run   s     >>AC>(()9)9::C4KC(//46 6C(//24 4C(//57 7C4KC8H&QQC#J""$r   c                     | j                   S )z1Parameter set for setting the initial conditions.)rm   r   s    r   inizSimulation.ini   s     yyr   c                 D    | j                          | j                          y)a  Function creates radial and mass grid.

        Notes
        -----
        The grids are set up with the parameters given in ``Simulation.ini``.
        If you want to have a custom radial grid you have to set the array of grid cell interfaces ``Simulation.grid.ri``,
        before calling ``Simulation.makegrids()``.
        You cannot have a customized mass grid, because the coagulation alorithm strictly needs a logarithmic grid. Always
        set your mass grid parameters with ``Simulation.ini``.N)_makemassgrid_makeradialgridr   s    r   	makegridszSimulation.makegrids   s     	r   c                    t        j                  | j                  j                  j                        }t        j                  | j                  j                  j
                        }t        j                  ||z
        }t        || j                  j                  j                  z        dz   }t        j                  t        j                  | j                  j                  j                        t        j                  | j                  j                  j
                        |d      }t        | |dd      | j                  _        t        | |dd      | j                  _        y)	zFunction sets the radial mass grid. If ``Simulation.grid.rint`` is not given it uses the parameters set in
        ``Simulation.ini``.   r   numbasez# of mass binsTr5   constantzMass grid [g]N)nplog10r   r2   r(   r)   ceilintr'   logspacer   r~   r   )r   logmminlogmmaxdecadesr~   r   s         r   r   zSimulation._makemassgrid   s     ((488==--.((488==--.'''G+,488==.../!3KK!3!34bhhHHMM7 %'c3""2TC		!4A		r   c                 J   | j                   j                  t        j                  t        j                  | j
                  j                   j                        t        j                  | j
                  j                   j                        | j
                  j                   j                  dz   d      }| j
                  j                   j                  }n(| j                   j                  }|j                  d   dz
  }d|dd |dd z   z  }t        j                  |dd dz  |dd dz  z
  z  }t        | |d	d
      | j                   _        t        | |dd
      | j                   _        t        | |dd
      | j                   _        t        | |dd
      | j                   _        y)zKFunction sets the mass grid using the parameters set in ``Simulation.ini``.Nr   r   r   r   g      ?   z# of radial grid cellsTr   zRadial grid cell centers [cm]z Radial grid cell interfaces [cm]u   Radial grid annulus area [cm²])r2   r}   r   r   r   r   r+   r,   r*   shapepir   r|   r6   )r   r}   r*   r|   r6   s        r   r   zSimulation._makeradialgrid   sK   99<<RXXdhhmm&8&89288""<$)-)9)9!);#GB!!BB!qBCR2ab6!"EE2ab619r#2wz)*"":TK		!!@4Q		""DtU		!!BTS		r   c                 
   | j                   j                  j                  t        d      | j                   j                  j                  t        d      | j                   j                  j
                  t        d      | j                   j                  j                  t        d      | j                   j                  j                  t        d      | j                   j                  j                  t        d      | j                   j                  j                  t        d      | j                  j                  t        d	      | j                  d
kD  rd}d}|t        dd      z  }t        |       t        dd      }t        |       t        j                   j#                  | j                   j                  j                  | j                   j                  j                  | j                  j                        \  }}}||f}d}||k  rd}dj%                  |      }dj%                  t        ||            }|dz  }|dj%                  |d
   | j                  j                  |d
            z  }|dj%                  |d   | j                  j                  |d            z  }t        |      }t        |       t        dd      }t        |       | j                   j                  j
                  }	| j                   j                  j                  }
| j                   j                  j                  }| j                   j                  j                  }| j                  j                  }| j                   j                  j                  }t        j                   j'                  |	|||      \  }}}||f}d}||k  rd}dj%                  |      }dj%                  t        ||            }|dz  }|dj%                  |d
   | j                  j                  |d
            z  }|dj%                  |d   | j                  j                  |d            z  }t        |      }t        |       t        dd      }t        |       t        j                   j)                  |	|
||||      \  }}}||f}d}||k  rd}dj%                  |      }dj%                  t        ||            }|dz  }|dj%                  |d
   | j                  j                  |d
            z  }|dj%                  |d   | j                  j                  |d            z  }t        |      }t        |       yy)zQFunction checks for mass conservation and prints the maximum relative mass error.Nz/'Simulation.dust.coagulation.stick' is not set.z3'Simulation.dust.coagulation.stick_ind' is not set.z+'Simulation.dust.coagulation.A' is not set.z-'Simulation.dust.coagulation.eps' is not set.z0'Simulation.dust.coagulation.lf_ind' is not set.z0'Simulation.dust.coagulation.rm_ind' is not set.z-'Simulation.dust.coagulation.phi' is not set.z'sim.grid.m' is not set.r   gvIh%<=r   z"Checking for mass conservation...
yellow)colorz    - Sticking:redgreenz{:9.2e}z        max. rel. error: {:}
z        for particle collision
z(            m[{:d}] = {:9.2e} g    with
z            m[{:d}] = {:9.2e} gr   z    - Full fragmentation:z    - Erosion:z             m[{:d}] = {:9.2e} g
)r0   rr   rA   RuntimeErrorrs   r6   rU   rt   ru   rv   r2   r   r   r
   r   r   dust_f check_mass_conservation_stickingr   *check_mass_conservation_full_fragmentationcheck_mass_conservation_erosion)r   erraccr   errmaxijtupr   errorr6   rU   klfkrmr   rv   s                  r   r   z Simulation.checkmassconservation  s    99  &&.AC C99  **2EG G99  ""*=? ?99  $$,?A A99  ''/BD D99  ''/BD D99  $$,?A A99;;9::>>A F C8A"*, ,C#J,H=C#J::FF		%%++TYY-B-B-L-Ldiikk[LFAqa&CE$$V,E299e,.C55C>EEA		CF+- -C4;;A		CF+- -C3-C#J 6hGC#J		%%''A))''++C))''..C))''..C		A))''++C::PP33 LFAqa&CE$$V,E299e,.C55C>EEA		CF+- -C4;;A		CF+- -C3-C#J +8<C#J::EE3S!S*LFAqa&CE$$V,E299e,.C55C>EEA		CF+- -C6==A		CF+- -C3-C#JS r   c                    t        | j                  j                  t              r$t        | j                  j                  t              s| j                          | j                  t        | dd      | _        d| j                  _        t        j                  j                  | j                  _        t        j                  | j                  t        j                  ddd	      t         j"                  z  g      | j                  _        | j'                          | j)                          | j+                          | j-                          | j.                  @t1        t        j2                  j4                  | j2                  j6                  d
| j2                  j8                  id      t1        t        j:                  j4                  | j:                  j6                  | j:                  j<                  | j:                  j>                  j@                  dd      g}tC        | j                  d      | _        || j.                  _"        t        j                  jF                  | j.                  _$        t        j                  jJ                  | j.                  _&        | jN                  tQ               | _'        | j.                  jS                          | jU                          y)zFunction initializes the simulation frame.

        Function sets all fields that are None with a standard value.
        If the grids are not set, it will call ``Simulation.makegrids()`` first.N        zTime [s]r4   皙?r&   r$      )r   rhs&Dust: implicit 1st-order direct solver
controllerr5   )ro   Sextz%Gas: implicit 1st-order direct solverzDefault integrator)+
isinstancer2   r~   r   r*   r   r   r	   cflr   simdtrn   r   hstack	geomspacerh   year	snapshots_initializestar_initializegrid_initializegas_initializedust
integratorr   r0   impl_1_directrz   _rhsr1   ro   rX   rC   r   instructionsprepare_implicit_dust
preparatorfinalize_implicit_dust	finalizerwriterr   	_finalizeupdate)r   r   s     r   
initializezSimulation.initialize{  s   
 $)),,.ju6UNN 66>D"*=DFDFFJ WWZZDFFN!yydDb9!&&@A DFF
 	 	 	 	 ??"CHH22 IIOO(-tyy~~ (*(P	 CGG11 HHNN,0HH,=,=(,

( )PL  )$8:DO+7DOO(),)F)FDOO&(+(F(FDOO% ;;$,DK 	!!#r   c           	      /   t        | j                  j                        }t        | j                  j                        t        | j                  j                        f}t        | j                  j                  | j                  j                  z        }t        | j                  j                        dz   t        | j                  j                        f}t        | j                  j                        t        | j                  j                        t        | j                  j                        f}| j                  j
                  ct        | t        j                  |      d      | j                  _        t        j                  j
                  | j                  j
                  _
        t        j                  j                  |       \  }}}}	}
}}| j                  j                  j                  (t        | |dd      | j                  j                  _        | j                  j                  j                  (t        | |	dd      | j                  j                  _        | j                  j                  j                  (t        | |
d	d      | j                  j                  _        | j                  j                  j                   (t        | |d
d      | j                  j                  _        | j                  j                  j"                  (t        | |dd      | j                  j                  _        | j                  j                  j$                  (t        | |dd      | j                  j                  _        | j                  j                  j&                  (t        | |dd      | j                  j                  _        | j                  j(                  ct        | t        j                  |      d      | j                  _        t        j                  j(                  | j                  j(                  _
        | j                  j*                  j,                  ]| j.                  j0                  j2                  t        j4                  |      z  }t        | |d      | j                  j*                  _        | j                  j*                  j6                  ]| j.                  j0                  j2                  t        j4                  |      z  }t        | |d      | j                  j*                  _        | j                  j*                  j8                  ]| j.                  j0                  j2                  t        j4                  |      z  }t        | |d      | j                  j*                  _        | j                  j                  ct        | t        j                  |      d      | j                  _        t        j                  j                  | j                  j                  _
        | j                  j:                  j<                  wt        | t        j                  |      d      | j                  j:                  _        t        j                  j>                  | j                  j:                  j<                  _
        | j                  j:                  j@                  wt        | t        j                  |      d      | j                  j:                  _         t        j                  jB                  | j                  j:                  j@                  _
        | j                  j:                  jD                  wt        | t        j                  |      d      | j                  j:                  _"        t        j                  jF                  | j                  j:                  jD                  _
        | j                  jH                  0t        | t        j4                  |      d      | j                  _$        | j                  jJ                  ct        | t        j                  |      d      | j                  _%        t        j                  jJ                  | j                  jJ                  _
        | j                  jL                  ct        | t        j                  |      d      | j                  _&        t        j                  jL                  | j                  jL                  _
        | j                  jN                  ct        | t        j                  |      d      | j                  _'        t        j                  jP                  | j                  jN                  _
        | j                  jR                  S| j.                  j                  jT                  t        j4                  |      z  }t        | |d      | j                  _)        | j                  jV                  jX                  wt        | t        j                  |      d      | j                  jV                  _,        t        j                  jZ                  | j                  jV                  jX                  _
        | j                  jV                  j$                  wt        | t        j                  |      d      | j                  jV                  _        t        j                  j\                  | j                  jV                  j$                  _
        | j                  j^                  j`                  wt        | t        j                  |      d      | j                  j^                  _0        t        j                  jb                  | j                  j^                  j`                  _
        | j                  j^                  jd                  :t        | t        j                  |      d      | j                  j^                  _2        | j                  j^                  jf                  wt        | t        j                  |      d      | j                  j^                  _3        t        j                  jh                  | j                  j^                  jf                  _
        | j                  j^                  jD                  wt        | t        j                  |      d       | j                  j^                  _"        t        j                  jj                  | j                  j^                  jD                  _
        | j                  jl                  ct        | t        j                  |      d!      | j                  _6        t        j                  jn                  | j                  jl                  _
        | j                  jp                  jX                  ]| j.                  j                  jr                  t        j4                  |      z  }t        | |d"      | j                  jp                  _,        | j                  jp                  jt                  jv                  t        | t        j                  |      d#      | j                  jp                  jt                  _;        t        j                  jx                  | j                  jp                  jt                  jv                  _
        | j                  jp                  jt                  jz                  t        | t        j                  |      d$      | j                  jp                  jt                  _=        t        j                  j|                  | j                  jp                  jt                  jz                  _
        | j                  jp                  jt                  j,                  t        | t        j                  |      d%      | j                  jp                  jt                  _        t        j                  j~                  | j                  jp                  jt                  j,                  _
        | j                  jp                  jt                  j6                  t        | t        j                  |      d&      | j                  jp                  jt                  _        t        j                  j                  | j                  jp                  jt                  j6                  _
        | j                  jp                  jt                  j8                  t        | t        j                  |      d'      | j                  jp                  jt                  _        t        j                  j                  | j                  jp                  jt                  j8                  _
        | j                  jp                  jt                  jD                  t        | t        j                  |      d(      | j                  jp                  jt                  _"        t        j                  j                  | j                  jp                  jt                  jD                  _
        | j                  jp                  j                  wt        | t        j                  |      d)      | j                  jp                  _C        t        j                  j                  | j                  jp                  j                  _
        | j                  jp                  j,                  wt        | t        j                  |      d*      | j                  jp                  _        t        j                  j                  | j                  jp                  j,                  _
        	 | j                  j                          | j                  j                  ?d+t        j                  j                  |       z  }t        | |d,      | j                  _G        | j                  j                  t        j                  j                  |       }t        j                  || j                  j                  k  d+| j                  j                  z  |      }t        | |d-      | j                  _H        t        j                  j                  | j                  j                  _L        t        j                  j                  | j                  j                  _N        | j                  j                          t        | | j                  j                  d.d/      | j                  _O        t        | t        j                  |      d0      | j                  _P        | j                  j                  j                  dt        | j                  j                  | j                  j                  | j                  j                  d12      | j                  j                  _R        | j                  j                  j                  t        | j                  j                  ddd3   | j                  j                  ddd3   | j                  j                  ddd3   d4d+| j                  j                  d3   z  5      | j                  j                  _V        | j                  j                  j                  j                          | j                  j                  j                  j                          t        j                  j                  |        | j                  j                  | j                  j                  d6<   y#  Y xY w)7z&Function to initialize dust quantitiesr   NzParticle size [cm]r4   zFragment normalization factorsTr   zRemnant mass distributionzIndex of largest fragmentzSmaller index of remnantzFragment distributionzSticking matrixz$Non-zero elements of sticking matrixu   Diffusivity [cm²/s]zRadial mixing parameterzTurbulent mixing parameterzVertical mixing parameterzDust-to-gas ratiozAdvective flux [g/cm/s]zDiffusive flux [g/cm/s]zTotal flux [g/cm/s]zFilling factorzScale heights [cm]u   Collision kernel [cm²/s]u+   Midplane mass density per mass bin [g/cm³]u   Solid state density [g/cm³]zFragmentation probabilityzSticking probabilityu   Coagulation sources [g/cm²/s]   External sources [g/cm²/s]   Hydrodynamic sources [g/cm²/s]u   Tot sources [g/cm²/s]zStokes numberzFragmentation velocity [cm/s]z"Relative azimuthal velocity [cm/s]z(Relative Brownian motion velocity [cm/s]zRelative radial velocity [cm/s]z"Relative turbulent velocity [cm/s]z*Relative vertical settling velocity [cm/s]zTotal relative velocity [cm/s]zMaximum drift velocity [cm/s]Radial velocity [cm/s]r   '   Floor value of surface density [g/cm²]u%   Surface density per mass bin [g/cm²]*   Previous value of surface density [g/cm²]r5   copy+   Right-hand side of matrix equation [g/cm²]
const_grad)	conditionr   val)r   value.)Yr   r2   r*   r~   r0   rN   r   r   zerosr   rn   coagulation_parametersrr   r6   rU   rt   ru   rv   rA   rs   rT   rK   r8   r   r1   r   onesr9   r:   rw   r<   F_advr=   F_diffr>   F_totrM   rP   rV   rQ   rho_midplanerL   r   rW   r@   p_fragp_stickrX   rx   S_coagrC   ry   S_hydS_totrO   St_Epstein_StokesIrS   r   rI   rE   vrel_azimuthal_driftrF   vrel_brownian_motionvrel_radial_driftvrel_turbulent_motionvrel_vertical_settlingvrel_totrH   	vdriftmaxvradr   r{   rz   MRN_distributionwhereSigma_derivdifferentiatorjacobianjacobinator	_SigmaOldr   ro   rp   r   r|   r}   rq   setboundaryenforce_floor_value)r   shape1shape2shape2ravelshape2p1shape3rA   rs   r6   rU   rt   ru   rv   rK   rL   r   r{   rz   s                     r   r   zSimulation._initializedust  s   diill#diill#S%67499<<		45		%a'TYY\\):;diill#SIILL&tyy||,. 99;;bhhv&6,@BDIIK"%((**DIIKK8;8W8W95y!S&&#99  ""*&+a%EPT'VDII!!#99  $$,(-c'BT)SDII!!%99  ''/+0f*EPT,VDII!!(99  ''/+0f*Dt,UDII!!(99  $$,(-c'>)ODII!!%99  &&.*/e):T+KDII!!'99  **2.3i-S^b/dDII!!+ 99;;bhhv&6,BDDIIK"%((**DIIKK99??&HHLL&&8E"'e)B#DDIIOO99??'HHLL&&8E#(e)E$GDIIOO 99??'HHLL&&8E#(e)D$FDIIOO  99== !$)%8:DIIM$'HHLLDIIMM!99<<#$T288,'@ BDIILL'*xx~~DIILL$99<<$ %dBHH-'@!BDIILL(+DIILL%99<<#$T288,'< >DIILL'*xx~~DIILL$99>>!"4*%57DIIN 99;;bhhv&6,@BDIIK"%((**DIIKK99#$T288,%@ BDII'*xxDII$99== !$)%RTDIIM$'HH$9$9DIIMM!99>>!88==++bggfo=D"d(FHDIIN 99;;#$T288,%@ BDIIKK'*xxDIIKK$99;;$ %dBHH-%;!=DIIKK(+(8(8DIIKK%99;;#$T288,%E GDIIKK'*xxDIIKK$99;;??"#D"((+%BDDIIKKO99;;??"#D"((+%FHDIIKKO&)hhnnDIIKKOO#99;;??"#D"((+%=?DIIKKO&)hhnnDIIKKOO#99<< rxx(%46DIIL#&88#>#>DIILL 99;;#HHMM''"''&/9E$e)H JDIIKK99;;??&"'bhh/%I#KDIIKKOO*-((*G*GDIIKKOO'99;;??  ($)$1%O%QDIIKKOO!,/HH,I,IDIIKKOO!!)99;;??&"'bhh/%F#HDIIKKOO*-((*D*DDIIKKOO'99;;??'#(rxx0%I$KDIIKKOO +.88+I+IDIIKKOO  (99;;??'#(rxx0%Q$SDIIKKOO +.88+J+JDIIKKOO  (99;;??&"'bhh/%E#GDIIKKOO*-((*;*;DIIKKOO'99;;'#(rxx0%D$FDIIKK +.88+=+=DIIKK  (99;;??"#D"((+%=?DIIKKO&)hhmmDIIKKOO#	II 99'SXX0066J#(j.W$YDII  99??"HH--d3EHHUdii&:&:: !5!55"$E $e)PRDIIO),)=)=		&&)hh&7&7		#		 $$))///[bfh		 tRXX&&SU		 99##+'/						&	(DII$ 99##+'/		DbD!		TrT"		"%$))..r22(DII$ 			  ,,.		  ,,.$$T*#'99??		C a	s   OA^ ^A^c                    t        | j                  j                        }t        | j                  j                        dz   }| j                  j                  S| j
                  j                  j                  t        j                  |      z  }t        | |d      | j                  _        | j                  j                  ct        | t        j                  |      d      | j                  _	        t        j                  j                  | j                  j                  _        | j                  j                  ct        | t        j                  |      d      | j                  _        t        j                  j                  | j                  j                  _        | j                  j                   ct        | t        j                  |      d      | j                  _        t        j                  j                   | j                  j                   _        | j                  j"                  ct        | t        j                  |      d      | j                  _        t        j                  j"                  | j                  j"                  _        | j                  j$                  ct        | t        j                  |      d	      | j                  _        t        j                  j&                  | j                  j$                  _        | j                  j(                  S| j
                  j                  j(                  t        j                  |      z  }t        | |d
      | j                  _        | j                  j*                  ct        | t        j                  |      d      | j                  _        t        j                  j,                  | j                  j*                  _        | j                  j.                  ct        | t        j                  |      d      | j                  _        t        j                  j.                  | j                  j.                  _        | j                  j0                  ct        | t        j                  |      d      | j                  _        t        j                  j2                  | j                  j0                  _        | j                  j4                  ct        | t        j                  |      d      | j                  _        t        j                  j6                  | j                  j4                  _        | j                  j8                  j:                  wt        | t        j                  |      d      | j                  j8                  _        t        j                  j<                  | j                  j8                  j:                  _        | j                  j8                  j>                  :t        | t        j                  |      d      | j                  j8                  _        | j                  j8                  j@                  wt        | t        j                  |      d      | j                  j8                  _         t        j                  jB                  | j                  j8                  j@                  _        | j                  jD                  3t        | dt        j                  |      z  d      | j                  _"        | j                  jF                  t        jH                  t        j                  jK                  | j                  jL                  | j
                  j                  jN                  | j
                  j                  jP                  | j
                  j                  jR                              }t        jT                  || j                  jD                        }t        | |d      | j                  _#        t        j                  jV                  | j                  jF                  _,        | j                  jZ                  ct        | t        j                  |      d      | j                  _-        t        j                  j\                  | j                  jZ                  _        | j                  j^                  j`                  :t        | t        j                  |      d      | j                  j^                  _0        | j                  j^                  jb                  wt        | t        j                  |      d      | j                  j^                  _1        t        j                  jd                  | j                  j^                  jb                  _        | j                  jb                  jf                  wt        | t        j                  |      d      | j                  jb                  _3        t        j                  jh                  | j                  jb                  jf                  _        | j                  jb                  jj                  wt        | t        j                  |      d      | j                  jb                  _5        t        j                  jl                  | j                  jb                  jj                  _        t        | t        jn                  | j                  jF                        dd      | j                  _8        t        | t        j                  |      d      | j                  _9        | jt                  jv                  jx                  :t        | t        j                  |      d      | jt                  jv                  _<        | jt                  jv                  jz                  :t        | t        j                  |      d      | jt                  jv                  _=        | j                  j}                          | j                  j~                  j                  t        | j                  jL                  | j                  j                  | j                  jF                        | j                  j~                  _@        | j                  j~                  j                  j                  d        | j                  j~                  j                  j                          | j                  j~                  j                  t        | j                  jL                  ddd!   | j                  j                  ddd!   | j                  jF                  ddd!         | j                  j~                  _E        | j                  j~                  j                  j                  d"| j                  jD                  d!          | j                  j~                  j                  j                          | j                  jF                  | j                  jp                  d#<   y)$z%Function to initialize gas quantitiesr   NzTurbulent alpha parameterr4   zIsothermal sound speed [cm/s]zPressure gradient parameterzGas flux interfaces [g/cm/s]zPressure scale height [cm]zMidplane mean free path [cm]zMean molecular weight [g]u   Miplane number density [1/cm³]u   Kinematic viscosity [cm²/s]u   Midplane pressure [g/cm/s²]u   Miplane mass density [g/cm³]r   r   u   Total sources [g/cm²/s]g0.++r   u   Surface density [g/cm²]zTemperature [K]u=   Specific angular momentum injection rate by torque [cm²/s²]z+Effective velocity imposed by torque [cm/s]z!Viscous accretion velocity [cm/s]r   r   Tr   r   zPull factorzPush factorr   r   r   .)Fr   r2   r*   r1   r   r   r   r   r   r[   r   r   cs_isothermalrn   ra   eta_midplanerw   r\   r_   mfp_midplaner    r^   
n_midplaner]   r`   
P_midplanerQ   r   rX   ry   r   rC   r>   r   r{   rz   arraylyndenbellpringle1974r|   r"   r!   r   maximumr  r  r/   	T_passiverb   rY   rS   vtorquerZ   vviscr8   r   
zeros_liker  r   r0   rR   r6   r7   r   ro   rp   r   r}   setconditionr  rq   )r   r
  shape1p1r   r    SigmaGass         r   r   zSimulation._initializegas  s   diill#		%a'88>>!HHLL&&8E"e)DFDHHN 88;;bhhv&6,KMDHHK"%''"7"7DHHKK88<< rxx(%BDDHHL#&77#7#7DHHLL 88;;bhhx&8,JLDHHK"%''**DHHKK88;;bhhv&6,HJDHHK"%''**DHHKK88<< rxx'7-KMDHHL#&77#7#7DHHLL 88;;2776?2Bb,GIDHHK 88::tRXXf%5+LNDHHJ!$!3!3DHHJJ88;;bhhv&6,JLDHHK"%''**DHHKK88::tRXXf%5+IKDHHJ!$!3!3DHHJJ88<< rxx'7-LNDHHL#&77#7#7DHHLL 88::>>!"4*%FHDHHJJN%(WW]]DHHJJNN"88::>>!"4*%BDDHHJJN88::>>!"4*%?ADHHJJN%(WW]]DHHJJNN"88&"'gbggfo-;d#fDHH 88>>!xx = =		TXX\\11488<<3H3H$((,,J\J\!^ _Hzz(DHH,?,?@H"4/IKDHHN%(WW%5%5"88::tRXXf%5+<>DHHJ!$!2!2DHHJJ88??!!)%*4&1A7v&xDHHOO"88??$ %dBHHV,<2_!aDHHOO(+DHHOO% 88::??"#D"((6*:0SUDHHJJO&)ggmmDHHJJOO#88::>>!"4&)9/GIDHHJJN%(WW\\DHHJJNN" #"--/=iptv dBHH%!NP 99!!##+',bggfo=(BDII""$99!!##+',bhhv&M(CDII""$ 	88""*&.		TYY\\488>>';DHH#HH##00>++-88""*&.		DbD!499<<"#5txx~~dd7K'MDHH#HH##00txx**2.0++-"&((..3r   c                 h   t        | j                  j                        }| j                  j                  ct	        | t        j                  |      d      | j                  _        t        j                  j                  | j                  j                  _        | j                  j                          y)z&Function to initialize grid quantitiesNzKeplerian frequency [1/s]r4   )
r   r2   r*   rc   r   r   r   r   rn   r   )r   r
  s     r   r   zSimulation._initializegrid   sp    diill#99#$T288F+;1L NDII'*xxDII$		r   c                    | j                   j                  Pt        | dd      | j                   _        t        j                   j                  | j                   j                  _        | j                   j                  ;t        | | j                  j                   j                  d      | j                   _        | j                   j                  ;t        | | j                  j                   j                  d      | j                   _        | j                   j                  ;t        | | j                  j                   j                  d      | j                   _	        | j                   j                          y)z-Function to initialize the stellar quantitiesNr   zLuminosity [erg/s]r4   zMass [g]zRadius [cm]zEffective temperature [K])r3   rd   r   r   
luminosityrn   r-   r   r.   r/   r   r   s    r   r   zSimulation._initializestar+  s     99;;b6JKDIIK"%(("5"5DIIKK99;;dhhmmoo,68DIIK 99;;dhhmmoo,9;DIIK 99;;dhhmmoo,GIDIIK 			r   c                 B
   t        | j                  j                  t              r$t        | j                  j                  t              st        d      t        | j                  j                        D ])  \  }}|j                  | j                  j                  u s) n |dk(  r/t        | j                  j                  | j                  j                  z        }t        | | j                  j                  dd      | j                  _        t        | t        j                  |      d      | j                  _        t"        j                  j$                  | j                  j                  _        t"        j(                  j*                  | j,                  _        g d| j                  j0                  _        g d	| j                  _        d
dg| j                  j2                  _        t"        j(                  j4                  | j                  _        t"        j(                  j8                  | j                  _        |dk(  rlt=        t"        j                  j>                  | j                  j                  d| j                  j                   id      }|| j                  j                  <   n|dk(  rtA        d      |dk(  rtA        d      t        d      |dk(  rtC        | j                  d      r| j                  `tC        | j                  d      r| j                  `d| j                  j                  _        g d| j                  j0                  _        g d| j                  _        g d| j                  j2                  _        t"        j(                  jD                  | j                  _        t"        j(                  jF                  | j                  _        |dk(  rt"        j(                  jH                  | j,                  _        t=        tJ        jL                  | j                  j                  | j                  j2                  jN                  dddd       }|| j                  j                  <   | j,                  jQ                  d!tR        jT                  z         nt        d"      t        d#      | j                  jW                          | jY                          | jZ                  d$kD  r2d%j]                  t_        |d&      t_        |d&            }ta        |       yy)'a  Function sets the dust integrator.

        Parameters
        ----------
        scheme : string, optional, default : "explicit"
            Possible values
                {"explicit", "implicit"}
        method : string, optional, default : "cash-karp"
            Possible values for explicit integration
                {"cash-karp"}
            Possible values for implicit integration
                {"direct", "gmres", "bicgstab}zNThe simulation frame has to be initialized before calling setdustintegrator().implicitr   Tr   r   r4   rG   rJ   rC   r>   directr   r   r   gmresz%GMRES method is not implemented, yet.bicgstabz!BiCGSTAB is not implemented, yet.z(Invalid method for implicit integration.explicitr  r   N)r@   rH   r8   rI   )rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rw   rV   rW   rX   )rx   ry   rC   r>   	cash-karpr   g?)dYdxrU   rX   z2Dust: explicit 5th-order adaptive Cash-Karp methodr   z(Invalid method for explicit integration.zUnknown integration scheme.r   z5Setting dust integrator
    scheme: {}
    method: {}r   )1r   r2   r~   r   r*   r   	enumerater   r   Yr0   rz   r   r  r   r   r   r   r  r  r   r   r   rn   rS   rX   r   r   r   r   r   r   NotImplementedErrorhasattrprepare_explicit_dustfinalize_explicit_dustdt_adaptiver   expl_5_cash_karp_adptvr>   suggestrh   r   r   r   r   r   r
   r   )r   schememethodr   instr  r   s          r   setdustintegratorzSimulation.setdustintegrator@  s    $)),,.ju6U`b b !!=!=> 	GAtvv(	 Ztyy||DIILL89K
 #(diioo3_fj#lDII #4**WYDIIN +.((*;*;DIIOO' !WWZZDFFN #>DIIKK!]DII#(%.DIIKK *-)F)FDOO&(+(F(FDOO% !"388#9#9#'99??/4diinn /1/W	% 37,,Q/7");= =:%)*MNN"#MNNz! tyy+.II'tyy&)IIN +/DIIOO' #EDIIKK!cDII"?DIIKK *-)F)FDOO&(+(F(FDOO%$ "%!4!4 #7#A#A#'99??7;yy{{6947/1 0d% 37,,Q/r!&&y) ##MNN<==!!#>>AKRR((66*BDC#J r   )r'  r(  )__name__
__module____qualname____doc__rg   r   propertyr   r   r   r   r   r   r   r   r   r   r6  __classcell__)r   s   @r   r   r      sl    	T HfP*  AS(eN;zW3rM1^	*wr   r   )simframer   r   r   r   simframe.framer   r   r	   simframe.utils.colorr
   dustpy.constants	constantsrh   dustpyr   simframe.io.writersr   dustpy.utils.boundaryr   dustpy.utils.simplenamespacer   numpyr   r   re   r   r   <module>rG     s=             ! )   * * 8 b br   