PLaSK library

Loading...
Searching...
No Matches
your_solver.cpp
Go to the documentation of this file.
1/*
2 * This file is part of PLaSK (https://plask.app) by Photonics Group at TUL
3 * Copyright (c) 2022 Lodz University of Technology
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14#include "your_solver.hpp"
15
16 namespace plask { namespace category { namespace your_solver {
17
18YourSolver::YourSolver(const std::string& name=""): SolverWithMesh<ForExample_Geometry2DCartesian, ForExample_RectilinearMesh2D>(name),
19 outSomeField(this, &YourSolver::getDelegated) // getDelegated will be called whether provider value is requested
20{
21 inTemperature = 300.; // temperature receiver has some sensible value
22}
23
24
25 void YourSolver::loadConfiguration(XMLReader& reader, Manager& manager) {
26 // Load a configuration parameter from XML.
27 // Below you have an example
28 while (reader.requireTagOrEnd()) {
29 std::string param = reader.getNodeName();
30 if (param == "newton") {
31 newton.tolx = reader.getAttribute<double>("tolx", newton.tolx);
32 newton.tolf = reader.getAttribute<double>("tolf", newton.tolf);
33 newton.maxstep = reader.getAttribute<double>("maxstep", newton.maxstep);
34 reader.requireTagEnd();
35 } else if (param == "wavelength") {
36 std::string = reader.requireTextUntilEnd();
37 inWavelength.setValue(boost::lexical_cast<double>(wavelength));
38 } else
39 parseStandardConfiguration(reader, manager, "<geometry>, <mesh>, <newton>, or <wavelength>");
40 }
41}
42
43
44 void YourSolver::compute(double parameter)
45{
46 // Below we show some key objects of the computational methods
47 initCalculation(); // This must be called before any calculation!
48 writelog(LOG_INFO, "Begining calculation of something");
49 auto temperature = inTemperature(*mesh); // Obtain temperature from some other solver
50 // Please mind that temperature can be NaN. You should test this assume 300K in such case.
51 // [...] Do your computations here
52 outSingleValue = new_computed_value;
53 writelog(LOG_RESULT, "Found new value of something = $1$", new_computed_value);
54 outSingleValue.fireChanged(); // Inform other solvers that you have computed a new value
55 outSomeField.fireChanged();
56}
57
58
59 void YourSolver::onInitialize() // In this function check if geometry and mesh are set
60{
61 if (!geometry) throw NoGeometryException(getId());
62 if (!mesh) throw NoMeshException(getId());
63 my_data.reset(mesh->size()); // and e.g. allocate memory
64}
65
66
67 void YourSolver::onInvalidate() // This will be called when e.g. geometry or mesh changes and your results become outdated
68{
69 outSingleValue.invalidate(); // clear the value
70 my_data.reset();
71 // Make sure that no provider returns any value.
72 // If this method has been called, before next computations, onInitialize will be called.
73}
74
75
76 const DataVector<const double> YourSolver::getDelegated(const MeshD<2>& dst_mesh, InterpolationMethod method) {
77 if (!outSingleValue.hasValue()) // this is one possible indication that the solver is invalidated
78 throw NoValue(SomeSingleValueProperty::NAME);
79 return interpolate(*mesh, my_data, dst_mesh, getInterpolationMethod<INTERPOLATION_LINEAR>(method)); // interpolate your data to the requested mesh
80}
81
82
83}}} // namespace