Source code for openquake.calculators.classical_damage
# -*- coding: utf-8 -*-# vim: tabstop=4 shiftwidth=4 softtabstop=4## Copyright (C) 2014-2023 GEM Foundation## OpenQuake is free software: you can redistribute it and/or modify it# under the terms of the GNU Affero General Public License as published# by the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## OpenQuake 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 Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with OpenQuake. If not, see <http://www.gnu.org/licenses/>.importloggingimportnumpyfromopenquake.baselib.generalimportAccumDictfromopenquake.hazardlibimportstatsfromopenquake.calculatorsimportbase,classical_risk,viewsF32=numpy.float32
[docs]defclassical_damage(riskinputs,param,monitor):""" Core function for a classical damage computation. :param riskinputs: :class:`openquake.risklib.riskinput.RiskInput` objects :param param: dictionary of extra parameters :param monitor: :class:`openquake.baselib.performance.Monitor` instance :yields: dictionaries asset_ordinal -> damage(R, L, D) """crmodel=monitor.read('crmodel')mon=monitor('getting hazard',measuremem=False)forriinriskinputs:R=ri.hazard_getter.RL=len(crmodel.lti)D=len(crmodel.damage_states)result=AccumDict(accum=numpy.zeros((R,L,D),F32))withmon:haz=ri.hazard_getter.get_hazard()fortaxo,assetsinri.asset_df.groupby('taxonomy'):forrlzinrange(R):hcurve=haz[:,rlz]out=crmodel.get_output(assets,hcurve)forli,loss_typeinenumerate(crmodel.loss_types):fora,fracinzip(assets.ordinal,out[loss_type]):result[a][rlz,li]=fracyieldresult
[docs]defpost_execute(self,result):""" Export the result in CSV format. :param result: a dictionary asset_ordinal -> array(R, L, D) """D=len(self.crmodel.damage_states)damages=numpy.zeros((self.A,self.R,self.L,D),numpy.float32)forainresult:damages[a]=result[a]self.datastore['damages-rlzs']=damagesstats.set_rlzs_stats(self.datastore,'damages-rlzs',assets=self.assetcol['id'],loss_type=self.oqparam.loss_types,dmg_state=self.crmodel.damage_states)dmg=views.view('portfolio_damage',self.datastore)logging.info('\n'+views.text_table(dmg,ext='org'))