Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:19:07

0001 /*******************************************************************************
0002  * Copyright (c) 2018-2019 LongGang Pang, lgpang@qq.com
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and/or associated documentation files (the
0006  * "Materials"), to deal in the Materials without restriction, including
0007  * without limitation the rights to use, copy, modify, merge, publish,
0008  * distribute, sublicense, and/or sell copies of the Materials, and to
0009  * permit persons to whom the Materials are furnished to do so, subject to
0010  * the following conditions:
0011  *
0012  * The above copyright notice and this permission notice shall be included
0013  * in all copies or substantial portions of the Materials.
0014  *
0015  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0016  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0018  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
0019  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
0020  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
0021  * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
0022  ******************************************************************************/
0023 
0024 
0025 /*!< Use cpp exceptionn to handel errors */
0026 #define __CL_ENABLE_EXCEPTIONS 
0027 // System includes
0028 //#include <CL/cl.hpp>
0029 #include <cstdlib>
0030 #include <cstdio>
0031 #include <string>
0032 #include <vector>
0033 #include <cmath>
0034 #include <iostream>
0035 #include <fstream>
0036 #include <sstream>
0037 #include <cassert>
0038 #include <ctime>
0039 #include <algorithm>
0040 #include <map>
0041 
0042 #include <random>
0043 
0044 #include "include/Config.h"
0045 #include "include/opencl_backend.h"
0046 #include "include/bulkinfo.h"
0047 #include "include/error_msgs.h"
0048 
0049 namespace clvisc {
0050 
0051 
0052 BulkInfo::BulkInfo(int nx, int ny, int neta,
0053            int nx_skip, int ny_skip, int neta_skip,
0054            const OpenclBackend & backend, 
0055            const std::string & compile_option): nx_(nx), ny_(ny), neta_(neta), 
0056            nx_skip_(nx_skip), ny_skip_(ny_skip), neta_skip_(neta_skip),
0057            backend_(backend), compile_option_(compile_option){
0058     nx_out_ = int(floor((nx_-1)/nx_skip_)) + 1;
0059     ny_out_ = int(floor((ny_-1)/ny_skip_)) + 1;
0060     neta_out_ = int(floor((neta_-1)/neta_skip_)) + 1;
0061     /** tell jetscape what data is stored 
0062     * in std::vector<float> bulk_data_ */
0063     data_info_.push_back("energy_density");
0064     data_info_.push_back("entropy_density");
0065     data_info_.push_back("temperature");
0066     data_info_.push_back("pressure");
0067     data_info_.push_back("vx");
0068     data_info_.push_back("vy");
0069     data_info_.push_back("vz");
0070     data_info_.push_back("qgp_fraction");
0071     data_info_.push_back("pi00");
0072     data_info_.push_back("pi01");
0073     data_info_.push_back("pi02");
0074     data_info_.push_back("pi03");
0075     data_info_.push_back("pi11");
0076     data_info_.push_back("pi12");
0077     data_info_.push_back("pi13");
0078     data_info_.push_back("pi22");
0079     data_info_.push_back("pi23");
0080     data_info_.push_back("pi33");
0081 
0082     size_t size_out = nx_out_ * ny_out_ * neta_out_ * data_info_.size();
0083     h_bulk3d_1step_.resize(size_out);
0084     try {
0085         // build kernels for hydrodynamic evolution
0086         auto prg = backend_.BuildProgram("clvisc_kernel/kernel_bulk3d.cl", compile_option_);
0087         kernel_bulk3d_ = cl::Kernel(prg, "kernel_bulk3d");
0088         d_bulk3d_1step_ = backend_.CreateBuffer(size_out * sizeof(float));
0089     } catch (cl::Error & err ){
0090         std::cerr<<"Error:"<<err.what()<<"("<<err.err()<<")\n";
0091         std::cerr<<"@" << __FILE__ << ":line " << __LINE__ << std::endl;
0092         std::cerr<<ErrorMessage(err.err())<<std::endl;
0093         throw(err);
0094     }
0095 }
0096 
0097 /* push the fluid cell info of one time step back into a big vector */
0098 void BulkInfo::add_data(const cl::Buffer & d_ev, 
0099                         const cl::Buffer & d_shear_pi,
0100                         const cl::Buffer & d_bulk_pi,
0101                         const cl::Image2D & eos_table){
0102     kernel_bulk3d_.setArg(0, d_bulk3d_1step_);
0103     kernel_bulk3d_.setArg(1, d_ev);
0104     kernel_bulk3d_.setArg(2, d_shear_pi);
0105     kernel_bulk3d_.setArg(3, d_bulk_pi);
0106     kernel_bulk3d_.setArg(4, eos_table);
0107     backend_.enqueue_run(kernel_bulk3d_,
0108             cl::NDRange(nx_out_, ny_out_, neta_out_),
0109             cl::NullRange);
0110     backend_.enqueue_copy(d_bulk3d_1step_, h_bulk3d_1step_);
0111     for (auto val : h_bulk3d_1step_) {
0112         bulk_data_.push_back(val);
0113     }
0114 }
0115 
0116 const std::vector<float> & BulkInfo::get_data() {
0117     return bulk_data_;
0118 }
0119 
0120 const std::vector<std::string> & BulkInfo::get_data_info() {
0121     return data_info_;
0122 }
0123 
0124 void BulkInfo::save(const std::string & fpath){
0125   // save the data to given fpath
0126     std::ofstream fout(fpath);
0127   int idx = 0;
0128   fout << "# nx=" << nx_out_ << std::endl;
0129   fout << "# ny=" << ny_out_ << std::endl;
0130   fout << "# nz=" << neta_out_ << std::endl;
0131   fout << "# num_entries =" << data_info_.size() << std::endl;
0132   fout << "# ";
0133   for (auto val : data_info_) {
0134       fout << val << " ";
0135   }
0136   fout << std::endl;
0137   for (auto val : bulk_data_) {
0138       idx ++;
0139       fout << val << " ";
0140       if (idx % data_info_.size() == 0) {
0141           fout << std::endl;
0142       }
0143   }
0144   fout.close();
0145 }
0146 
0147 } // end namespace clvisc
0148 
0149 
0150 /*! \mainpage 
0151  *  \section  */
0152 
0153 /*! 
0154  *  \example 
0155 *
0156  *  \code
0157  *
0158  *
0159 
0160 
0161  * \endcode
0162 */
0163 
0164